SpringMVC's @ResponseBody appears garbled solution

Use @ResponseBody garbled solution

1. RequestMapping
Java code Collection code
    @Controller 
    @RequestMapping(value = "/test") 
    public class TestController { 
     
        @ResponseBody 
        @RequestMapping(value = "/test", method = {RequestMethod.GET}) 
        public String test() { 
            return "Chinese"; 
        } 
    } 

2. The reason is that the default encoding of the org.springframework.http.converter.StringHttpMessageConverter class is IOS-8859-1

3. Solution

   3.1. Define an implementation of HttpMessageConverter<String> by yourself class can solve the problem.
Java code Collection code

    public class UTF8StringHttpMessageConverter extends AbstractHttpMessageConverter<String> { 
     
        public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); 
     
        private final List<Charset> availableCharsets; 
     
        private boolean writeAcceptCharset = true; 
     
        public UTF8StringHttpMessageConverter() { 
            super(new MediaType("text", "plain", DEFAULT_CHARSET), MediaType.ALL); 
            this.availableCharsets = new ArrayList<Charset>(Charset.availableCharsets().values()); 
        } 
     
        public void setWriteAcceptCharset(boolean writeAcceptCharset) { 
            this.writeAcceptCharset = writeAcceptCharset; 
        } 
     
        @Override 
        public boolean supports(Class<?> clazz) { 
            return String.class.equals(clazz); 
        } 
     
        @Override 
        protected String readInternal(@SuppressWarnings("rawtypes") Class clazz, HttpInputMessage inputMessage) throws IOException { 
            Charset charset = getContentTypeCharset(inputMessage.getHeaders().getContentType()); 
            return FileCopyUtils.copyToString(new InputStreamReader(inputMessage.getBody(), charset)); 
        } 
     
        @Override 
        protected Long getContentLength(String s, MediaType contentType) { 
            Charset charset = getContentTypeCharset(contentType); 
            try { 
                return (long) s.getBytes(charset.name()).length; 
            } 
            catch (UnsupportedEncodingException ex) { 
                throw new InternalError(ex.getMessage()); 
            } 
        } 
     
        @Override 
        protected void writeInternal(String s, HttpOutputMessage outputMessage) throws IOException { 
            if (writeAcceptCharset) { 
                outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets()); 
            } 
            Charset charset = getContentTypeCharset(outputMessage.getHeaders().getContentType()); 
            FileCopyUtils.copy(s, new OutputStreamWriter(outputMessage.getBody(), charset)); 
        } 
     
        protected List<Charset> getAcceptedCharsets() { 
            return this.availableCharsets; 
        } 
     
        private Charset getContentTypeCharset(MediaType contentType) { 
            if (contentType != null && contentType.getCharSet() != null) { 
                return contentType.getCharSet(); 
            } 
            else { 
                return DEFAULT_CHARSET; 
            } 
        } 
    } 


Java代码  收藏代码

    <mvc:annotation-driven conversion-service="conversion-service" validator="validator"> 
        <mvc:message-converters register-defaults="false"> 
            <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" /> 
            <bean class="org.springframework.http.converter.FormHttpMessageConverter" /> 
            <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" /> 
            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> 
            <bean class="my.pkg.UTF8StringHttpMessageConverter" /> 
        </mvc:message-converters> 
    </mvc:annotation-driven> 

3.2、设置StringHttpMessageConverter编码
Java代码  收藏代码

    <mvc:annotation-driven conversion-service="conversion-service"validator="validator"> 
            <mvc:message-converters> 
                <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" /> 
                <bean class="org.springframework.http.converter.FormHttpMessageConverter" /> 
                <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" /> 
                <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> 
                <bean class="org.springframework.http.converter.StringHttpMessageConverter"> 
                    <constructor-arg value="UTF-8" /> 
                </bean> 
            </mvc:message-converters> 
        </mvc:annotation-driven> 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326570286&siteId=291194637