Forepick
Forepick

Reputation: 937

Spring with Tomcat 7: PUT request returns 403 for strange resons

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:

  1. Sending the request to a non-implemented URL (on GET to the same URL I get a 404)
  2. Sending an invalid JSON as the request body (Expected 400)
  3. 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

Upvotes: 0

Views: 299

Answers (2)

Conor Costello
Conor Costello

Reputation: 71

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.

Upvotes: 0

Onur Baştürk
Onur Baştürk

Reputation: 735

  1. Check out CORS filter configuration first as Andreas said: https://tomcat.apache.org/tomcat-8.5-doc/config/filter.html
  2. Check out this flowchart also https://tomcat.apache.org/tomcat-8.5-doc/images/cors-flowchart.png
  3. Check out this stackoverflow post finally 403 on JSON PUT request to Tomcat with Spring 3.0.5 and Jackson

Upvotes: 0

Related Questions