Spring: Request method 'PUT' not supported - java

I started with one of the Spring getting started samples. I am extending it to match my scenario. I am trying to use the PUT method on a web service call. I get the error message "Request method 'PUT' not supported". But, execution makes it into the web service. The error occurs after/during returning. Is there something I need to do to my objects to allow the to be returned from non-GET HTTP methods?
I am calling into the web service with a test stub written in python. I have not posted that code since execution is getting into the web service.
Following is the Spring code:
#ComponentScan
#EnableAutoConfiguration
#Controller
#RequestMapping("/jp5/rest/message")
public class MessageRestService
{
#RequestMapping(method=RequestMethod.PUT, value="/test")
public testResult test()
{
// I hit a breakpoint here:
return new testResult(true, "test");
}
}
class testResult
{
public testResult( boolean success, String message )
{
setSuccess(success);
setMessage(message);
}
//#XmlElement
private boolean success;
//#XmlElement
private String message;
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Edit
There is no stack trace, just this in the server output:
2013-11-13 21:26:20.976 WARN 5452 --- [nio-8888-exec-1]
o.s.web.servlet.PageNotFound :
Request method 'PUT' not supported
Here is the python as requested. And, I think the answer to the problem lies in "'allow': 'GET, HEAD'" in the response. So, how do I allow other methods? Maybe I need to think about an applicationContext?
path = '/jp5/rest/message/test'
method = 'PUT'
body = ''
target = urlparse(self.uri+path)
h = http.Http()
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json; charset=UTF-8'
}
response, content = h.request(
target.geturl(),
method,
body,
headers)
print response
output from the print:
{'status': '405', 'content-length': '1045', 'content-language': 'en-US', 'server':
'Apache-Coyote/1.1', 'allow': 'GET, HEAD', 'date': 'Thu, 14 Nov 2013 02:26:20 GMT',
'content-type': 'text/html;charset=utf-8'}
I am starting the server like this:
#ComponentScan
#EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Thanks

Thanks for the pointers. The solution is to add a #ResponseBody:
public #ResponseBody testResult test()
{
return new testResult(true, "test");
}

Related

Spring boot & Java - HTTP Status 404 error aka white-label error

Please have a look at my codes below. The Java codes seemed to work just fine, but localhost:8080 gives me the error code 404 when I try to access it. I want to make localhost 8080 work. Please let me know if you need further information.
Application
#SpringBootApplication(exclude = { ErrorMvcAutoConfiguration.class })
// exclude part is to elimnate whitelabel error
#EnableScheduling
public class Covid19TrackerApplication {
public static void main(String[] args) {
SpringApplication.run(Covid19TrackerApplication.class, args);
}
}
Controller
#Controller
public class HomeController {
CovidDataService covidDataService;
#RequestMapping("/")
public #ResponseBody String home(Model model) {
model.addAttribute( "locationStats", covidDataService.getAllStats());
return "home";
}
}
Main Code
#Service
public class CovidDataService {
private static String Covid_Data_URL = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv";
private List<LocationStats> allStats = new ArrayList<>();
public List<LocationStats> getAllStats() {
return allStats;
}
#PostConstruct//?
#Scheduled(cron = "* * 1 * * *") //????
// * sec * min *hour and so on
public void fetchCovidData() throws IOException, InterruptedException {
List<LocationStats> newStats = new ArrayList<>(); // why we are adding this? To prevent user get an error while we are working on new data.
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(Covid_Data_URL))
.build(); // uri = uniform resource identifier
HttpResponse<String> httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString());
StringReader csvBodyReader = new StringReader(httpResponse.body()); //StringReader needs to be imported
Iterable<CSVRecord> records = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(csvBodyReader); // parse(in) had error, we needed a "reader" instance.
for (CSVRecord record : records) {
LocationStats locationStat = new LocationStats(); //create an instance
locationStat.setState(record.get("Province/State"));
locationStat.setCountry(record.get("Country/Region"));
locationStat.setLatestTotalCase(Integer.parseInt(record.get(record.size()-1)));
System.out.println(locationStat);
newStats.add(locationStat);
}
this.allStats = newStats;
}
}
The problem may come from this piece of code
#RequestMapping("/")
public #ResponseBody String home(Model model) {
model.addAttribute( "locationStats", covidDataService.getAllStats());
return "home";
}
it returns "home" which should be existing view, normally, the view will be a jsp file which is placed somewhere in WEB-INF, please see this tutorial: https://www.baeldung.com/spring-mvc-view-resolver-tutorial
In the case of wrong mapping, it may returns 404 error
when you run the server, you should be able to see which port it's taken in the console.
Also, is server.port=8080 in the src/main/resources/application.properties file?
In the controller, the RequestMapping annotation is missing the method type and header
#RequestMapping(
path="/",
method= RequestMethod.GET,
produces=MediaType.APPLICATION_JSON_VALUE)
public String home(Model model) {
model.addAttribute( "locationStats", covidDataService.getAllStats());
return "home";
}
make sure to add consumes for POST or PUT methods
A bit unrelated to the question but the line in the controller is missing #Autowired annotation
CovidDataService covidDataService;
Preferrably, add the #Autowired in the constructor
#Autowired
public HomeController(CovidDataService covidDataService) {
this.covidDataService = covidDataService;
}

