पूरा 406 NOT_ACCEPTABLE - स्प्रिंगबूट में वेबलेयर का परीक्षण

Dec 07 2020

मैं एक स्प्रिंग बूट v2.1.0 में इस विधि है। कृपया अनुप्रयोग।

@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 {
..
}

जवाब

s7vr Dec 19 2020 at 04:59

आपकी समस्या दो गुना दिखाई देती है।

अंतर्निहित समस्या आपने सही संदेश कनवर्टर को पंजीकृत नहीं किया है जो कि json प्रतिक्रिया लिखने में सक्षम हो। यह बग के कारण छिपा हुआ है जहां सर्वर को गलतफहमी के लिए 406 के बजाय 500 लौटाया जाना चाहिए क्योंकि नियंत्रक ने एनोटेशन का उत्पादन किया है और क्लाइंट ने सभी के हेडर स्वीकार किए हैं।

अधिक जानकारी - https://github.com/spring-projects/spring-framework/issues/23287

यह अब केवल 2.1.x स्प्रिंग बूट संस्करण के साथ नवीनतम रिलीज़ में कोई समस्या नहीं है।

2 x80486 Dec 08 2020 at 00:48

आपको अनुरोधों के Content-Typeलिए शीर्षलेख सेट करने की आवश्यकता नहीं है GETक्योंकि आप कुछ भी नहीं भेज रहे हैं लेकिन कुछ के लिए पूछ रहे हैं। 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());