2.2.0.RELEASEस्प्रिंग बूट के नए रिलीज़ किए गए संस्करण में अपग्रेड करने के बाद मेरे कुछ परीक्षण विफल हो गए। ऐसा प्रतीत होता है कि MediaType.APPLICATION_JSON_UTF8हटा दिया गया है और अब नियंत्रक विधियों से डिफ़ॉल्ट सामग्री प्रकार के रूप में वापस नहीं किया गया है जो सामग्री प्रकार को स्पष्ट रूप से निर्दिष्ट नहीं करते हैं।
टेस्ट कोड की तरह
String content = mockMvc.perform(get("/some-api")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andReturn()
.getResponse()
.getContentAsString();
अचानक काम नहीं किया क्योंकि सामग्री प्रकार बेमेल थी जैसा कि नीचे दिखाया गया है
java.lang.AssertionError: Content type
Expected :application/json;charset=UTF-8
Actual :application/json
.andExpect(content().contentType(MediaType.APPLICATION_JSON))अब समस्या को हल करने के लिए कोड बदलना ।
लेकिन अब जब contentअपेक्षित क्रमबद्ध वस्तु की तुलना की जाती है तो वस्तु में कोई विशेष वर्ण होने पर भी एक मिसमैच होता है। ऐसा प्रतीत होता है कि .getContentAsString()विधि डिफ़ॉल्ट रूप से (किसी भी अधिक) UTF-8 वर्ण एन्कोडिंग का उपयोग नहीं करती है।
java.lang.AssertionError: Response content expected:<[{"description":"Er hörte leise Schritte hinter sich."}]> but was:<[{"description":"Er hörte leise Schritte hinter sich."}]>
Expected :[{"description":"Er hörte leise Schritte hinter sich."}]
Actual :[{"description":"Er hörte leise Schritte hinter sich."}]
मैं contentUTF-8 एन्कोडिंग में कैसे प्राप्त कर सकता हूं ?