获取 JsonMappingException,同时将数据发送到视图

2022-09-01 14:59:45

我正在尝试向我的网页显示数据库数据。我已经做了以下代码,当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;
    }
}

答案 1

默认情况下,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 尝试序列化从被调用(这是一个常规的 Bean 属性名称)调用的字段。然而,getter方法似乎出于某种原因抛出了一个。validgetterisValidNullPointerException

如果您希望 Jackson 忽略它,则可以使用 getter 或您的类进行注释,并指定属性名称,即。.@JsonIgnore@JsonIgnorePropertiesvalid


答案 2

在我的情况下,当我使用异常已经消失,但问题是它无法再从中接收该值,Spring忽略了它(显然是因为),所以我调查了这个问题,并发现问题是和。我有财产,而我是。因此,当我更改为我的问题时,问题解决了,错误就消失了。@JsonIgnoreAPI Request@JsonIgnoregettersetterIntegergetterintgetterInteger

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