将对象列表转换为Json字符串

时间:2018-09-21 07:19:11

标签: java json rest

我有一个Java RESTapi,我想在其中将自定义Pet对象的列表转换为Json,并将其显示在端点中。

这是我到目前为止所拥有的:

@Path("/allPets")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getPetsfromCollection() {
    List<Pet> petList = new ArrayList<>();
    petList.addAll(facade.returnAllPets());

    String json = gson.toJson(petList);

    //TODO return proper representation object
     return Response.ok().entity(json).build();
}

我使用的是Facade模式,这里有一种将Java实体添加到列表中的方法,例如:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("PetHospitaljpa");


public Collection<Pet> returnAllPets (){

    EntityManager  em = emf.createEntityManager();
    //vi laver en typed query for at specificere hvilken datatype, 
    // det er vi leder efter, i dette tilfælde er det en Pet
    TypedQuery<Pet> query = em.createNamedQuery("Pet.findAll", Pet.class);
    return query.getResultList();
}

如果要稍后将ArrayList的数据结构更改为其他内容,我将返回一个集合。

我尝试了几种解决方法,但是我不断收到堆栈溢出错误。

我知道一个事实,我需要使用DTO来代替,而且我已经制定了一种自定义方法来将实体更改为DTO,例如:

public static DTOPet converttoDTO(Pet entity){

   DTOPet dto = new DTOPet();
   dto.setId(entity.getId());
   dto.setName(entity.getName());
   dto.setBirth(entity.getBirth());
   dto.setDeath(entity.getDeath());
   dto.setSpecies(entity.getSpecies());

   return dto;
}

我不确定是否还有其他方法可以将一组实体转换为DTO,这是否是一种好的代码习惯?

如前所述。发生此问题是因为我有一个循环引用。

在我的宠物实体类中:

@ManyToOne
private Owner ownerId;

在我的所有者实体类中:

@OneToMany(mappedBy = "ownerId")
private Collection<Pet> petCollection;

我的宠物班:

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Entities;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author kristoffer
 */
@Entity
@Table(name = "pet")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Pet.findAll", query = "SELECT p FROM Pet p")
    , @NamedQuery(name = "Pet.findById", query = "SELECT p FROM Pet p WHERE p.id = :id")
    , @NamedQuery(name = "Pet.findByName", query = "SELECT p FROM Pet p WHERE p.name = :name")
    , @NamedQuery(name = "Pet.findByBirth", query = "SELECT p FROM Pet p WHERE p.birth = :birth")
    , @NamedQuery(name = "Pet.findBySpecies", query = "SELECT p FROM Pet p WHERE p.species = :species")
    , @NamedQuery(name = "Pet.findByDeath", query = "SELECT p FROM Pet p WHERE p.death = :death")})
public class Pet implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "name")
    private String name;
    @Basic(optional = false)
    @NotNull
    @Column(name = "birth")
    @Temporal(TemporalType.DATE)
    private Date birth;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "species")
    private String species;
    @Column(name = "death")
    @Temporal(TemporalType.DATE)
    private Date death;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "petId")
    private Collection<Event> eventCollection;
    @JoinColumn(name = "owner_id", referencedColumnName = "id")
    @ManyToOne
    private Owner ownerId;

    public Pet() {
    }

    public Pet(Integer id) {
        this.id = id;
    }

    public Pet(Integer id, String name, Date birth, String species) {
        this.id = id;
        this.name = name;
        this.birth = birth;
        this.species = species;
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public String getSpecies() {
        return species;
    }

    public void setSpecies(String species) {
        this.species = species;
    }

    public Date getDeath() {
        return death;
    }

    public void setDeath(Date death) {
        this.death = death;
    }

    @XmlTransient
    public Collection<Event> getEventCollection() {
        return eventCollection;
    }

    public void setEventCollection(Collection<Event> eventCollection) {
        this.eventCollection = eventCollection;
    }

    public Owner getOwnerId() {
        return ownerId;
    }

    public void setOwnerId(Owner ownerId) {
        this.ownerId = ownerId;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Pet)) {
            return false;
        }
        Pet other = (Pet) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Pet{" + "id=" + id + ", name=" + name + ", birth=" + birth + ", species=" + species + ", death=" + death + ", eventCollection=" + eventCollection + ", ownerId=" + ownerId + '}';
    }   
}

编辑: 我尝试创建一种方法,将所有对象都转换为DTO,但显示时字符串仍然为空:

 @Path("/allPets")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getPetsfromCollection() { 

    //med denne metode skal vi bruge et DTO(data transfer object til at formatere til Json)

    List<Pet> petList = new ArrayList<>();
    List<DTOPet> DTOPetList = new ArrayList<>();

    petList.addAll(facade.returnAllPets());
    for(Pet pet: petList){
        DTOPet dtopet = EntitytoDTO.converttoDTO(pet);
        DTOPetList.add(dtopet);
    }

    String json = gson2.toJson(DTOPetList);
     return Response.ok().entity(json).build();
}

当我使用调试器时,将使用正确的参数成功创建新列表,但是即使使用GSON,字符串JSON也仅以此[{},{},{},{}]创建

2 个答案:

答案 0 :(得分:1)

没有看到“宠物”类的样子,很难找出问题所在。我怀疑您的Pet类中有另一个类的变量,该变量也具有对pet类本身的引用(创建循环引用会导致序列化过程中的堆栈溢出)

答案 1 :(得分:1)

您需要检测错误的位置。我建议添加调试信息,例如

@Path("/allPets")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getPetsfromCollection() {
    log.debug("getPetsfromCollection start");
    List<Pet> petList = new ArrayList<>(facade.returnAllPets());
    log.debug("petList" + petList.length());

    String json = gson.toJson(petList);
    log.debug("json " + json);

    //TODO return proper representation object
     return Response.ok().entity(json).build();
}


EntityManagerFactory emf = Persistence.createEntityManagerFactory("PetHospitaljpa");
public Collection<Pet> returnAllPets (){
    log.debug("returnAllPets start"); 
    EntityManager  em = emf.createEntityManager();
    log.debug("createNamedQuery start"); 
    TypedQuery<Pet> query = em.createNamedQuery("Pet.findAll", Pet.class);
    log.debug("single result" + query.getSingleResult() ); 

    TypedQuery<Pet> query = em.createNamedQuery("Pet.findAll", Pet.class);
    log.debug("list result" + query.getResultList()); 

    TypedQuery<Pet> query = em.createNamedQuery("Pet.findAll", Pet.class);
    return query.getResultList();
}

P.S。。另外,请显示宠物课程,这可能是与此课程有关的问题。

更新:我也建议您尝试临时删除:

@OneToMany(cascade = CascadeType.ALL, mappedBy = "petId")
private Collection<Event> eventCollection;

和/或

@JoinColumn(name = "owner_id", referencedColumnName = "id")
@ManyToOne
private Owner ownerId;

然后检查您是否有这样的SO异常。看起来事件或所有者表太大或具有圆依赖性。