@GetMapping is called even when Accept is defined how null

匿名 (未验证) 提交于 2019-12-03 02:35:01

问题:

I am working with Spring Framework 4.3.13.RELEASE

About Rest, for POST, I have the following:

@Controller @RequestMapping(path="/personas") public class PersonaRestController {      @PostMapping(consumes={MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_UTF8_VALUE})     public ResponseEntity<Void> saveOne(             @Validated @RequestBody Persona persona, ... 

About Testing working with JUnit and with ResultActions I can get the java.lang.AssertionError: No handler error message and confirm the following:

MockHttpServletRequest:       HTTP Method = POST       Request URI = /personas        Parameters = {}           Headers = {}  Handler:              Type = null  Async:     Async started = false      Async result = null  Resolved Exception:              Type = org.springframework.web.HttpMediaTypeNotSupportedException  ModelAndView:         View name = null              View = null             Model = null  FlashMap:        Attributes = null  MockHttpServletResponse:            Status = 415     Error message = null           Headers = {Accept=[application/xml, application/json;charset=UTF-8]}      Content type = null              Body =      Forwarded URL = null    Redirected URL = null           Cookies = [] 

Has sense this behaviour because the saveOne method expects the content data be either XML or JSON and for this test scenario the Content-Type was not sent.

Therefore is expected that Spring arises a HttpMediaTypeNotSupportedException instance. Then the @Test method fails and the java.lang.AssertionError: No handler error message appears because there is no a handler to handle the user request.

Until here all OK.

Again, about Rest, for GET, I have the following:

@GetMapping(path="/{id}",             produces={MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_UTF8_VALUE}) public @ResponseBody Persona findOneById(@PathVariable String id){     return personaService.findOneById(id); } 

For other Testing scenario with the following:

MockHttpServletRequest:       HTTP Method = GET       Request URI = /personas/100        Parameters = {}           Headers = {}  Handler:              Type = com.manuel.jordan.rest.persona.PersonaRestController            Method = public com.manuel.jordan.domain.Persona com.manuel.jordan.rest.persona.PersonaRestController.findOneById(java.lang.String) 

My interceptor shows the following data

HttpHeaderControlInterceptor - preHandle     HttpServletRequest >      URL: http://localhost/personas/100      URI: /personas/100         HttpMethod: GET            Accept: null            Accept-Language: null            Content-Type: null  

I am confused why the findOneById method was called how a handler even when Accept was null. I had expected something similar than the java.lang.AssertionError: No handler error message and the HttpMediaTypeNotAcceptableException class instead, it because the findOneById method can return the data in either XML or JSON

What is missing?

回答1:

I am not fully sure but guess that its caused by HTTP 1.1 ,

See Section - 10.4.7 406 Not Acceptable here

Note: HTTP/1.1 servers are allowed to return responses which are not acceptable according to the accept headers sent in the request. In some cases, this may even be preferable to sending a 406 response. User agents are encouraged to inspect the headers of an incoming response to determine if it is acceptable.

I think, treatment of consumes vs produces is different and that is why you see the difference.

Refer to this SO question here

I had posted an answer before that I have deleted. Basically, I had misunderstood your question.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!