使用空日期发送POST请求时,Java JAX-RS Restful服务中的空指针异常

时间:2018-03-23 11:32:40

标签: java rest jax-rs

我想创建一个java类来处理search / * path下的restful请求。这是我在web.xml文件中的servlet条目:

<servlet>
        <servlet-name>Search-Servlet</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>gr.histopath.platform.controllers.search</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Search-Servlet</servlet-name>
        <url-pattern>/search/*</url-pattern>
    </servlet-mapping>

我的测试Java类是:

package gr.histopath.platform.controllers.search;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import gr.histopath.platform.model.TransferObjects.Incident;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.criteria.CriteriaBuilder;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.List;

@Path("")
public class SearchController {

    private ObjectMapper mapper;
    private String jsonString;
    private List<Incident> incidentsList;
    protected EntityManager entityManager;
    private static EntityManagerFactory factory;
    private CriteriaBuilder criteriaBuilder;

    public SearchController() {

        this.mapper = new ObjectMapper();
        this.jsonString = null;
        this.incidentsList = new ArrayList<Incident>();

        if (factory == null) {
            factory = Persistence.createEntityManagerFactory("PersistenceUnit");
        }

        this.entityManager = factory.createEntityManager();
        this.criteriaBuilder = entityManager.getCriteriaBuilder();

    }

    @POST
    @Path("incidents")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String searchIncidents(Incident requestBody) {

        System.out.println("POST RECEIVED THE FOLLOWING: ");
        System.out.println(requestBody.toString());

        return "testing";
    }
}

我的POJO事件对象是:

package gr.histopath.platform.model.TransferObjects;

import com.fasterxml.jackson.annotation.JsonManagedReference;

import javax.persistence.*;
import java.sql.Date;

/**
 * Created by mixtou on 7/1/17.
 */
@Entity
public class Incident {
    private int id;
    protected int protocolNo;
    private Date date;
    private byte isPayed;
    private String yliko;
    private String makro;
    private String anoso;
    private String mikro;
    private String symperasma;
    private String klinikesPlirofories;
    private String histo;
    private String simpliromatikiEkthesi;
    private Patient patient;
    private Doctor doctor;
    private Clinic clinic;
    private SigningDoctor signingDoctor;


    @Id
    @Column(name = "id")
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Basic
    @Column(name = "protocolNo")
    public int getProtocolNo() {
        return protocolNo;
    }

    public void setProtocolNo(int protocolNo) {
        this.protocolNo = protocolNo;
    }

    @Basic
    @Column(name = "date")
    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    @Basic
    @Column(name = "isPayed")
    public byte getIsPayed() {
        return isPayed;
    }

    public void setIsPayed(byte isPayed) {
        this.isPayed = isPayed;
    }


    @Basic
    @Column(name = "yliko")
    public String getYliko() {
        return yliko;
    }

    public void setYliko(String yliko) {
        this.yliko = yliko;
    }

    @Basic
    @Column(name = "makro")
    public String getMakro() {
        return makro;
    }

    public void setMakro(String makro) {
        this.makro = makro;
    }

    @Basic
    @Column(name = "anoso")
    public String getAnoso() {
        return anoso;
    }

    public void setAnoso(String anoso) {
        this.anoso = anoso;
    }

    @Basic
    @Column(name = "mikro")
    public String getMikro() {
        return mikro;
    }

    public void setMikro(String mikro) {
        this.mikro = mikro;
    }

    @Basic
    @Column(name = "symperasma")
    public String getSymperasma() {
        return symperasma;
    }

    public void setSymperasma(String symperasma) {
        this.symperasma = symperasma;
    }

    @Basic
    @Column(name = "klinikesPlirofories")
    public String getKlinikesPlirofories() {
        return klinikesPlirofories;
    }

    public void setKlinikesPlirofories(String klinikesPlirofories) {
        this.klinikesPlirofories = klinikesPlirofories;
    }

    @Basic
    @Column(name = "histo")
    public String getHisto() {
        return histo;
    }

    public void setHisto(String histo) {
        this.histo = histo;
    }

    @Basic
    @Column(name = "simpliromatikiEkthesi")
    public String getSimpliromatikiEkthesi() {
        return simpliromatikiEkthesi;
    }

