我正在尝试向我的网页显示数据库数据。
GET请求@RequestMapping(value = "/api/binder")
时,我已经制作了以下代码。
但是当get请求来到这个方法时,它将获取数据(我在控制台上打印并显示良好)但它没有映射到我的Java Script Ajax调用,它显示我错误。
以下是我获取数据的代码:
@Autowired
IBinderViewRepository repository;
@RequestMapping(method= RequestMethod.GET)
public @ResponseBody
List<BinderResponse> getBinders(){
List<BinderView> binders = repository.getBinders();
List<BinderResponse> responses = new ArrayList<>();
ModelMapper mapper = Mapper.getInstance();
for(int i = 0; i < binders.size(); i++){
System.out.println("In Loop");
BinderResponse response = mapper.map(binders.get(i),BinderResponse.class);
System.out.println("Data :: " + response.getBinderName());
responses.add(response);
}
return responses;
}
但它显示以下错误:
HTTP Status 500 - Could not write JSON: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[0]->com.ngl.dto.outgoing.BinderResponse["valid"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[0]->com.ngl.dto.outgoing.BinderResponse["valid"])
这是来自knockout js的ajax调用:
ajax.get('api/binder').done(function(response){ ... }
此处BinderView and BinderResponse
具有相同的字段:
private String binderName;
private String binderAddress1;
和getter setter以及两者。
和repository.genBinders()
方法从数据库中提取数据。
这是插入方法,对我来说很好用:
@RequestMapping(method= RequestMethod.POST,consumes = "application/json")
public @ResponseBody
IWebApiResponse addBinder(@RequestBody AddBinderForm binder){
.....
}
我必须放任何json annotation on my BinderResponse class ?
我不明白我错在哪里?任何请求指导我。
更新:
public class BinderResponse extends WebApiResponseBase {
private String binderName;
private String binderAddress1;
public String getBinderName() {
return binderName;
}
public void setBinderName(String binderName) {
this.binderName = binderName;
}
public String getBinderAddress1() {
return binderAddress1;
}
public void setBinderAddress1(String binderAddress1) {
this.binderAddress1 = binderAddress1;
}
}
BinderView:
public class BinderView extends BaseView {
private String binderName;
private String binderAddress1;
public String getBinderName() {
return binderName;
}
public void setBinderName(String binderName) {
this.binderName = binderName;
}
public String getBinderAddress1() {
return binderAddress1;
}
public void setBinderAddress1(String binderAddress1) {
this.binderAddress1 = binderAddress1;
}
}
在控制台中,它打印数据/ BinderName:
In Loop
Data :: ada
In Loop
Data :: tya
新更新:
这是 BaseView :
@MappedSuperclass
public abstract class BaseView implements IEntity {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name="id")
private long id;
public long getId() {
return id;
}
public void setId(long id) {
if (this.id != 0 && this.id != id) {
throw new IllegalStateException(
"The ID must not be changed after it is set.");
}
this.id = id;
}
}
和 IEntity :
public interface IEntity extends Serializable {
long getId();
void setId(long id);
}
WebApiResponseBase :
public class WebApiResponseBase implements IWebApiResponse {
private String _uri;
@Override
public String getUri() {
return _uri == null ? "" : _uri;
}
@Override
public void setUri(String uri) {
_uri = uri;
}
}
答案 0 :(得分:34)
public class BinderResponse extends WebApiResponseBase {
好像是
Could not write JSON: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[0]->com.ngl.dto.outgoing.BinderResponse["valid"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[0]->com.ngl.dto.outgoing.BinderResponse["valid"])
杰克逊试图从名为valid
的{{1}}序列化一个名为getter
的字段(这是一个传统的bean属性名称)。然而,getter方法似乎因任何原因抛出isValid
。
如果您希望杰克逊忽略它,您可以使用NullPointerException
或您的班级@JsonIgnore
注释getter并指定属性名称,即。 @JsonIgnoreProperties
。
答案 1 :(得分:3)
在我的情况下,当我使用@JsonIgnore
时,异常消失了,但问题是它不再能够从API Request
接收到该值,而Spring忽略了它(显然是因为@JsonIgnore
)因此,我对该问题进行了调查,发现问题是getter
和setter
。
我的Integer
是getter
时,我拥有int
属性。因此,当我将getter
更改为Integer
时,我的问题解决了,错误消失了。
private Integer purchaseId;
@JsonIgnore
public int getPurchaseId() {
return purchaseId;
}
public void setPurchaseId(int purchaseId) {
this.purchaseId = purchaseId;
}
更改为:
private Integer purchaseId;
public Integer getPurchaseId() {
return purchaseId;
}
public void setPurchaseId(Integer purchaseId) {
this.purchaseId = purchaseId;
}
答案 2 :(得分:-1)
@Column(name="createddate")
private Date createdDate;
@Transient
private String formatedCreatedDate;
public String getFormatedCreatedDate() {
DateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy");
return dateFormat.format(this.getCreatedDate());
}
它抛出相同的异常,因为通过调用getCreatedDate()值可能为null,因此它不能格式化null日期,所以保持null检查如下:
解决方案
public String getFormatedCreatedDate() {
DateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy");
Date createDdate=this.getCreatedDate();
**if(createDdate!=null){
return dateFormat.format(createDdate);
}**
return "-";
}