0
回答
springboot读取配置文件后的序列化问题
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

刚学习springboot的配置文件读取方式,可以通过

@ConfigurationProperties(prefix="jdbc")的方式从默认的application.properties文件中读取配置,我读取后直接通过restcontroller方式可以返回json

@RestController
@EnableConfigurationProperties({User.class})
public class FirstController {
	@Autowired //这个类是我从配置文件中读取的配置类
	private User user;
	@Autowired
	private JdbcBean jdbcBean;
	
	@RequestMapping("/{name}")
	public User myname(@PathVariable("name") String name){
		return user;
	}
	

然后我读取非默认文件后就无法直接转换成json返回 ,这是我的配置类

@Configuration
@PropertySource("classpath:test.properties")
@ConfigurationProperties(prefix="jdbc")
public class JdbcBean implements Serializable{
	private static final long serialVersionUID = 5562973759963193223L;
	
	private String url;
	private String name;
	private String password;
	
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	

}

返回代码

@RestController
@EnableConfigurationProperties({JdbcBean.class})
public class FirstController {

	@Autowired
	private JdbcBean jdbcBean;
	
	@RequestMapping("/jdbc")
	public JdbcBean jdbc(){
		return jdbcBean;
	}
	

错误信息

Could not write JSON: No serializer found for class org.springframework.context.expression.StandardBeanExpressionResolver
 and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS);

求解

举报
gitOpen_1
发帖于3个月前 0回/33阅
顶部