SpringBoot读取配置文件信息显示报错
Posted 花椒、斯国一
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot读取配置文件信息显示报错相关的知识,希望对你有一定的参考价值。
一、描述错误
当我在读取自定义配置文件信息时,希望返回到前台(以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);
以上是关于SpringBoot读取配置文件信息显示报错的主要内容,如果未能解决你的问题,请参考以下文章
3springboot:springboot配置文件(配置文件YAML属性文件值注入<@Value@ConfigurationProperties@PropertySource,@Imp(代码片
springboot报错说 Failed to parse multipart servlet request; nested exception is java.io.IOException(代码片
错误记录Android Studio 配置 AspectJ 报错 ( all buildscript {} blocks must appear before any plugins {} )(代码片
springboot配置文件application-dev.properties,application-prod.properties,application-test.properties(代码片
解决报错提示:Loading class `com.mysql.jdbc.Driver‘.The new driver class is ‘com.mysql.cj.jdbc.Driver‘.(代码片