mockmvc 上的 HttpMediaTypeNotSupportedException post
HttpMediaTypeNotSupportedException on mockmvc post
我有一个 RestController 和一个接受 post 请求的函数
@RequestMapping(path = "/auth",method = RequestMethod.POST)
public void authenticate(@RequestBody AuthenticationRequest authenticationRequest, HttpServletResponse httpServletResponse) throws IOException {
}
我尝试发出 post 请求
mockMvc.perform(post("/auth")
.contentType(MediaType.APPLICATION_JSON)
.content("{ \"foo\": \"bar\", \"fruit\": \"apple\" }".getBytes()))
.andDo(print());
我收到
Resolved Exception:
Type = org.springframework.web.HttpMediaTypeNotSupportedException
有什么解决办法吗?
编辑:我也尝试在控制器上指定 consumes="application/json",但仍然无效。
异常表明 "media type" 又名 "content type" 不被接受。
尝试将 consumes = "application/json" 添加到您的控制器函数中。
@RequestMapping(path = "/auth",method = RequestMethod.POST,consumes = "application/json")
public void authenticate(@RequestBody AuthenticationRequest authenticationRequest, HttpServletResponse httpServletResponse) throws IOException {
}
有关详细信息,请参阅 spring 文档 https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html#consumes--
我有一个 RestController 和一个接受 post 请求的函数
@RequestMapping(path = "/auth",method = RequestMethod.POST)
public void authenticate(@RequestBody AuthenticationRequest authenticationRequest, HttpServletResponse httpServletResponse) throws IOException {
}
我尝试发出 post 请求
mockMvc.perform(post("/auth")
.contentType(MediaType.APPLICATION_JSON)
.content("{ \"foo\": \"bar\", \"fruit\": \"apple\" }".getBytes()))
.andDo(print());
我收到
Resolved Exception:
Type = org.springframework.web.HttpMediaTypeNotSupportedException
有什么解决办法吗?
编辑:我也尝试在控制器上指定 consumes="application/json",但仍然无效。
异常表明 "media type" 又名 "content type" 不被接受。
尝试将 consumes = "application/json" 添加到您的控制器函数中。
@RequestMapping(path = "/auth",method = RequestMethod.POST,consumes = "application/json")
public void authenticate(@RequestBody AuthenticationRequest authenticationRequest, HttpServletResponse httpServletResponse) throws IOException {
}
有关详细信息,请参阅 spring 文档 https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html#consumes--