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

use*_*3 ツ 14 java ajax json spring-mvc jackson

我正在尝试向我的网页显示数据库数据.当我向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;
    }
Run Code Online (Sandbox Code Playgroud)

但它显示我跟随错误:

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"])
Run Code Online (Sandbox Code Playgroud)

这是来自knockout js的ajax调用:

ajax.get('api/binder').done(function(response){ ... }

这里BinderView and BinderResponse有相同的字段:

    private String binderName;
    private String binderAddress1;
Run Code Online (Sandbox Code Playgroud)

两者都有吸气剂和吸气剂.和repository.genBinders()方法从DB中提取数据.

这是插入方法,对我来说很好:

    @RequestMapping(method= RequestMethod.POST,consumes = "application/json")
    public @ResponseBody
    IWebApiResponse addBinder(@RequestBody AddBinderForm binder){
        .....
    }
Run Code Online (Sandbox Code Playgroud)

要我放任何东西 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

}
Run Code Online (Sandbox Code Playgroud)

在控制台中,它打印数据/ BinderName:

In Loop
Data :: ada
In Loop
Data :: tya
Run Code Online (Sandbox Code Playgroud)

新更新:

这是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;
    }
}
Run Code Online (Sandbox Code Playgroud)

IEntity中:

public interface IEntity  extends Serializable {
    long getId();
    void setId(long id);
}
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Sot*_*lis 41

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

public class BinderResponse extends WebApiResponseBase {
Run Code Online (Sandbox Code Playgroud)

这好像是

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"])
Run Code Online (Sandbox Code Playgroud)

杰克逊尝试序列被称为场valid从一个getter称为isValid(这是一个常规的bean属性名称).然而,getter方法似乎NullPointerException因任何原因而抛出.

如果你想让杰克逊忽略它,你可以使用@JsonIgnore或者你的类注释getter @JsonIgnoreProperties并指定属性名称,即.valid.


Far*_*Zad 7

就我而言,当我使用@JsonIgnore该异常时,异常消失了,但问题是它再也无法从中接收该值API Request了,而Spring忽略了它(显然是因为@JsonIgnore),所以我对此问题进行了调查,并找出了问题是getterand setter。我有Integer产权,而我getterint。因此,当我将其更改getterInteger我的问题已解决且错误消失时。

private Integer purchaseId;

@JsonIgnore
public int getPurchaseId() {
    return purchaseId;
}

public void setPurchaseId(int purchaseId) {
    this.purchaseId = purchaseId;
}
Run Code Online (Sandbox Code Playgroud)

变成 :

private Integer purchaseId;


public Integer getPurchaseId() {
    return purchaseId;
}

public void setPurchaseId(Integer purchaseId) {
    this.purchaseId = purchaseId;
}
Run Code Online (Sandbox Code Playgroud)