一, 描述错误
当我在读取自定义配置文件信息时, 希望返回到前台 (以 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);
来源: http://www.bubuko.com/infodetail-2880318.html