Ukończono 406 NOT_ACCEPTABLE - Testowanie WebLayer w SpringBoot

Dec 07 2020

Mam tę metodę w aplikacji 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);


} 

i ten test:

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

a oto wynik testu:

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 = []

i

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

Odpowiedzi

s7vr Dec 19 2020 at 04:59

Twój problem pojawia się dwojako.

Podstawowy problem polega na tym, że nie zarejestrowałeś odpowiedniego konwertera wiadomości, aby móc napisać odpowiedź JSON. Jest to ukryte z powodu błędu polegającego na tym, że serwer powinien zwracać 500 zamiast 406 z powodu błędnej konfiguracji, ponieważ kontroler określił adnotację produkuje, a klient zaakceptował nagłówek wszystkich.

Więcej szczegółów - https://github.com/spring-projects/spring-framework/issues/23287

Nie jest to już problem w najnowszej wersji, tylko w przypadku wersji Spring Boot 2.1.x.

2 x80486 Dec 08 2020 at 00:48

Nie musisz ustawiać Content-Typenagłówka dla GETżądań, ponieważ nic nie wysyłasz, tylko o coś prosisz. W Accepttym przypadku szukamy nagłówka.

Co więcej, MediaType.APPLICATION_JSON_VALUEnie pasuje MediaType.APPLICATION_JSON_UTF8_VALUE.

Poleciłbym refaktoryzację tego fragmentu kodu na:

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