如何使用POST方法执行findAllBy?

问题描述

我被要求创建一个请求,该请求将根据性别列出客户。但是,请求方法必须是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() {

    }

这是基于我的代码的客户流示例:

{
        "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添加到我的依赖项中,并尝试了使用customer和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);
    }

解决方法

好的,那很简单。

在您的控制器中

//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的意思是,如果它们具有很大的数据有效载荷,则可以用来接受请求正文或用于投射自定义响应。我认为您的上司会要求使用DTO来定制响应。在响应中只放入您想要的变量。

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.
}

您的存储库

@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);
    //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;
}

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...