Spring MVC Request Mapping passes requests that should not be passed

**EDIT** - This is now solved, the error was in my spring configuration. I had missed the `` tag. When adding it, everything worked as expected. I have defined a controller using Spring MVC that should accept`HTTP GET` requests to the address `/controllertest` only if the content type of the request is `application/json`. It also returns data in `application/json`and therefore only requests that accept data in that format should pass. However, this does not work. This is my code using Spring 3.2.0: package test.controllers; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; import static org.springframework.web.bind.annotation.RequestMethod.GET; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/controllertest") public class TestController { @RequestMapping(method = GET, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE) @ResponseBody public String helloJSON() { return "{\"message\":\"HelloWorld\"}"; } } This is what I expect: 1. If I use any HTTP method other than `GET` I should receive a `405 - Method not allowed` response 2. If I send a request that do not accept `application/json` I should receive a `406 Not Acceptable` response. 3. If I send a request with a Content-Type other than `application/json` I should receive a `415 Unsupported Media Type` response. This is the actual result: 1. I do actually get a `405 - Method not allowed`. Very nice. 2. When sending a request with an `Accept` header with `text/xml` only I get a `200 - OK` response and the content type of the response is `text/xml` even though the method only produces `application/json`. 3. When sending a request where the content type is `text/xml` I get a `200 - OK` response. I may add that I implemented all of this in Jersey and the results were all correct.

以上就是Spring MVC Request Mapping passes requests that should not be passed的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