Spring @即使Accept定义为null,也会调用GetMapping
我正在使用Spring框架Spring @即使Accept定义为null,也会调用GetMapping,spring,spring-mvc,spring-rest,Spring,Spring Mvc,Spring Rest,我正在使用Spring框架4.3.13.RELEASE 关于Rest,对于POST,我有以下内容: @Controller @RequestMapping(path="/personas") public class PersonaRestController { @PostMapping(consumes={MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_UTF8_VALUE}) public Respo
4.3.13.RELEASE
关于Rest,对于POST
,我有以下内容:
@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, ...
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 = []
@GetMapping(path="/{id}",
produces={MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_UTF8_VALUE})
public @ResponseBody Persona findOneById(@PathVariable String id){
return personaService.findOneById(id);
}
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)
因为saveOne
方法要求内容数据为XML
或JSON
,所以可以感觉到这种行为,并且对于此测试场景,content Type
未被发送
因此,预计Spring会出现一个HttpMediaTypeNotSupportedException
实例。然后,@Test
方法失败,出现java.lang.AssertionError:No handler
错误消息,因为没有处理程序来处理用户请求
直到这里一切都好
同样,关于Rest,对于GET
,我有以下内容:
@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, ...
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 = []
@GetMapping(path="/{id}",
produces={MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_UTF8_VALUE})
public @ResponseBody Persona findOneById(@PathVariable String id){
return personaService.findOneById(id);
}
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)
对于其他测试场景,请执行以下操作:
@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, ...
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 = []
@GetMapping(path="/{id}",
produces={MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_UTF8_VALUE})
public @ResponseBody Persona findOneById(@PathVariable String id){
return personaService.findOneById(id);
}
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)
我的拦截器显示以下数据
HttpHeaderControlInterceptor - preHandle
HttpServletRequest >
URL: http://localhost/personas/100
URI: /personas/100
HttpMethod: GET
Accept: null
Accept-Language: null
Content-Type: null
我很困惑为什么findOneById
方法在Accept
为空时如何调用处理程序。我期望类似于java.lang.AssertionError的东西:没有处理程序
错误消息,而是类,因为findOneById
方法可以返回XML
或JSON
格式的数据
缺少什么?我不太确定,但我猜这是由HTTP 1.1引起的 参见第10.4.7 406节-不可接受 注意:允许HTTP/1.1服务器返回以下响应: 根据中发送的accept标头,不可接受 要求在某些情况下,这甚至可能比发送 406答复。鼓励用户代理检查 确定其是否可接受的传入响应 我认为,
消费
与生产
的处理方式是不同的,这就是为什么你会看到差异
请参阅此问题
我之前已经发布了一个答案,我已经删除了。基本上,我误解了你的问题