Spring 中 No serializer found for class问题

HTTP Status 500 - Could not write content: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain

原因:hibernate添加了一些字段(使变成Json时报错)

解决方案一: 字段上加注解 

@JsonIgnoreProperties(value={"hibernateLazyInitializer","handler","fieldHandler"})

解决方案二:自己配置(拷备过来用即可)

1. 新建一个CustomMapper 类

public class CustomMapper extends ObjectMapper {
    public CustomMapper() {
        this.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        // 设置 SerializationFeature.FAIL_ON_EMPTY_BEANS 为 false
        this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    }
}

2.在applicationContext-mvc.xml配置



    
        
            
                
                    application/json; charset=UTF-8
                    application/x-www-form-urlencoded; charset=UTF-8
                
            
            
            
                 <--自己类的全限定名-->
                
            
        
    

 

你可能感兴趣的:(java基础)