用springboot简单写一个返回Model的小例子,因为控制器上注解为@RestController,自动应该返回json格式,报错:
>Could not write JSON: No serializer found for class org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.cc.springbootDemo.schema.Project$$EnhancerBySpringCGLIB$$491fb76d["CGLIB$CALLBACK_0"])
代码如下:
bean:
@Configuration
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
public class Project implements java.io.Serializable{
private static final long serialVersionUID = 1L;
/*
* @Value注解加载属性值的时候可以支持两种表达式来进行配置
* 1.PlaceHolder方式,格式为${...},大括号内为PlaceHolder
* 2.SpEl表达式,格式为#{...},大括号内为SpEL表达式
*/
@Value("${project.name}")
private String name;
@Value("${project.author}")
private String author;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
Controller:
@RestController
@RequestMapping("/project")
public class ProjectController {
//==============以下是依赖注入的简单示例=======================================
@Autowired
private ProjectService projectService;
@RequestMapping("/handleBean")
public String handleBean() {
return projectService.findOneProject().getAuthor();
}
@RequestMapping("/handleBean2")
public Project handleBean2() {
Project pro1 = projectService.findOneProject();
return pro1;
}
public ProjectService getProjectService() {
return projectService;
}
public void setProjectService(ProjectService projectService) {
this.projectService = projectService;
}
}
最后发现通过原来是因为这个对象是通过代理产生的类,所以才报错:
划红线的部分都是bean没有的属性,在jackson2转化为json的时候发现,没有这个属性,所以不能产生成对应的json返回,如果这样写:
@RequestMapping("/handleBean2")
public Project handleBean2() {
Project pro1 = projectService.findOneProject();
//规避代理类不能转化为json的错误
Project pro = new Project();
pro.setAuthor(pro1.getAuthor());
pro.setName(pro1.getName());
return pro;
}
访问此请求http://localhost:8080/project/handleBean2(自己小例子,点击无用)显示:
多说一些:
1.在IE浏览器上你会发现它默认是文件格式,提示让你下载:
我们需要加上拦截让他显示成text/html格式,xml需要这样配置:
<mvc:annotation-driven validator="validator">
<mvc:message-converters>
<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
2.每一个对象都要从代理类转化为实体类,很是麻烦,但是发现如果不当做配置类注入@Value的话,它是没有这个错误的:
bean:
@Component//把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>)
public class Student implements java.io.Serializable{
private static final long serialVersionUID = 1L;
//名称
private String name;
//性别
private Integer sex;
//电话
private String telephone;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
}
Controller:
@RequestMapping("/handleBean3")
public Student handleBean3() {
return projectService.findOneStudent();
}
访问http://localhost:8080/project/handleBean3后,显示:
{“name”:”cc”,”sex”:1,”telephone”:”18800000000”}