Java 向视图发送数据时获取JsonMappingException

Java 向视图发送数据时获取JsonMappingException,java,ajax,json,spring-mvc,jackson,Java,Ajax,Json,Spring Mvc,Jackson,我正在尝试向我的网页显示DB数据。 当GET请求到@RequestMapping(value=“/api/binder”)时,我生成了以下代码 但当get请求到达这个方法时,它将获取数据(我在控制台上打印并显示得很好),但它没有映射到我的Java脚本Ajax调用,它向我显示了一个错误 以下是我获取数据的代码: @Autowired IBinderViewRepository repository; @RequestMapping(method= RequestMetho

我正在尝试向我的网页显示DB数据。 当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;
    }
下面是来自knockout js的ajax调用:

ajax.get('api/binder').done(函数(响应){…}

此处
BinderView和BinderResponse具有相同的字段:

    private String binderName;
    private String binderAddress1;
和盖特·塞特在这两个方面都是如此。 和
repository.genBinders()
方法从数据库中获取数据

以下是insert方法,对我来说效果很好:

    @RequestMapping(method= RequestMethod.POST,consumes = "application/json")
    public @ResponseBody
    IWebApiResponse addBinder(@RequestBody AddBinderForm binder){
        .....
    }
我需要在BinderResponse类上添加任何
json注释吗?

我不明白我错在哪里?有人请引导我

更新:

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;
    }
}
public interface IEntity  extends Serializable {
    long getId();
    void setId(long id);
}
public class WebApiResponseBase implements IWebApiResponse {

    private String _uri;

    @Override
    public String getUri() {
        return _uri == null ? "" : _uri;
    }

    @Override
    public void setUri(String uri) {
        _uri = uri;
    }
}
而在
中:

@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;
    }
}
public interface IEntity  extends Serializable {
    long getId();
    void setId(long id);
}
public class WebApiResponseBase implements IWebApiResponse {

    private String _uri;

    @Override
    public String getUri() {
        return _uri == null ? "" : _uri;
    }

    @Override
    public void setUri(String uri) {
        _uri = uri;
    }
}
WebApiResponseBase

@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;
    }
}
public interface IEntity  extends Serializable {
    long getId();
    void setId(long id);
}
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
注释类,并指定属性名,即
有效

 @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()value come,此处可能为null,因此无法格式化null date,因此请在此处保持null检查,如:

解决方案

public String getFormatedCreatedDate() {
    DateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy");
    Date createDdate=this.getCreatedDate();
    **if(createDdate!=null){
        return  dateFormat.format(createDdate);
    }**
    return "-";
}

在我的例子中,当我使用
@JsonIgnore
时,异常已经消失,但问题是它无法再从
API请求
接收该值,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;
}

看看您的
响应包含什么内容。您似乎有一个名为
valid
BinderResponse
字段,即
null
。该字段是什么?您在
webapirresponsebase中有一个
NullPointerException
。是否有效
。您对此进行过调查吗?是的,除此之外,它还是有效的bean属性名,因此Jackson将尝试为其创建一个JSON字段。这似乎是导致NPE的原因。查看
@JsonIgnoreProperties
注释。我的变量定义为“Double”,getter和setter为“Double”。将变量从“Double”更改为“Double”对我来说很有效。