首页 > 解决方案 > 在 java rest API 中将集合从实体转换为 DTO

问题描述

我有一个 java rest 应用程序,我想在其中将一些信息显示为 json。我现在有两个实体类在玩宠物和事件。这种关系太多了,所以一个 Pet 有一个事件集合。

以前我制作了一个实体宠物列表并制作了一个自定义静态方法来将宠物实体转换为 DTO 对象,如下所示:

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


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


    // here i'm looping through my list and then converting them to DTO object, and adding dem to a string called json   
    petList.addAll(facade.returnAllPets());
    for(Pet pet: petList){
        DTOPet dtopet = EntitytoDTO.convertDTOPet(pet);
        DTOPetList.add(dtopet);
    }

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

现在我想做同样的事情,但我也想在 PetDTO 内部显示事件集合

这是我的 PetDTO 课程

    /*
 * 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 DTO;

import Entities.Event;
import java.util.Collection;
import java.util.Date;

/**
 *
 * @author kristoffer
 */
public class DTOPet {

private Integer id;
private String name;
private Date birth;
private String species;
private Date death;
private Collection<DTOEvent> eventCollection;

public DTOPet(Integer id, String name, Date birth, String species, Date death, Collection<DTOEvent> eventCollection) {
    this.id = id;
    this.name = name;
    this.birth = birth;
    this.species = species;
    this.death = death;
    this.eventCollection = eventCollection;
}

public DTOPet() {
}

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

public Collection<DTOEvent> getEventCollection() {
    return eventCollection;
}

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

}

我怎样才能做到这一点?

我尝试了几种方法,但还没有成功。

到目前为止,我在这里得到了什么:

 @Path("/allPetsE")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getEventofPets() { 

    //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<>();
    List<Event> eventList = new ArrayList<>();
    List<DTOEvent> DTOevents = new ArrayList<>();

    petList.addAll(facade.returnAllPets());
    eventList.addAll(facade.returnAllEvents());

    //dette er ikke en pæn løsning, men lad os prøve at få hver event lavet om til et DTO og tilføje det til de forskellige dyr


    for(Pet pet: petList){
        DTOPet dtopet = EntitytoDTO.convertDTOPet(pet);
        dtopet.setEventCollection(DTOevents);
        DTOPetList.add(dtopet);
        //dette bliver ikke performance optimere i føste omgang, vi skal bare få det til at virke


    }


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

我的代码的问题是我无法将整个事件实体对象集合转换为 DTO 对象,我可以尝试在方法内部循环,但这会对性能产生可怕的影响,或者我可以尝试制作一个JPQL 查询,但这仍然不能解决转换为 DTO 对象的问题。

可以解决的另一种方法是更改​​我当前的 JPQL 查询的返回类型,如下所示:

 public Collection<Event> returnAllEvents (){

    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<Event> query = em.createNamedQuery("Event.findAll", Event.class);

    return query.getResultList();
}

所以它会返回一个集合集合,但我不知道该怎么做。

标签: javajsonrestjpajpql

解决方案


推荐阅读