SpringBoot讀取配置文件信息顯示報錯
阿新 • • 發佈:2018-12-11
npr 開始 pen 存儲 prope 自定義 ria standard solver
一、描述錯誤
當我在讀取自定義配置文件信息時,希望返回到前臺(以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讀取配置文件信息顯示報錯