解决com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simpl

在使用jackson 进行json字符串转java bean的时候出现如下错误,

com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class org.test.Library]: can not instantiate from JSON object (need to add/enable type information?)  at [Source: java.io.StringReader@d1831; line: 1, column: 2]
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:983)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:276)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)

原因java bean中没有提供无参的构造函数,如下

public class Library {  

    private String name;  
      
    private List<Book> bookList ;  
    //添加无参的构造器
    public Library(){ 
    }
	
    public Library(String name){
	this.name = name;  
    }
	
    public String getName() {  
        return name;  
    } 
	public void setName(String name) {  
        this.name = name;  
    }

	public List<Book> getBookList() {  
        return bookList;  
    }  
  
    public void setBookList(List<Book> bookList) {  
        this.bookList = bookList;  
    }
}
来源://作者:/更新时间:2014-05-16
相关文章
评论:
验证码:
匿名评论: