springboot乱码最新解决方案

您所在的位置:网站首页 springboot响应乱码 springboot乱码最新解决方案

springboot乱码最新解决方案

#springboot乱码最新解决方案| 来源: 网络整理| 查看: 265

原文链接:莫问博客-SpringBoot乱码问题解决

说明

   使用springboot开发理论上是不会出现乱码的,因为springboot默认编码为UTF-8,但是当客户端编码和服务器编码格式不一致时是会导致乱码的,所以这种情况首先需要和客户端约定请求编码格式,这里我们强制约定为UTF-8

一、修改springboot配置文件,以application.properties为例,增加如下配置

spring.http.encoding.force=true spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true server.tomcat.uri-encoding=UTF-8

二、当使用springboot模板进行页面开发时,模版解析乱码

       2,1、不推荐的做法是在controller的@RequestMapping中强行设置编码,如

                @RequestMapping(value="/test", produces="text/html;charset=UTF-8" )

       2.2、实现WebMvcConfigurer类来配置编码,2.0以下的springboot可以通过继承WebMvcConfigurerAdapter来修改,以WebMvcConfigurer为例,代码如下

package com.mybatis.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import java.nio.charset.Charset; import java.util.List; /** * @author liuxiaoding * @Date 2019/11/20 **/ @Configuration public class EncodeConfig implements WebMvcConfigurer { @Override public void extendMessageConverters(List


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3