    public void setSimpliromatikiEkthesi(String simpliromatikiEkthesi) {
        this.simpliromatikiEkthesi = simpliromatikiEkthesi;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Incident incident = (Incident) o;

        if (id != incident.id) return false;
        if (protocolNo != incident.protocolNo) return false;
        if (isPayed != incident.isPayed) return false;
        if (date != null ? !date.equals(incident.date) : incident.date != null) return false;
        if (yliko != null ? !yliko.equals(incident.yliko) : incident.yliko != null) return false;
        if (makro != null ? !makro.equals(incident.makro) : incident.makro != null) return false;
        if (anoso != null ? !anoso.equals(incident.anoso) : incident.anoso != null) return false;
        if (mikro != null ? !mikro.equals(incident.mikro) : incident.mikro != null) return false;
        if (symperasma != null ? !symperasma.equals(incident.symperasma) : incident.symperasma != null) return false;
        if (klinikesPlirofories != null ? !klinikesPlirofories.equals(incident.klinikesPlirofories) : incident.klinikesPlirofories != null)
            return false;
        if (histo != null ? !histo.equals(incident.histo) : incident.histo != null) return false;
        if (simpliromatikiEkthesi != null ? !simpliromatikiEkthesi.equals(incident.simpliromatikiEkthesi) : incident.simpliromatikiEkthesi != null)
            return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = (int) (id ^ (id >>> 32));
        result = 31 * result + protocolNo;
        result = 31 * result + (date != null ? date.hashCode() : 0);
        result = 31 * result + (int) isPayed;
        result = 31 * result + (yliko != null ? yliko.hashCode() : 0);
        result = 31 * result + (makro != null ? makro.hashCode() : 0);
        result = 31 * result + (anoso != null ? anoso.hashCode() : 0);
        result = 31 * result + (mikro != null ? mikro.hashCode() : 0);
        result = 31 * result + (symperasma != null ? symperasma.hashCode() : 0);
        result = 31 * result + (klinikesPlirofories != null ? klinikesPlirofories.hashCode() : 0);
        result = 31 * result + (histo != null ? histo.hashCode() : 0);
        result = 31 * result + (simpliromatikiEkthesi != null ? simpliromatikiEkthesi.hashCode() : 0);
        return result;
    }

    @ManyToOne
    @JoinColumn(name = "patient_id", referencedColumnName = "id", nullable = false)
    @JsonManagedReference
    public Patient getPatient() {
        return patient;
    }

    public void setPatient(Patient patient) {
        this.patient = patient;
    }

    @ManyToOne
    @JoinColumn(name = "doctor_id", referencedColumnName = "id", nullable = false)
    @JsonManagedReference
    public Doctor getDoctor() {
        return doctor;
    }

    public void setDoctor(Doctor doctor) {
        this.doctor = doctor;
    }

    @ManyToOne
    @JsonManagedReference
    @JoinColumn(name = "clinic_id", referencedColumnName = "id")
    public Clinic getClinic() {
        return clinic;
    }

    public void setClinic(Clinic clinic) {
        this.clinic = clinic;
    }

    @ManyToOne
    @JsonManagedReference
    @JoinColumn(name = "signingDoctor_id", referencedColumnName = "id", nullable = false)
    public SigningDoctor getSigningDoctor() {
        return signingDoctor;
    }

    public void setSigningDoctor(SigningDoctor signingDoctor) {
        this.signingDoctor = signingDoctor;
    }
}

如果我使用POST向servlet发送一个完全空的JSON对象{}。 如果我发送没有日期字段的任何JSON对象,它的工作原理。 如果我发送一个空日期{...,“date”:“”,...}的JSON对象,我得到Java Null指针异常错误。 如果发送一个带有日期{...,“date”:“2000-10-10”的JSON对象,...}它可以工作。点符号{...}表示任何内容

任何想法为什么会发生这种情况以及如何解决? 我使用搜索表单,并使用Angular的 form.value 将整个表单作为对象发送,如果我没有在日期设置任何一个它发送{“date”:“”}这导致java后端的空指针异常错误

3 个答案:

答案 0 :(得分:1)

解决方案是@Sathishkumar Manogaran在他的第二条评论中发布的。我在事件POJO课程中将?date=更改为java.sql.Date,并且神奇地工作了。因此导致问题的是导入的库。

答案 1 :(得分:0)

您必须发送空值而不是空字符串。发送&#34;日期&#34;:null

答案 2 :(得分:0)

您可以在填充此特定POJO时允许Date为null值。这可以在var pricing = []; for (i = 0; i <15; i++) { var checkbox = "checkbox"+i; var priceValue = "priceValue"+i; var priceCurrency = "priceCurrency"+i; var priceAttribute = "priceAttribute"+i; if ($("#"+checkbox).is(':checked')) { for (j = 1; j <=2; j++) { var priceValue1 = priceValue+'_'+j; var priceCurrency1 = priceCurrency+'_'+j; var priceAttribute1 = priceAttribute+'_'+j; var value = $("#"+priceValue1).val(); var currency = $("#"+priceCurrency1).val(); var attribute = $("#"+priceAttribute1).val(); var item = []; item.push(value); item.push(currency); item.push(attribute); pricing.push(i); } } } 包中找到。

javax.persistance.*

您也可以对某些必填字段使用相同的@Column(name = "date", nullable = true) public Date getDate() { return date; }

注意:但这不是一个好习惯。