mockmvc帖子上的HttpMediaTypeNotSupportedException

时间:2016-09-01 14:45:59

标签: unit-testing spring-boot mime-types mockmvc

我有一个RestController和一个接受帖子请求的函数

any

我尝试发布帖子请求

@RequestMapping(path = "/auth",method = RequestMethod.POST)
public void authenticate(@RequestBody AuthenticationRequest authenticationRequest, HttpServletResponse httpServletResponse) throws  IOException {
}

我收到了

mockMvc.perform(post("/auth")
                    .contentType(MediaType.APPLICATION_JSON)
                    .content("{ \"foo\": \"bar\", \"fruit\": \"apple\" }".getBytes()))
            .andDo(print());

任何解决方法的想法?

编辑:我也试过指定consumes =" application / json"在控制器上,但仍然无法正常工作。

1 个答案:

答案 0 :(得分:1)

Exception表示不接受“媒体类型”又称“内容类型”。

尝试将 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--