Java Spring引导:根据用户请求从API获取所需的列

Java Spring引导:根据用户请求从API获取所需的列,java,spring,spring-boot,spring-mvc,spring-data,Java,Spring,Spring Boot,Spring Mvc,Spring Data,我正在尝试实现一个API端点,用户可以在其中从API请求所需的列 基本上,这就是我想要的: 这是我的产品表实体 @Entity @Table(name ="products") class product{ private Long id; private String itemName; private String itemDesc; private double quantity; private double purchaseRate;

我正在尝试实现一个API端点,用户可以在其中从API请求所需的列

基本上,这就是我想要的: 这是我的产品表实体

@Entity
@Table(name ="products") 
class product{
   private Long id;
   private String itemName;
   private String itemDesc;
   private double quantity;
   private double purchaseRate;
   private double saleRate;
   private double onlineSaleRate;
.
.
.
constructor()
getter & setter
}
***我的目标是:
localhost:8080/api/v1/products

要求: 我想写一个api端点,在这里我根据需求A请求列,并将这些列作为响应

示例:如果我只需要-itemName、itemPrice和quantity,我将只返回它们作为响应。 如果某个用户对itemName、purchaseRate、saleRate、quantity有需求,他只会将这些作为响应

现在我正在根据需求编写新的端点,但我认为有一些方法可以做到这一点


我想在我的应用程序中实现这一点,为此我尝试了google,但没有根据我的要求生成搜索查询。

创建一个包含实体所有字段的类,字段类型为nullable boxed Boolean(用于请求json)

然后,要构造自定义响应,可以使用java映射来实现:

public ResponseEntity<Object> getFilteredColumns(ProductColumns columns) {
    Map<String, Object> map = new HashMap<String, Object>();

    if (columns.getItemName() == true) {
        map.put("itemName", [your repo/service method for getting the particular value]);
    }
    if (columns.getItemDesc() == true) {
        map.put("itemDesc", your repo/service method for getting the particular value]);
    }

    ...

    return ResponseEntity<Object>(map, HttpStatus.OK);
}
public ResponseEntity getFilteredColumns(ProductColumns){
Map Map=newhashmap();
if(columns.getItemName()==true){
put(“itemName”,[获取特定值的回购/服务方法]);
}
if(columns.getItemDesc()==true){
put(“itemDesc”,用于获取特定值的repo/service方法);
}
...
返回响应性(map,HttpStatus.OK);
}

当然,您应该根据自己的喜好将其包装在一些
try catch
中。

@SyedMustafaHussain这对我的要求没有帮助,这是一个关于如何编写rest api的指南。您可以使用JsonViews来实现这一点。请记住,在这种方法中,您应该自己实现这一点。它不是即插即用的。您的要求也不是微不足道的。谢谢您的回复。这将完成目前的工作。
public ResponseEntity<Object> getFilteredColumns(ProductColumns columns) {
    Map<String, Object> map = new HashMap<String, Object>();

    if (columns.getItemName() == true) {
        map.put("itemName", [your repo/service method for getting the particular value]);
    }
    if (columns.getItemDesc() == true) {
        map.put("itemDesc", your repo/service method for getting the particular value]);
    }

    ...

    return ResponseEntity<Object>(map, HttpStatus.OK);
}