I am using spring rest APIs and getting Http 405 error for all the POST methods.
I have following POST method,
#RequestMapping(value = "/GetPlanByBasicContext/", method = RequestMethod.POST)
public #ResponseBody Plan getPlanByBasicContext(#RequestBody BasicPlanContext basicPlanContext)
{
return planService.getPlanByBasicContext(basicPlanContext);
}
I am using fiddler to post the following request,
POST
http://localhost:8080/now/Plan/GetPlanByBasicContext
{ "sourceLocation":"",
"destinationLocation":"",
"modeOfTransport":"car"
"budget":"any"
}
Same attributes are present in BasicPlanContext on the server, along with getters and setters.
I have tried other solutions mentioned and nothing has worked.
Note: Security is not configured for spring yet.
you are posting to a wrong URL , you are missing a trailing slash in the end of your URL, try posting to : http:// localhost:8080/now/Plan/GetPlanByBasicContext/
Related
Below is the request mapping method:
#GetMapping("/redirect")
public ResponseEntity<Void> redirect() {
String url = "http://yahoo.com";
return ResponseEntity.status(HttpStatus.FOUND)
.location(URI.create(url))
.build();
}
When I hit the URL http://somehost:8080/redirect in the browser I see that it takes me to yahoo.com, but when the /redirect is called from the UI(reactjs) the 302 Found httpstatus value is returned in the browser console but the page on the browser is blank. I was expecting to see the yahoo.com page. Seems it is not redirecting.
I referred this link: Redirect to an external URL from controller action in Spring MVC
reactjs code:
yield globalAxios.get(http://somehost:8080/redirect)
Below image when the http://somehost:8080/redirect gets called from the UI
Below image is when we the /redirect redirects to the link: yahoo.com
Is it because of the 405 method not allowed error as seen in the above image
Just in case if someone run into something like this in the future.
I end up using this code getting rid of 405 method not allowed while I am doing PUT-REDIRECT-GET pattern.
Notice it is #Controller and not #RestContorller. Otherwise it won't work.
If this is to be implemented in an existing rest controller you may want to add #ResponseBody over the other methods but not on these.
#Controller
#RequestMapping("/redirect")
public class RedirectController {
#PutMapping()
public String redirect() {
return "redirect:/redirect";
}
#GetMapping()
public String redirectPost() {
return "redirect:https://www.google.com";
}
}
I have a web application written on Spring 3.1 (not boot) and running on Tomcat 7.
I have a #Controller implements method PUT on a certain URL.
In some cases When sending a PUT request from Postman, I get a 403 response instead of what is expected.
For example:
Sending the request to a non-implemented URL (on GET to the same URL I get a 404)
Sending an invalid JSON as the request body (Expected 400)
Sending a string instead of a numeric request parameter (Expected 400)
I also implement a filter that excepts all requests and just before the filter exists, I can verify I get the expected status from the rest of the chain.
This is an example of a controller code:
#RequestMapping(value = "/{book}", method = RequestMethod.PUT)
#ResponseStatus(HttpStatus.OK)
#ResponseBody
protected Book put(#PathVariable(value = "bookId") String id, #RequestBody #Valid Book book) {
return book; // just a stub
}
And this is the relevant part in the filter:
filterChain.doFilter(req, res);
// res.getStatus() is the expected status
return; // after this line I move to internal code of Tomcat which I cannot debug, but something happens there.
What do I miss?
Thanks
Check out CORS filter configuration first as Andreas said: https://tomcat.apache.org/tomcat-8.5-doc/config/filter.html
Check out this flowchart also https://tomcat.apache.org/tomcat-8.5-doc/images/cors-flowchart.png
Check out this stackoverflow post finally 403 on JSON PUT request to Tomcat with Spring 3.0.5 and Jackson
Your path variable value is bookId, but your url uses {book}; both should match. Try changing the url to "/{bookId}" or the path variable to #PathVariable(value = "book"). It might be useful to know the URL that you are calling to help analyse the issue.
I have a webservice which calls another WS and returns the response from the second WS. It looks like so:
// MyController
public ResponseEntity<Foo> requestFooController(#RequestBody #Valid Bar request) {
return this.myService.requestFooService(request);
}
//MyService
ResponseEntity<Foo> requestFooService(Bar request) {
Buzz improvedRequest = ...
return this.secondWS.secondRequestFoo(improvedRequest);
}
When I call the API through Postman, I receive a HTTP OK response with an empty body. Yet, when I'm in debug mode I can see that the service is returning a ResponseEntity with a body. The headers are not lost though.
I changed my code like so and it works fine:
// MyController
public ResponseEntity<Foo> requestFooController(#RequestBody #Valid Bar request) {
ResponseEntity<Foo> tmp = this.myService.requestFooService(request);
return ResponseEntity.status(tmp.getStatusCode()).body(tmp.getBody());
}
Now through Postman I do have the expected body. However, I don't understand the behaviour. I thought that maybe it's due to the fact that the body is some kind of stream that can be read once or something similar. But from reading the source code I don't see anything that could explain this behaviour.
I'm using the Netflix-stack (so HTTP calls between the two WS are made through a Feign client).
Any idea why I'm getting this result?
EDIT:
More details on my stask:
SpringBoot 1.5.3.RELEASE
Feign 2.0.5
There is a bug that causes the named body of an HTTP MultiPart POST to fail. The symptom of this is that you make a POST request with a body, and Spring-Boot can't match it up to an endoint. The exception I see is:
2019-01-23 15:22:45.046 DEBUG 1639 --- [io-8080-exec-10] .w.s.m.m.a.ServletInvocableHandlerMethod : Failed to resolve argument 3 of type 'org.springframework.web.multipart.MultipartFile'
org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present
Zuul is doing caching of the request in order to re-try multiple times. In this process, it fails to preserve the named field for the binary body. You may find it working if you preface the request with zuul. So instead of http://myserver.com/myservice/endpoint use zuul in the path: http://myserver.com/zuul/myservice/endpoint
That will effectively avoid the saving of the request and the retry mechanism.
More details are available on this issue in Zuul's GitHub Bug List.
I have a spring mvc controller, which accepts a post request and it needs to redirect to a URL (GET request).
#RequestMapping(value = "/sredirect", method = RequestMethod.POST)
public String processForm(HttpServletRequest request) {
System.out.println("Ews redirect hit !!! ");
request.setAttribute(View.RESPONSE_STATUS_ATTRIBUTE,HttpStatus.MOVED_PERMANENTLY);
return "redirect:https://www.google.com/";
}
And the class is annotated with #RestController. I am always getting 405, method not allowed for the redirect url. (i.e google.com).
As per docs (https://www.rfc-editor.org/rfc/rfc7238), it should allow the method to be changed . I am not sure what am I doing wrong? Can someone help
It looks like Rest Controllers can't use the simple "redirect:" convention, like non-Rest Controllers can. See Spring MVC #RestController and redirect
Have you tried accepting GET in the request mapping?
method = { RequestMethod.POST, RequestMethod.GET }
Here I have written this code for a rest web service in Spring Controllor Class . After build the project I try to use this service using a Restful-Client
RestService Code :-
#RequestMapping(value="/someurl/{prm_passPhraseCode}/{prm_email}", method= RequestMethod.POST)
public #ResponseBody User sendResetLink(#PathVariable("prm_passPhraseCode") String prm_sPassPhrase, #PathVariable("prm_email") String prm_sEmail , HttpServletRequest prm_ObjRequest, HttpServletResponse prm_ObjResponse){
......
..... //some more logical Code.
return new User(); //just dummy object for reference.
}
Here how I tried to access the Url. I have selected the method type as post.
I have also added two headers
Content-Type : application/json
Accept : application/json
URL http://127.0.0.1:8080/webservice.staff.backend/someurl/23812397997713/kumarvikrant625#gmail.com
Although all my other Rest services urls either it is GET or POST are working fine.
I have also try by change the method = RequestMethod.GET but still get the same error.
Error :-
Status Code 406 : The resource identified by this request is only capable of
generating responses with characteristics not acceptable according to the request "accept" headers.
please help if any one have a Idea.
your Url isn't mapped correctly, you are missing sendResetPasswordLink
#RequestMapping(value="/someurl/sendResetPasswordLink/{prm_passPhraseCode}/{prm_email}", method= RequestMethod.POST)
Try this.. May be it can work...
#RequestMapping(value="/someurl/{prm_passPhraseCode}/{prm_email}",headers="Accept=*/*", method= RequestMethod.POST)