springboot static下页面中文乱码

误以为:

package com.program.face.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;

/**
 * @Author: hao
 * @Description: 编码配置
 * @Date: 2020/3/31 20:47
 * @Version: 1.0
 */

// implements WebMvcConfigurer
// extends WebMvcConfigurationSupport
@Configuration
public class CharsetConfig extends WebMvcConfigurationSupport {

    // 1、解决:中文乱码
    @Bean
    public HttpMessageConverter<String> responseBodyConverter() {
        StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
        stringHttpMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON_UTF8));
        return stringHttpMessageConverter;
    }

    // 2.1、解决:返回json时WARN:No converter found for return value of type: 或:Could not find acceptable representation
    public ObjectMapper getObjectMapper() {
        return new ObjectMapper();
    }

    // 2.2、解决:返回json时WARN:No converter found for return value of type:
    public MappingJackson2HttpMessageConverter messageConverter() {
        MappingJackson2HttpMessageConverter converter=new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(getObjectMapper());
        return converter;
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        //解决中文乱码
        // 解决:返回json时WARN:No converter found for return value of type: 或:Could not find acceptable representation
        converters.add(messageConverter());
        converters.add(responseBodyConverter());
    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false);
    }

}

结果:o.s.web.servlet.PageNotFound : No mapping for GET

最终解决方式(我也是醉了,网上很坑啊,本来很简单的):

html的head里加上:

<meta charset="utf-8">

就行!

发布了388 篇原创文章 · 获赞 105 · 访问量 31万+

猜你喜欢

转载自blog.csdn.net/haoranhaoshi/article/details/105256350