SpringBoot读取配置文件信息显示报错

本文描述了在Spring Boot应用中,从自定义配置文件读取信息并尝试将其序列化为JSON格式时遇到的问题及解决方案。主要错误是由于序列化过程中未能找到合适的序列化器导致的InvalidDefinitionException异常。通过重新实例化实体类对象并手动设置属性值,成功解决了该问题。

一、描述错误

当我在读取自定义配置文件信息时,希望返回到前台(以json的格式),但是报错。

错误信息大致如下(未完全粘贴):

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.springframework.context.expression.StandardBeanExpressionResolver

 

二、大致情况

就是后台返回的数据不能正确被序列化

 

三、看代码

resource.properties

######################################
##########   存储配置相关的信息         ########## 
######################################
com.xf.name=陈独秀
com.xf.age=21
com.xf.gender=男
com.xf.school=家里蹲大学
com.xf.address=里根

映射实体类:

package com.xf.database.entity;


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
/*
 * 读取自定义配置文件信息
 * 当在自定义配置文件存储中文时,读取文件信息的
 * @PropertySource(value="classpath:config/resource.properties",
encoding="UTF-8")
 *  需要指定编码encoding="UTF-8" 才不会显示乱码
 * 
 */
@Configuration
@ConfigurationProperties(prefix="com.xf") //指定前缀
@PropertySource(value="classpath:config/resource.properties",
encoding="UTF-8")
public class CResource{
    /**
     * 
     */
    private String name; // 姓名
    private Integer age; // 年龄
    private String school; // 学校
    private String address; // 地址
    private String gender; // 性别
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getSchool() {
        return school;
    }
    public void setSchool(String school) {
        this.school = school;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    @Override
    public String toString() {
        return "Resource [name=" + name + ", age=" + age + ", school=" + school + ", address=" + address + ", gender="
                + gender + "]";
    }
    
}
实体类

读取配置文件:

package com.xf.controllers;


import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xf.database.entity.CResource;

@RestController
public class ReadConfigurationController {
    @Resource
    private CResource resource;
    
    @RequestMapping("/getresource")
    public String getResource() throws JsonProcessingException{
        ObjectMapper mapper = new ObjectMapper();
//        CResource c = new CResource();
//        c.setName(resource.getName());
//        c.setAge(resource.getAge());;
//        c.setAddress(resource.getAddress());
//        c.setSchool(resource.getSchool());
//        c.setGender(resource.getGender());
//        return mapper.writeValueAsString(c);
        return mapper.writeValueAsString(resource);
    }
}

若取消掉注释部分,,,那么就会最开始的那个报错。。

四、解决办法

重新实例化一个对象实例,并且把获取的值重新赋值,并以json的格式返回

ObjectMapper mapper = new ObjectMapper();
CResource c = new CResource();
c.setName(resource.getName());
c.setAge(resource.getAge());;
c.setAddress(resource.getAddress());
c.setSchool(resource.getSchool());
c.setGender(resource.getGender());
return mapper.writeValueAsString(c);

 

转载于:https://www.cnblogs.com/crazy-xf/p/10101090.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值