gpt4 book ai didi

java - 发送数据查看时获取 JsonMappingException

转载 作者:搜寻专家 更新时间:2023-10-30 20:55:19 27 4
gpt4 key购买 nike

我正在尝试向我的网页显示数据库数据。当 GET 请求到 @RequestMapping(value = "/api/binder") 时,我做了以下代码。

但是当获取请求到达此方法时,它会获取数据(我在控制台上打印并显示良好)但它没有映射到我的 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(函数(响应){ ... }

此处 BinderView 和 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){
.....
}

我是否必须在我的 BinderResponse 类上放置任何 json 注释?

我不明白我哪里错了?任何人都请指导我。

UPDATE :

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;
}
}

Binder View :

    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

New Update :

这是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;
}
}

最佳答案

默认情况下,Jackson 序列化对象的整个继承层次结构,即。父类字段也是如此。在这种情况下

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"])

Jackson 尝试从名为 isValid(这是一个常规的 bean 属性名称)的 getter 序列化一个名为 valid 的字段。但是,无论出于何种原因,getter 方法似乎都会抛出 NullPointerException

如果您希望 Jackson 忽略它,您可以使用 @JsonIgnore 注释 getter 或使用 @JsonIgnoreProperties 注释您的类并指定属性名称,即。 有效

关于java - 发送数据查看时获取 JsonMappingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22853509/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com