Search code examples
spring-boothibernaterestmappingdto

DTO and Entities mapping


I am building a Spring Rest Application, I need help with DTO's and parsing a result to a endpoint

  • This is json that I return at the moment to the endpoint:

    {
      "id": 1,
      "name": "Ella - IPA Is Dead",
      "description": "2015 IPA is Dead Series. Supremely floral, this hugely under-rated hop is related to Galaxy and was first cultivated in the Australian state of Victoria.",
      "method": {
          "mash_temp": [
           {
               "temp": {
                   "value": 65
               }
           }
      ]
     }
    }
    

I don't want to return "method" from this json, I just need "id", "name", "description", "mash_temp" - so it should look like this:

{
  "id": 1,
  "name": "Ella - IPA Is Dead",
  "description": "2015 IPA is Dead Series. Supremely floral, this hugely under-rated hop is related to Galaxy and was first cultivated in the Australian state of Victoria. Initially given the same name as a certain Eurolager, their lawyers got involved and the St- prefix was dropped. Ella displays subtle notes of spice, but is fundamentally a truly floral bouquet, redolent of the Southern Hemisphere.",
  "mash_temp": [
   {
      "temp": {
         "value": 65
         }
      }
  ]
}

Those are the entities that I am using now:

  • Beer Entity:

    @Entity
    public class Beer implements Serializable {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "beer_id", unique = true, nullable = false)
        private Integer id;
    
        @Column(name = "name", nullable = false)
        private String name;
    
        @JsonProperty("description")
        @Column(name = "description", nullable = false, columnDefinition = "TEXT")
        private String description;
    
        @JsonProperty("method")
        @OneToOne(cascade = CascadeType.ALL)
        private Method method;
    }
    
  • Method Entity:

    @Entity
    public class Method implements Serializable
    {
    
         @JsonIgnore(value = true)
         @Id
         @GeneratedValue(strategy = GenerationType.IDENTITY)
         private Integer id;
    
         @JsonProperty("mash_temp")
         @OneToMany(cascade = CascadeType.ALL)
         @JoinColumn(name = "mash_temp")
         private List<MashTemp> mash_temp = new ArrayList<>();
    }
    
  • MashTemp Entity:

    @Entity
    public class MashTemp implements Serializable
    {
    
        @JsonIgnore(value = true)
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Integer id;
    
        @OneToOne(cascade = CascadeType.ALL)
        private Temp temp;
    
        @ManyToOne
        private Method method;
    }
    
  • Temp Entity:

    @Entity
    public class Temp implements Serializable
    {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Integer id;
    
        private Integer value;
    
        @JsonIgnore(value = true)
        private String unit;
    
        @OneToOne
        private MashTemp mashTemp;
    }
    

Does anyone know how to create DTO's from this Entities but without "method" field?

  • Also this is my Controller:

    @GetMapping("/beers")
    public ResponseEntity<Set<Beer>> getAllBeers()
    {
        return new ResponseEntity<>(beerService.getAllBeers(), HttpStatus.OK);
    }
    
    @GetMapping("/beers/{id}")
    public ResponseEntity<Beer> getById(@PathVariable Integer id) {
        Beer beer = beerService.findById(id);
    
        return new ResponseEntity<>(beer, HttpStatus.OK);
    }
    

Solution

  • Have a look at the @JsonUnwrapped annotation (https://fasterxml.github.io/jackson-annotations/javadoc/2.8/com/fasterxml/jackson/annotation/JsonUnwrapped.html). You can put it on the method field in the Beer class, and then the properties of the Method class are serialized directly on the same level as the ones from Beer.