我正在使用Spring Framework 4.3.13.RELEASE
关于休息,对于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, ...
关于使用JUnit
和ResultActions
进行测试我可以得到java.lang.AssertionError: No handler
错误消息并确认以下内容:
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 = []
感觉这种行为是因为saveOne
方法期望内容数据是XML
或JSON
,并且对于这个测试场景,没有发送Content-Type
。
因此,预计春天会出现一个HttpMediaTypeNotSupportedException
实例。然后@Test
方法失败并出现java.lang.AssertionError: No handler
错误消息,因为没有处理用户请求的处理程序。
直到这里一切都好。
再次,关于休息,对于GET
,我有以下内容:
@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
为null。我曾经期望类似于java.lang.AssertionError: No handler
错误消息和HttpMediaTypeNotAcceptableException类,它因为findOneById
方法可以返回XML
或JSON
中的数据
缺什么?