2016-09-01 22 views
1

我有一個RestController並接受POST請求的功能HttpMediaTypeNotSupportedException上mockmvc後

@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」,但仍然不起作用。

回答

0

該例外說「媒體類型」又名「內容類型」不被接受。

嘗試將consumes =「application/json」添加到您的控制器功能中。

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

詳見https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html#consumes--

+0

我試了一下春天的文檔,但它給出了同樣的異常。 – gkatzioura

+0

也許你可以用更多的上下文來提供整個異常? –

相關問題