【问题标题】:Spring rest controller to filter a resource用于过滤资源的 Spring REST 控制器
【发布时间】:2018-05-26 11:50:23
【问题描述】:

在我的 Spring Boot 应用程序中,我有一个问题作为资源,具有以下字段。

    [  
       {  
          "questionId":6,
          "area":"TECHNICAL",
          "title":"Find the index of first 1 in an infinite sorted array of 0s and 1s",
          "description":"Given an infinite sorted array consisting 0s and 1s. The problem is to find the index of first 1 in that array. As the array is infinite, therefore it is guaranteed that number 1 will be present in the array.",
          "state":"ACTIVE",
          "difficultyLevel":"EASY",
          "skills":[  
             {  
                "skillId":1,
                "skillName":"ALGORITHM"
             },
             {  
                "skillId":2,
                "skillName":"PROGRAMMING"
             }
          ],
          "proposedBy":"agrawalo",
          "noOfTimesUsed":0,
          "examples":null,
          "probes":null,
          "approvedBy":null,
          "addedBy":null,
          "dateCreated":"2018-05-16T19:29:11.113",
          "dateLastUpdated":"2018-05-16T19:29:11.113"
       }, 
       {
        ...
       },
       ...
    ]

我想编写控制器来过滤问题。

例如:

1./questions?area="技术"。返回区域为“技术”的问题的控制器。为此,我编写了以下方法。

@RestController
public class QuestionController { 
    @RequestMapping("/questions",  method = GET)
    String getFilteredQuestions(@RequestParam("area") String questionArea) {

    }
}

2./questions?area="technical"&skill="programming"。现在,我想编写一个控制器,它返回区域为“技术”和技能为“编程”的问题。

实现此目的的一种方法是向“getFilteredQuestion”方法添加一个请求参数。

但是,我最终会编写一个非常混乱的代码(例如,如果我想添加更多过滤器)来检查哪些请求参数为空,哪些不是,并基于该应用过滤器。

@RestController
public class QuestionController {
    @RequestMapping("/questions",  method = GET)
    String getFilteredQuestions(@RequestParam("area") String questionArea, @RequestParam("skill") String questionSkill) {

    }
}

3./questions?skill="algorithm"&proposedby="agrawalo" ...

有什么简洁的方法可以编写这样的控制器吗?

【问题讨论】:

标签: java rest spring-boot spring-restcontroller


【解决方案1】:

也许你可以把 Question 类写成 Entity 类。

public class Question {
         private String title;
         private String description;
         private List<String> skills;
         private Date createdAt;

         public void setTitle(String title) {
             this.title = title;
         }
         public String getTitle() {
             return this.title;
         }

    }

然后通过这种方式你可以得到你的数据

@RestController
public class QuestionController {
    @RequestMapping("/questions",  method = GET)
    String getFilteredQuestions@RequestBody Question question) {

    }
}

【讨论】:

  • 我认为将该对象称为 Entity 是不合适的。它是一个临时对象,仅对单个请求有效。请看docs.oracle.com/javaee/6/tutorial/doc/bnbqa.html
  • 你可以做这个QuestionDto
  • 是的,DTO 会更合适。此外,请注意,不鼓励 GET 请求中的 body。不过,如果您不拘泥于 RESTful 原则,那么您建议的解决方案会经常使用,但使用 POST 方法。
【解决方案2】:

使用您支持的所有查询和过滤参数创建一个 Java POJO。确保您可以将该 POJO 映射到 JSON,反之亦然。您的客户端可以编辑 JSON 版本并将其与请求一起发送。您将其映射回您的 POJO,并将其传递给负责准备适当查询的业务逻辑。这样,您就可以保持控制器的方法不变。

【讨论】:

  • 任何示例参考,或者您能帮我提供解决上述用例的真实代码吗?
  • 看看 Jackson 对象映射,例如springframework.guru/processing-json-jackson 如果您想坚持使用 GET 请求,您可以序列化查询 JSON 对象并将其附加到您的 URL,例如 /questions?query=。
猜你喜欢
  • 2017-04-22
  • 1970-01-01
  • 1970-01-01
  • 2017-09-23
  • 2018-04-14
  • 2018-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多