springboot使用@ConfigurationProperties中文乱码

springboot使用@ConfigurationProperties中文乱码,第1张

springboot使用@ConfigurationProperties中文乱码

代码:

package com.qkjia.pojo;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Component
@ConfigurationProperties(prefix = "person")// 实现批量注入(set方法 在配置文件上面)
public class Person {
    private int id; //id
    private String name; //名称
    private List hobby; //爱好
    private String[] family; //家庭成员
    private Map map;
    private Pet pet; //宠物


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List getHobby() {
        return hobby;
    }

    public void setHobby(List hobby) {
        this.hobby = hobby;
    }

    public String[] getFamily() {
        return family;
    }

    public void setFamily(String[] family) {
        this.family = family;
    }

    public Map getMap() {
        return map;
    }

    public void setMap(Map map) {
        this.map = map;
    }

    public Pet getPet() {
        return pet;
    }

    public void setPet(Pet pet) {
        this.pet = pet;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", hobby=" + hobby +
                ", family=" + Arrays.toString(family) +
                ", map=" + map +
                ", pet=" + pet +
                '}';
    }
}
package com.qkjia.pojo;


public class Pet {

    // 类型
    private String type;
    // 名称
    private String name;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Pet{" +
                "type='" + type + ''' +
                ", name='" + name + ''' +
                '}';
    }
}

配置文件

server.port=8084
server.servlet.context-path=/springboot
# 中文乱码
server.servlet.encoding.force-response=true
server.servlet.encoding.force=true
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8
# 排除
#spring.devtools.restart.exclude=static/**,public/**
# 使用springboot 4.2之前配置顺序
spring.config.use-legacy-processing=true
# person对象
person.id=1
person.name=Jack
person.hobby=打篮球
person.family=father,mother
person.map.k1=v1
person.map.k2=v2
person.pet.type=dag
person.pet.name=??



测试

package com.qkjia.springboot_01_demo;

import com.qkjia.pojo.Person;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class) // 测试启动器,并加载Spring Boot测试注解
@SpringBootTest // 标记为Spring Boot单元测试类,并加载项目的ApplicationContext上下文环境
class Springboot01DemoApplicationTests {

    // 配置测试
    @Autowired
    private Person person;

    @Test
    void contextLoads() {
        System.out.println(person);
    }

}

咋回事:

好了 :


这个

中文乱码
server.servlet.encoding.force-response=true
server.servlet.encoding.force=true
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8


来了一遍

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5684917.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存