Fallback Factory not working to handle Custom Exception in Feign Client

My requirement is to access the custom exception thrown from first service along with it's body content in the second service
I have tried 2 things so far, FallbackFactory and ErrorDecoder, out of which only Fallback factory worked for me. Error decoder did not have the message of the exception which was thrown from other service. Here is the sample code that I found in another question:
There will be 2 services: inventory-service and product-service
inventory-service
InventoryController.java
#RestController
#RequestMapping("/inventories")
public class InventoryController {
private final ProductServiceClient productService;
public InventoryController(ProductServiceClient productService) {
super();
this.productService = productService;
}
#GetMapping
public ResponseEntity<?> companyInfo() {
return productService.hello();
}
}
ProductServiceClient.java
#FeignClient(name = "product-service", url = "http://localhost:9100", fallbackFactory = ProductServiceClientFallback.class)
public interface ProductServiceClient {
#GetMapping("/products")
ResponseEntity<?> hello();
}
#Component
class ProductServiceClientFallback implements FallbackFactory<ProductServiceClient> {
#Override
public ProductServiceClient create(Throwable cause) {
return new ProductServiceClient() {
#Override
public ResponseEntity<?> hello() {
System.out.println("hello!! fallback reason was " + cause.getMessage());
return ResponseEntity.ok().build();
}
};
}
}
product-service
ProductController.java
#RestController
#RequestMapping(value = "/products")
public class ProductController {
#GetMapping
public String hello() throws Exception {
if (true) {
throw new Exception("Service B Exception...");
}
return "Hello World";
}
}
ProductControllerAdvice.java
#RestControllerAdvice
public class ProductControllerAdvice {
#ExceptionHandler
public ResponseEntity<?> handleException(Exception exception) {
return new ResponseEntity<>("Caused due to : " + exception.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
So, when /inventories api is triggered in Inventory controller, it triggers a call to product-service via Feign Client and on product-service side, I throw a custom exception with a message, I have to access that message in my inventory-service.
To get that I have implemented fallback factory and it worked in a test-workspace since I got an output like this in console of inventory-service
hello!! fallback reason was status 500 reading ProductServiceClient#hello(); content:
Caused due to : Service B Exception...
But, my problem is when I try the similar approach with the applications that I'm working on, I did not get the message of exception, instead I got an out put like this:
reached fallback on workflow side, reason: status 400 reading ProvisioningServiceProxy#executeOrderAction(Long,Long,String)
Service-A
TestServiceA.java
#FeignClient( url = "/executeOrder", fallbackFactory = TestServiceAFallback.class )
public interface TestServiceA extends Serializable{
#PostMapping( value = "order/{requestId}/order/{orderId}/{command}" )
public ResponseEntity<ProcessInstanceVariable> executeOrderAction( #PathVariable( name = "command" ) String command );
}
Service-B from where the custom exception is thrown
TestServiceBController.java
#PostMapping( value = /executeOrder )
public ResponseEntity<ProcessInstanceVariable> executeOrderAction( #PathVariable( value = "command" ) String command )
{ //switch code to check the command value and throw exception for one particular command
throw new ValidationException("validation exception from service B");
}
I have an advice also, which handles Validation Exceptions and there is a method like this in that class
TestServiceBControllerAdvice.java
#ExceptionHandler( ValidationException.class )
public ResponseEntity<Object> handleValidationException( ValidationException ve )
{
return new ResponseEntity<>( ve.getMessage(), HttpStatus.BAD_REQUEST );
}
So, I was expecting to receive the message on TestServiceA side which I sent from TestServiceB, but I received a generic message showing that BAD REQUEST while reading the API.
I'm not sure if any extra configuration is required on TestServiceA side apart from below configuration:
testServiceA.properties
feign.hystrix.enabled=true
Let me know if anything is missing from my end, I have gone through this documentation and seems to me I have done the implementation the way it should happen to get the message and body of exception thrown from other service.
For anyone who comes to this question looking for some answers, I did end up implementing ErrorDecoder, which helped me in capturing the errors. The details are a little fade to me, how the message was caught.
But I used the below code:
public class CustomExceptionDecoder implements feign.codec.ErrorDecoder
{
#Override
public Exception decode( String methodKey,
Response response )
{
final ErrorDecoder defaultErrorDecoder = new Default();
try
{
if( response.body() != null )
{
byte[] bodyData = Util.toByteArray( response.body().asInputStream() );
String responseBody = new String( bodyData );
LOGGER.error( "Error captured in Custom Exception Decoder: ", responseBody );
return new CustomValidationException( responseBody );
}
}
catch( IOException e )
{
LOGGER.error( "Throwing IOException :: {}", e.getCause() );
}
return defaultErrorDecoder.decode( methodKey, response );
}
}

javax.ws.rs.NotFoundException: HTTP 404 Not Found angular 1.5

i have this client angular 1.5 code
var getGroupForUser = function () {
var deferred = $q.defer();
$http.get(env.domain+'/Voices/authorize').then(
function successCallback(response) {
// self.isAdOps = response.data.;
deferred.resolve(response.data);
}, function errorCallback(response) {
console.log(response.data.errorMsg);
self.isAdOps = true;
deferred.reject("data: "+response.data+" code:"+response.status+" "+response.statusText+", please look at the web console");
});
return deferred.promise;
};
and this jersey java code on the server:
#Path("/Voices")
public class VoicesOperation {
#Path("/search")
#GET
#Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public List<VoiceUi> search(#QueryParam("q") String searchTerm) throws Exception {...
}
#Path("/authorize")
#GET
#Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public String authorize() {
logger.info("in voicesOperation - authorization");
logger.error("checking error log ==== in voicesOperation - authorization");
However i get in the server an error:
javax.ws.rs.NotFoundException: HTTP 404 Not Found
what am i missing?
Just modify your like below and try:-
public String authorize(#Context UriInfo info) {
.
.
}

Custom HTTP status code

I consider using fluent-http in a project.
I started with a simple "login/password" page. I create a simple POJO with fields login and password :
public class LoginRequest() {
private String login;
private String password;
//...
}
And I send it to fluent-http via a Resource :
#Prefix("/user")
public class PersonResource {
#Post("/")
public String get(LoginRequest loginRequest) {
//[...]
}
}
And it works well :)
Now, I wondered if it was possible to send a response with code HTTP 200 in case of success and code HTTP 401 in case of failure.
So I tried to inject the Response :
#Post("/")
public String login(LoginRequest loginRequest, Response response) {
if(loginRequest.getPassword().equals("helloworld")) {
response.setStatus(200);
return "SUCCESS";
} else {
response.setStatus(401);
return "ERROR";
}
}
The correct String is returned but the status code does not seem to be used. In both cases, the response has a code HTTP 200.
Note : I found that some status code are pre-implemented :
In case of exception, a code 500 is returned.
In case of resource not found, a code 400 is returned.
Any idea?
If you want to change the default content-type, status or headers, the method should return a net.codestory.http.payload.Payload.
Here's what you should write:
#Post("/")
public Payload login(LoginRequest loginRequest) {
if(!loginRequest.getPassword().equals("helloworld")) {
return new Payload("ERROR").withCode(HttpStatus.UNAUTHORIZED);
}
return new Payload("SUCCESS").withCode(HttpStatus.CREATED);
}

post service is not calling throwing 400 (Bad Request)

Hi friends I am using Angularjs and rest-servies but when I am calling rest services from service.js file something is goning wrong and it is throwing 400(bad request )
main.js
garantiesService.getTarifs($scope.recap.ageDirigeant,$scope.selectedCompany.zipcode)
.success(function(){
console.log('in success');
})
service.js
healthApp.factory('garantiesService', ['$http', function($http) {
var service = {
getTarifs: function(age,zipcode)
{
console.log("age : "+age);
console.log("zipcode : "+zipcode);
var directorHealthInsuranceInfo = {};
directorHealthInsuranceInfo.age=age;
directorHealthInsuranceInfo.department=zipcode;
return $http.post('rest-service/quotes/health /director',directorHealthInsuranceInfo);
}
};
return service;
HealthInsuranceController.java
#Controller
public class HealthInsuranceQuoteResource {
#RequestMapping("quotes/health/director")
#ResponseBody
public String quoteDirector(#RequestBody DirectorHealthInsuranceInfo info) {
System.out.println("------HealthInsuranceQuoteResult------");
return "hi";
}
DirectorHealthInsuranceInfo.java
#Value
public class DirectorHealthInsuranceInfo {
private String department;
private int age;
}
when I am sending the request it is throwing Bad Request 400 error.
I see that there is a space in the url you supplied to the http.post method.
"rest-service/quotes/health /director"
I don't know if that is causing it.
But I also see that you POST your request to the service. Are you sure that your endpoint has been set up for POST requests?
I would recommend creating a basic endpoint that you call with a GET request, and no parameters. Just to root out the problem.

Categories

Resources