Java 如何使用POST方法执行findAllBy()?

Java 如何使用POST方法执行findAllBy()?,java,spring-boot,hibernate,spring-data-jpa,modelmapper,Java,Spring Boot,Hibernate,Spring Data Jpa,Modelmapper,我被要求创建一个请求,该请求将根据客户的性别列出客户。然而,请求方法必须是POST,有人建议我使用dto和映射器来实现这个目标。我将举一些例子来进一步解释我的问题 我的客户实体如下: @Data @Entity @Table(name = "customer", schema = "public") public class Customer implements Serializable { @Id @Column(name = &quo

我被要求创建一个请求,该请求将根据客户的性别列出客户。然而,请求方法必须是POST,有人建议我使用dto和映射器来实现这个目标。我将举一些例子来进一步解释我的问题

我的客户实体如下:

@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() {

    }
我被要求将此流作为输入:

{ "gender": "W" }
作为输出,我希望收到性别为“W”的客户实体列表。因此,我创建了一个CustomerTo类:

@Data
public class CustomerDto {
    private String gender;
}
这是我将在CustomerRepository中定义的方法:

@Repository
public interface CustomerRepository extends JpaRepository<Customer, Integer> {

    List<Customer> findAllByGender(Customer customer);
}
@Repository
public interface CustomerRepository extends JpaRepository<Customer, Integer> {

    List<Customer> findAllByGender(String gender);
}
@存储库
公共接口CustomerRepository扩展了JpaRepository{
列出findAllByGender(客户);
}
这是我在控制器和服务上分别拥有的:

@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);
    }
@RequestMapping(method=RequestMethod.POST,value=“/customers/gender”)
公共列表GetCustomerByStream(@RequestBody@Valid Customer){
退货服务。getCustomersByGender(客户);
}
公共列表getCustomersByGender(客户){
返回回购findAllByGender(客户);
}
我将ModelMapper添加到依赖项中,并尝试了几种方法,包括customer和CustomerTo输入。但我未能成功地按性别列出客户。我希望有一个正确解释的代码答案,这样我就能理解发生了什么

编辑:

这是不使用ModelMapper的答案。如果有人正在搜索解决方案:

控制器:

@RequestMapping(method= RequestMethod.POST, value="/customers/gender")
    public List<Customer> getCustomersByStream(@RequestBody @Valid CustomerDto dto) {
        String gender = dto.getGender();
        return service.getCustomersByGender(gender);
    }
@RequestMapping(method=RequestMethod.POST,value=“/customers/gender”)
公共列表GetCustomerByStream(@RequestBody@Valid CustomerTo dto){
字符串gender=dto.getGender();
退货服务。getCustomersByGender(性别);
}
存储库:

@Repository
public interface CustomerRepository extends JpaRepository<Customer, Integer> {

    List<Customer> findAllByGender(Customer customer);
}
@Repository
public interface CustomerRepository extends JpaRepository<Customer, Integer> {

    List<Customer> findAllByGender(String gender);
}
@存储库
公共接口CustomerRepository扩展了JpaRepository{
列出findAllByGender(字符串性别);
}
服务:

public List<Customer> getCustomersByGender(String gender) {
        return repo.findAllByGender(gender);
    }
公共列表getCustomersByGender(字符串性别){
返回回购findAllByGender(性别);
}

好的,这很简单

在控制器中 你的回购协议
@存储库
公共接口CustomerRepository扩展了JpaRepository{
列出findAllByGender(字符串性别);
}
您的业务层。一些Java类。
公共列表getCustomersByGender(字符串性别){
列表响应=新建ArrayList();
List List=repo.findAllByGender(性别);
//的Autowire对象映射器手动创建新实例。
ObjectMapper mapper=新的ObjectMapper();
列表。forEach(客户->{
YourDTO ref=mapper.map(列表,YourDTO.class);
回复。添加(参考);
});
返回响应;
}

现在,您只需返回从服务层收到的响应。

客户将性别字段作为字符串,而不是customerDTO。您可以尝试更改
repo.findAllByGender(customer.getGender())
和更改
列表findAllByGender(字符串性别)谢谢您友好的回答。我对你的建议提出了类似的解决方案,并将其作为编辑添加到我的问题中。但是,它不使用映射器。我会问我的上司这是否可以接受。你不必总是为post请求发送一些值。您仍然可以发送空的post请求。此外,您的上级必须要求使用DTO作为响应,您在请求中接受DTO作为主体。我理解你们们的要求,若我写这篇文章,出于你们们的兴趣,它可能是相当大的代码结构。我对这个问题很陌生,所以我可能会遗漏一些你说过的东西。但他特别要求我输入
{“性别”:“W”}
{“性别”:“M”}
。我想这意味着它不是一个空的请求。不过,我还是很感谢你提供的关于空请求的信息。我正在添加一个响应!它有很多解释,所以它需要一些时间。我希望这将有助于你无论如何,你的答案很容易理解和信息。我理解你的意思,但我被要求创建一个自定义输入,而不是自定义输出。但是,您的示例使我更好地理解了mapper的使用。我开始认为这是某种类型的沟通失误,我的案例不需要地图绘制者。我对答案投了更高的票,因为它仍然会根据性别列出客户。我没有接受它,因为
@RequestParam
注释。因为我被特别要求作为一个整体提供信息。希望你的回答能引导更多的人克服困难。我认为这是一个更好的学习例子:)
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.
}
@Repository
public interface CustomerRepository extends JpaRepository<Customer, Integer> {

    List<Customer> findAllByGender(String gender);
}
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;
}