报错:


1 HTTP Status 500 - Could not write content: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered
2 to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.agen.entity.Product_$$_jvst2a_3["handler"]);
3 nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no
4 properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.agen.entity.Product_$$_jvst2a_3["handler"])
View Code
出现这个问题,是因为:


1 @ResponseBody
2 @RequestMapping("/Updateproduct")
3 @Transactional
4 public Product updateProduct2(Product product){
5 Product product1 = productService.load(product.getProductId());
6 if(product1 != null){
7 product1.setProductName(product.getProductName());
8 product1.setProductCre(product.getProductCre());
9 }
10
11 return product1;
12 }
View Code
这个方法中使用的load()获取到数据库中的这一条数据。
使用load()时,进入BUG模式可以看到,虽然获取到这条数据,但是你要看,却发现展示出来的这个对象的字段都是null。

但是其中是有值的!!

这样返回给前台,前台接收不到值,会跑出异常:

修改:
于是我们应该将load()方法修改为get()方法


1 /**
2 * 进行产品修改操作
3 * @return
4 */
5 @ResponseBody
6 @RequestMapping("/Updateproduct")
7 @Transactional
8 public Product updateProduct2(Product product){
9 Product product1 = productService.get(product.getProductId());
10 if(product1 != null){
11 product1.setProductName(product.getProductName());
12 product1.setProductCre(product.getProductCre());
13 }
14
15 return product1;
16 }
View Code
这样就能解决这个问题!!