• Java-Class-C:org.springframework.http.converter.StringHttpMessageConverter.java


    ylbtech-Java-Class-C:org.springframework.http.converter.StringHttpMessageConverter.java
    1.返回顶部
    1.1、
    import org.springframework.http.converter.StringHttpMessageConverter;
    1.2、
    restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8)); // 防止中文乱码
    1.3、
    2.返回顶部
     
    3.返回顶部
     
    4.返回顶部
    1、
    //
    // Source code recreated from a .class file by IntelliJ IDEA
    // (powered by Fernflower decompiler)
    //
    
    package org.springframework.http.converter;
    
    import java.io.IOException;
    import java.nio.charset.Charset;
    import java.nio.charset.StandardCharsets;
    import java.util.ArrayList;
    import java.util.List;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.HttpInputMessage;
    import org.springframework.http.HttpOutputMessage;
    import org.springframework.http.MediaType;
    import org.springframework.lang.Nullable;
    import org.springframework.util.Assert;
    import org.springframework.util.StreamUtils;
    
    public class StringHttpMessageConverter extends AbstractHttpMessageConverter<String> {
        public static final Charset DEFAULT_CHARSET;
        @Nullable
        private volatile List<Charset> availableCharsets;
        private boolean writeAcceptCharset;
    
        public StringHttpMessageConverter() {
            this(DEFAULT_CHARSET);
        }
    
        public StringHttpMessageConverter(Charset defaultCharset) {
            super(defaultCharset, new MediaType[]{MediaType.TEXT_PLAIN, MediaType.ALL});
            this.writeAcceptCharset = true;
        }
    
        public void setWriteAcceptCharset(boolean writeAcceptCharset) {
            this.writeAcceptCharset = writeAcceptCharset;
        }
    
        public boolean supports(Class<?> clazz) {
            return String.class == clazz;
        }
    
        protected String readInternal(Class<? extends String> clazz, HttpInputMessage inputMessage) throws IOException {
            Charset charset = this.getContentTypeCharset(inputMessage.getHeaders().getContentType());
            return StreamUtils.copyToString(inputMessage.getBody(), charset);
        }
    
        protected Long getContentLength(String str, @Nullable MediaType contentType) {
            Charset charset = this.getContentTypeCharset(contentType);
            return (long)str.getBytes(charset).length;
        }
    
        protected void writeInternal(String str, HttpOutputMessage outputMessage) throws IOException {
            HttpHeaders headers = outputMessage.getHeaders();
            if (this.writeAcceptCharset && headers.get("Accept-Charset") == null) {
                headers.setAcceptCharset(this.getAcceptedCharsets());
            }
    
            Charset charset = this.getContentTypeCharset(headers.getContentType());
            StreamUtils.copy(str, charset, outputMessage.getBody());
        }
    
        protected List<Charset> getAcceptedCharsets() {
            List<Charset> charsets = this.availableCharsets;
            if (charsets == null) {
                charsets = new ArrayList(Charset.availableCharsets().values());
                this.availableCharsets = (List)charsets;
            }
    
            return (List)charsets;
        }
    
        private Charset getContentTypeCharset(@Nullable MediaType contentType) {
            if (contentType != null && contentType.getCharset() != null) {
                return contentType.getCharset();
            } else if (contentType != null && contentType.isCompatibleWith(MediaType.APPLICATION_JSON)) {
                return StandardCharsets.UTF_8;
            } else {
                Charset charset = this.getDefaultCharset();
                Assert.state(charset != null, "No default charset");
                return charset;
            }
        }
    
        static {
            DEFAULT_CHARSET = StandardCharsets.ISO_8859_1;
        }
    }
    2、
    5.返回顶部
     
     
    6.返回顶部
     
    warn 作者:ylbtech
    出处:http://ylbtech.cnblogs.com/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Spring Boot 2 (七):Spring Boot 如何解决项目启动时初始化资源
    Spring Boot 2 (八):Spring Boot 集成 Memcached
    Spring Boot 2 (五):Docker Compose + Spring Boot + Nginx + Mysql 实践
    Spring Boot 2 (六):使用 Docker 部署 Spring Boot 开源软件云收藏
    Spring Boot 2 (四):使用 Docker 部署 Spring Boot
    微信开发中,不同手机系统遇到的bug(不定时更新)
    gulp-sourcemaps的用法
    实现输入框高度随内容变化
    微信开发,浏览器缓存问题
    mac中nvm的安装和使用
  • 原文地址:https://www.cnblogs.com/storebook/p/11096488.html
Copyright © 2020-2023  润新知