5

I have this method in a Spring Boot v2.1.0.RELEASE app.

@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);


} 

and this test:

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

and this is the result of the test:

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

and

@JsonSerialize(as = IWildProject.class)
public interface IWildProject {
..
}
Nuñito Calzada
  • 4,394
  • 47
  • 174
  • 301
  • To me it looks like the key to this is HttpMediaTypeNotAcceptableException. It's also a bit strange that the REQUEST sets a content-type and a null body. What happens if you remove the 'accept' code line? – antonyh Dec 18 '20 at 12:28

2 Answers2

2

You don't need to set the Content-Type header for GET requests since you are not sending anything but asking for something. The Accept header is what are looking for in this case.

Moreover, MediaType.APPLICATION_JSON_VALUE won't match MediaType.APPLICATION_JSON_UTF8_VALUE.

I would recommend to refactor that piece of code into:

this.mockMvc.perform(get("/wildProject")
  .accept(MediaType.APPLICATION_JSON_VALUE))
  // .andDo(print())
  .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
  .andExpect(status().isOk());
x80486
  • 6,627
  • 5
  • 52
  • 111
  • 1
    Did you try with the correct Media Type? I put the one that you had in your test also by mistake in a "copy-and-pasta mode" O:) – x80486 Dec 08 '20 at 13:22
0

Your problem appears two fold.

The underlying problem you've not registered the right message converter to be able to write json response. This is hidden because of the bug where the server should be returning 500 instead of 406 for misconfiguration as controller has specified the produces annotation and client has accept header of all.

More details - https://github.com/spring-projects/spring-framework/issues/23287

This is no longer a issue in latest release only issue with 2.1.x spring boot version.

s7vr
  • 73,656
  • 11
  • 106
  • 127