在 rest api 中响应个性化对象的最佳做法是什么 (spring)
What is the best practice to response a personalized object in rest apis (spring)
在我的控制器中响应个性化对象的最佳做法是什么?
必要性示例:
我有 X 个实体,我的一个控制器需要 return 一个具有其他实体特定属性的 json 对象。
创建一个名为 responses 的包,并在其中创建一个 class 和个性化字段,这是个好主意吗?
public class OrderItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private Integer quantity;
@ManyToOne
@JoinColumn(name= "order_id")
private Order order;
@ManyToOne
@JoinColumn(name= "product_id")
private Product product;
}
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String description;
}
public class ResponseOrderItems {
@JsonIgnore
Integer orderId;
Integer productId;
Integer quantity;
String description;
}
基本上,ResponseOrderItems 是一个 class“组合”来自 Product 和 OrderItems 以及其他实体的属性。
我应该继续把它当作回应还是当作 entity/model?
更多详情:
在服务层,我使用其他实体的属性构建 ResponseOrderItems 并 returning 它。
这绝对是正确的方法。你甚至应该避免让你的实体在你的控制器上有响应对象。一个更重要的原因是您可能需要调整您的实体模型或 API 模型,但您不想修改另一个。只有当你有单独的模型时,你才能这样做。这个模式被命名为 DTO (Data Transfer Object):
DTOs or Data Transfer Objects are objects that carry data between
processes in order to reduce the number of methods calls. The pattern
was first introduced by Martin Fowler in his book EAA. He explained
that the pattern's main purpose is to reduce roundtrips to the server
by batching up multiple parameters in a single call.
(...)
With DTOs, we can build different views from our domain models,
allowing us to create other representations of the same domain but
optimizing them to the clients' needs without affecting our domain
design. Such flexibility is a powerful tool to solve complex problems.
这与您正在尝试做的类似。
在我的控制器中响应个性化对象的最佳做法是什么?
必要性示例: 我有 X 个实体,我的一个控制器需要 return 一个具有其他实体特定属性的 json 对象。 创建一个名为 responses 的包,并在其中创建一个 class 和个性化字段,这是个好主意吗?
public class OrderItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private Integer quantity;
@ManyToOne
@JoinColumn(name= "order_id")
private Order order;
@ManyToOne
@JoinColumn(name= "product_id")
private Product product;
}
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String description;
}
public class ResponseOrderItems {
@JsonIgnore
Integer orderId;
Integer productId;
Integer quantity;
String description;
}
基本上,ResponseOrderItems 是一个 class“组合”来自 Product 和 OrderItems 以及其他实体的属性。 我应该继续把它当作回应还是当作 entity/model?
更多详情: 在服务层,我使用其他实体的属性构建 ResponseOrderItems 并 returning 它。
这绝对是正确的方法。你甚至应该避免让你的实体在你的控制器上有响应对象。一个更重要的原因是您可能需要调整您的实体模型或 API 模型,但您不想修改另一个。只有当你有单独的模型时,你才能这样做。这个模式被命名为 DTO (Data Transfer Object):
DTOs or Data Transfer Objects are objects that carry data between processes in order to reduce the number of methods calls. The pattern was first introduced by Martin Fowler in his book EAA. He explained that the pattern's main purpose is to reduce roundtrips to the server by batching up multiple parameters in a single call. (...) With DTOs, we can build different views from our domain models, allowing us to create other representations of the same domain but optimizing them to the clients' needs without affecting our domain design. Such flexibility is a powerful tool to solve complex problems.
这与您正在尝试做的类似。