英文:
How can I perform findAllBy() using POST method?
问题
以下是翻译的内容:
我被要求创建一个请求,根据顾客的性别列出顾客。然而,请求方法必须是POST,并且我被建议使用DTO和Mapper来实现这个目标。我将举一些例子来进一步解释我的问题。
我的顾客实体如下所示:
@Data
@Entity
@Table(name = "customer", schema = "public")
public class Customer implements Serializable {
@Id
@Column(name = "id_", unique = true)
private Integer id_;
@Column(name = "name_", nullable = false)
private String name_;
@Column(name = "surname", nullable = false)
private String surname;
@Column(name = "phone", nullable = false)
private String phone;
@Column(name = "email", nullable = false)
private String email;
@Column(name = "gender", columnDefinition = "text", nullable = false)
private String gender;
@JsonBackReference
@OneToMany(mappedBy = "customer")
Set<PurchaseOrder> purchaseOrder = new HashSet();
public Customer() {
}
这是基于我的代码的顾客流的示例:
{
"id_": 1,
"name_": "Laura",
"surname": "Blake",
"phone": "95334567865",
"email": "Bulvar 216 PF Changs",
"gender": "W"
}
我被要求将此流用作输入:
{ "gender": "W" }
作为输出,我期望收到一个性别为'W'的顾客实体列表。因此,我创建了一个CustomerDto类:
@Data
public class CustomerDto {
private String gender;
}
这是我将在CustomerRepository中使用的方法:
@Repository
public interface CustomerRepository extends JpaRepository<Customer, Integer> {
List<Customer> findAllByGender(Customer customer);
}
以下是我的控制器和服务上的内容:
@RequestMapping(method= RequestMethod.POST, value="/customers/gender")
public List<Customer> getCustomersByStream(@RequestBody @Valid Customer customer) {
return service.getCustomersByGender(customer);
}
public List<Customer> getCustomersByGender(Customer customer) {
return repo.findAllByGender(customer);
}
我在我的依赖项中添加了ModelMapper,并尝试了使用顾客和CustomerDto输入的多种方法。但我未能成功按性别列出顾客。我会非常感谢一份带有适当解释的代码答案,这样我就能理解正在发生的情况。
编辑:
这是不使用ModelMapper的答案。以防有人正在寻找解决方案:
控制器:
@RequestMapping(method= RequestMethod.POST, value="/customers/gender")
public List<Customer> getCustomersByStream(@RequestBody @Valid CustomerDto dto) {
String gender = dto.getGender();
return service.getCustomersByGender(gender);
}
仓库:
@Repository
public interface CustomerRepository extends JpaRepository<Customer, Integer> {
List<Customer> findAllByGender(String gender);
}
服务:
public List<Customer> getCustomersByGender(String gender) {
return repo.findAllByGender(gender);
}
英文:
I was asked to create a request that will list customers based on their genders. However the request method has to be POST and I was adviced to use a dto and a mapper to achieve this goal. I'll give some examples to further explain my problem.
My Customer entity is as follows:
@Data
@Entity
@Table(name = "customer", schema = "public")
public class Customer implements Serializable {
@Id
@Column(name = "id_", unique = true)
private Integer id_;
@Column(name = "name_", nullable = false)
private String name_;
@Column(name = "surname", nullable = false)
private String surname;
@Column(name = "phone", nullable = false)
private String phone;
@Column(name = "email", nullable = false)
private String email;
@Column(name = "gender", columnDefinition = "text", nullable = false)
private String gender;
@JsonBackReference
@OneToMany(mappedBy = "customer")
Set<PurchaseOrder> purchaseOrder = new HashSet();
public Customer() {
}
This is an example for customer stream based on my code:
{
"id_": 1,
"name_": "Laura",
"surname": "Blake",
"phone": "95334567865",
"email": "Bulvar 216 PF Changs",
"gender": "W"
}
I am asked to give this stream as input:
{ "gender": "W" }
As an output I am expected to receive a list of customer entities with gender 'W'. So, I have created a CustomerDto class:
@Data
public class CustomerDto {
private String gender;
}
This is the method I'm going to use defined in CustomerRepository:
@Repository
public interface CustomerRepository extends JpaRepository<Customer, Integer> {
List<Customer> findAllByGender(Customer customer);
}
This is what I have on my controller and service, respectively:
@RequestMapping(method= RequestMethod.POST, value="/customers/gender")
public List<Customer> getCustomersByStream(@RequestBody @Valid Customer customer) {
return service.getCustomersByGender(customer);
}
public List<Customer> getCustomersByGender(Customer customer) {
return repo.findAllByGender(customer);
}
I added ModelMapper to my dependencies and I tried several methods both with customer and customerDto inputs. But I failed to successfully list customers by gender. I'd appreciate a code answer with proper explanations so that I can understand what's going on.
EDIT:
This is the answer without using ModelMapper. In case anyone is searching for a solution:
Controller:
@RequestMapping(method= RequestMethod.POST, value="/customers/gender")
public List<Customer> getCustomersByStream(@RequestBody @Valid CustomerDto dto) {
String gender = dto.getGender();
return service.getCustomersByGender(gender);
}
Repository:
@Repository
public interface CustomerRepository extends JpaRepository<Customer, Integer> {
List<Customer> findAllByGender(String gender);
}
Service:
public List<Customer> getCustomersByGender(String gender) {
return repo.findAllByGender(gender);
}
答案1
得分: 1
好的,以下是翻译好的内容:
在您的控制器中:
// 传递参数 geneder = someValue,您将在 gender 变量中得到该值
@RequestMapping(method = RequestMethod.POST, value = "/customers/gender")
public List<Customer> getCustomersByStream(@RequestBody AnotherDTO gender) {
return service.getCustomersByGender(anotherDTO.getGender());
}
DTO 用于接受请求体,如果请求体具有大量数据负载或用于强制转换自定义响应。我认为您的上级可能会要求使用 DTO 来定制响应。只放入您希望在响应中存在的变量。
ResponseDTO.java
public class CustomerDto {
private String gender;
private Long id;
private String name;
private String surname;
private String phone;
private String email;
// 标准的设置器和获取器
// 此外,您需要确保实体中的变量名与您的实体完全相同。在 DTO 中,您只需放入希望在响应中的那些变量。
}
您的 Repo
@Repository
public interface CustomerRepository extends JpaRepository<Customer, Integer> {
List<Customer> findAllByGender(String gender);
}
您的业务层。一些 Java 类。
public List<Customer> getCustomersByGender(String gender) {
List<Customer> response = new ArrayList<>();
List<Customer> list = repo.findAllByGender(gender);
// 自动装配对象映射器或手动创建新实例。
ObjectMapper mapper = new ObjectMapper();
list.forEach(customer -> {
YourDTO ref = mapper.map(list, YourDTO.class);
response.add(ref);
});
return response;
}
现在,您可以简单地返回从服务层接收到的响应。
英文:
Okay, that's pretty simple.
In Your Controller
//Pass a parameter geneder = someValue and you will get that in the gender variable
@RequestMapping(method= RequestMethod.POST, value="/customers/gender")
public List<Customer> getCustomersByStream(@RequestBody AnotherDTO gender) {
return service.getCustomersByGender(anotherDTO.getGender());
}
DTO's are meant to be used to accept a request body if they have a large data payload or for casting custom responses. As I think your superior would have asked to use a DTO for customizing response. Put only those varibles which you want to be there in the response.
ResponseDTO.java
public class CustomerDto {
private String gender;
private Long id;
private String name;
private String surname;
private String phone;
private String email;
//Standard Setters and getters
//Also, you need to make sure that the variable name in the Entity should
//be exactly same as your Entity. In DTO you just need to put those
//variables which you want to be in response.
}
Your Repo
@Repository
public interface CustomerRepository extends JpaRepository<Customer, Integer> {
List<Customer> findAllByGender(String gender);
}
Your Business layer. Some Java class.
public List<Customer> getCustomersByGender(String gender) {
List<Customer> response = new ArrayList<>();
List<Customer> list = repo.findAllByGender(gender);
//Autowire Object mapper of manually create a new instance.
ObjectMapper mapper = new ObjectMapper();
list.forEach(customer ->{
YourDTO ref = mapper.map(list, YourDTO.class);
response.add(ref);
});
return response;
}
Now, you can simply return the response that you've received from the service layer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论