获取 JsonMappingException,同时将数据发送到视图
我正在尝试向我的网页显示数据库数据。我已经做了以下代码,当GET请求到.@RequestMapping(value = "/api/binder")
但是当get请求来到此方法时,它将获取数据(我在控制台上打印并显示良好),但它没有映射到我的Java脚本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"])
这是来自淘汰赛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;
}
}
在控制台中,它打印数据/绑定器名称:
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;
}
}