DTO 和实体映射
DTO and Entities mapping
我正在构建一个 Spring Rest 应用程序,我需要 DTO 方面的帮助并将结果解析到端点
这是json我return此刻到终点:
{
"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
}
}
]
}
}
我不想return这个json的“方法”,我只需要“id”、“name”、“description”、“mash_temp”-所以它应该是这样的:
{
"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
}
}
]
}
这些是我现在使用的实体:
啤酒实体:
@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;
}
方法实体:
@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
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;
}
临时实体:
@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;
}
有谁知道如何从这个实体创建 DTO 但没有“方法”字段?
这也是我的控制器:
@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);
}
查看 @JsonUnwrapped
注释 (https://fasterxml.github.io/jackson-annotations/javadoc/2.8/com/fasterxml/jackson/annotation/JsonUnwrapped.html)。可以放在Beer
class中的method
字段,然后Method
class的属性直接在同级序列化来自 Beer
.
的
我正在构建一个 Spring Rest 应用程序,我需要 DTO 方面的帮助并将结果解析到端点
这是json我return此刻到终点:
{ "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 } } ] } }
我不想return这个json的“方法”,我只需要“id”、“name”、“description”、“mash_temp”-所以它应该是这样的:
{
"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
}
}
]
}
这些是我现在使用的实体:
啤酒实体:
@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; }
方法实体:
@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 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; }
临时实体:
@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; }
有谁知道如何从这个实体创建 DTO 但没有“方法”字段?
这也是我的控制器:
@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); }
查看 @JsonUnwrapped
注释 (https://fasterxml.github.io/jackson-annotations/javadoc/2.8/com/fasterxml/jackson/annotation/JsonUnwrapped.html)。可以放在Beer
class中的method
字段,然后Method
class的属性直接在同级序列化来自 Beer
.