【问题标题】:Completed 406 NOT_ACCEPTABLE - Testing WebLayer in SpringBoot完成 406 NOT_ACCEPTABLE - 在 Spring Boot 中测试 Web 层
【发布时间】:2021-03-19 05:19:29
【问题描述】:

我在 Spring Boot v2.1.0.RELEASE 应用中有这个方法。

@GetMapping(value = "/wildProject", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<WildProject>> getList(HttpServletRequest request,
        HttpServletResponse response)
        throws Exception {

    List<WildProject> list = authorisationService.getList();

    System.out.println("-----------------");
    System.out.println(list);
    System.out.println("-----------------");

    return ok().body(list);


} 

还有这个测试:

 this.mockMvc.perform(get("/wildProject")
  //.accept(MediaType.APPLICATION_JSON_UTF8_VALUE))
  // .andDo(print())
  .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
  .andExpect(status().isOk());

这是测试的结果:

20:03:38.253 [main] DEBUG o.s.w.s.m.m.a.HttpEntityMethodProcessor - Using 'application/json', given [*/*] and supported [application/json]
20:03:38.255 [main] WARN  o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]
20:03:38.256 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Completed 406 NOT_ACCEPTABLE

MockHttpServletRequest:
      HTTP Method = GET
      Request URI = /wildProject
       Parameters = {}
          Headers = {Content-Type=[application/json;charset=UTF-8]}
             Body = null
    Session Attrs = {}

Handler:
             Type = com.bonansa.controller.AuthorisationController
           Method = public org.springframework.http.ResponseEntity<java.util.List<com.bonansa.WildProject>> com.bonansa.controller.AuthorisationController.getList() throws java.lang.Exception

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = org.springframework.web.HttpMediaTypeNotAcceptableException

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 406
    Error message = null
          Headers = {}
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

和

@JsonSerialize(as = IWildProject.class)
public interface IWildProject {
..
}

【问题讨论】:

  • 在我看来,关键是 HttpMediaTypeNotAcceptableException。 REQUEST 设置了内容类型和空正文也有点奇怪。如果删除“接受”代码行会发生什么?

标签: java json spring spring-boot rest


【解决方案1】:

您不需要为GET 请求设置Content-Type 标头,因为您不发送任何内容,而是请求某些内容。在这种情况下,Accept 标头就是要查找的内容。

此外,MediaType.APPLICATION_JSON_VALUE 不会匹配 MediaType.APPLICATION_JSON_UTF8_VALUE。

我建议将这段代码重构为:

this.mockMvc.perform(get("/wildProject")
  .accept(MediaType.APPLICATION_JSON_VALUE))
  // .andDo(print())
  .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
  .andExpect(status().isOk());

【讨论】:

  • 您是否尝试了正确的媒体类型?我也错误地将您在测试中的那个放在“复制和粘贴模式”中 O:)
【解决方案2】:

你的问题出现了两个方面。

您没有注册正确的消息转换器来编写 json 响应的根本问题。这是隐藏的,因为服务器应该返回 500 而不是 406 错误配置的错误,因为控制器已指定产生注释并且客户端已接受所有标头。

更多详情 - https://github.com/spring-projects/spring-framework/issues/23287

在 2.1.x spring boot 版本的最新版本中,这不再是问题。

【讨论】:

    猜你喜欢
    • 2021-03-26
    • 2018-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-25
    • 1970-01-01
    • 2020-08-20
    • 2018-02-25
    相关资源
    最近更新 更多