Java 使用自定义请求对象时使用可选参数的Spring@RequestMapping

Java 使用自定义请求对象时使用可选参数的Spring@RequestMapping,java,spring,controller,Java,Spring,Controller,您好,我想在我的控制器上设置一个可选参数,id,到目前为止看起来像这样 @ApiOperation(value = "get runs by date in order to paginate") @ApiResponses({ @ApiResponse(code = 200, message = "Success"), @ApiResponse(code = 500, message = "Unexpected failure") }) @Req

您好,我想在我的控制器上设置一个可选参数,
id
,到目前为止看起来像这样

    @ApiOperation(value = "get runs by date in order to paginate")
    @ApiResponses({ @ApiResponse(code = 200, message = "Success"),
            @ApiResponse(code = 500, message = "Unexpected failure") })
    @RequestMapping(value = "/time/{date}/to/{toPage}/from/{fromPage}",
                    params = {"pageSize", "id"},
                    method = RequestMethod.GET)
    public RunCollection getPolicyByDate(GetPolicyByDateRequest request) {
        return serviceImpl.getListOfRunsByDate(request);
    }

但这意味着参数是必需的。我希望它是可选的。我在他们使用
@RequestParam
的地方看到了这个答案,但我想将它包含在我的
GetPolicyByDateRequest
对象中。这可能吗?spring文档没有暗示要关闭必需的属性。我是否可以使用
可选

2个选项:

  • 如果
    GetPolicyByDateRequest
    没有基元属性,则应通过Spring将它们插入到对象中。如果未设置参数(即url中没有
    pageSize
    ),则Spring会在属性中注入NULL
  • 奇怪的是,我认为这是Spring中的一些错误之王,即使在使用
    Optional
    时,如果没有提供requestParam中的参数,您也会得到一个空对象

  • 如果要获取所有参数,则需要在本例中将
    @PathVariable
    @RequestParam
    一起使用。以下是获取参数的方法:
  • @RequestMapping(value=“/time/{date}/to/{toPage}/from/{fromPage}”,method=RequestMethod.GET)
    公共RunCollection getPolicyByDate(@PathVariable字符串日期、@PathVariable int toPage、@PathVariable int fromPage、@RequestParam(必需=false)可选页面大小、@RequestParam(必需=false)可选id){
    //然后按照您当时使用的方式使用参数。
    }
    
    请注意:

    • @PathVariable
      检索URL中的参数,其中值介于
      {}
      之间,如
      {date}
      ->
      @PathVariable String date
    • @RequestParam
      检索URL中
      后面的参数<代码>?pageSize=10
    ->
    @RequestParam int pageSize
  • 除其他参数外,这两个注释都采用
    required=true | false
    。当您将一个参数标记为非必需时,请确保您不使用诸如int之类的基元类型,否则,您可以将该对象设置为null,并且您的代码将在运行时失败。这就是为什么我使用
    可选
    ,以允许该类型。但是Spring很聪明,如果它检测到参数中的
    可选
    ,那么您将被迫在
    @RequestParam
    中使用
    required=false
  • 如果您想填充
    GetPolicyByDateRequest
    ,那么您应该已经完成了
    POST
    映射,并使用注释
    @RequestBody GetPolicyByDateRequest
    ,并在请求中发送一个JSON正文以生成Spring(实际上是Jackson)将JSON转换为
    GetPolicyByDateRequest
    对象


    希望它有帮助

    “我想把它包括在我的
    GetPolicyByDateRequest
    对象中”GET请求没有任何正文。我不确定您打算如何从GET请求中获取
    GetPolicyByDateRequest
    方法参数?因为这是秘密的,所以我不确定@narendra choudhary它是如何工作的。请求对象是否不保存所有请求数据?因为只要名称相同,它就可以拾取所有其他4个参数。请看一下如何使用
    @PathVariable
    。我知道我可以使用
    @RequestParam
    来执行此操作,但我希望将其保留在
    Request
    对象中。这就是问题您的第一个选项是我的设置,但是如果参数不存在,spring会抱怨。我正在寻找一种方法,将其保存在请求对象中,而不需要强制执行。您的spring版本是什么?因为它在4.2.1这样的Melook中运行良好,所以当传递一个参数而不是另一个参数时,我会出现此错误
    出现意外错误(type=Bad Request,status=400)。实际请求参数不满足参数条件“pageSize,id”:pageSize={25}
    查看此链接,在任何版本上都不可能删除
    params={“pageSize”,“id”},
    属性并尝试一下。
    public class GetPolicyByDateRequest {
    
        private String date;
        private int toPage;
        private int fromPage;
        private Integer pageSize;
        private Integer id;
    
        public GetPolicyByDateRequest() {
        }
    
        public String getDate() {
            return date;
        }
    
        public void setDate(String date) {
            this.date = date;
        }
    
        public int getToPage() {
            return toPage;
        }
    
        public void setToPage(int toPage) {
            this.toPage = toPage;
        }
    
        public int getFromPage() {
            return fromPage;
        }
    
        public void setFromPage(int fromPage) {
            this.fromPage = fromPage;
        }
    
        public Integer getPageSize() {
            return pageSize;
        }
    
        public void setPageSize(Integer pageSize) {
            this.pageSize = pageSize;
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        @Override
        public String toString() {
            return "MyRequest{" +
                    "date='" + date + '\'' +
                    ", toPage=" + toPage +
                    ", fromPage=" + fromPage +
                    ", pageSize=" + pageSize +
                    ", id=" + id +
                    '}';
        }
    }
    
     @RequestMapping(value = "/time/{date}/to/{toPage}/from/{fromPage}", method = RequestMethod.GET)
        public RunCollection getPolicyByDate(@PathVariable String date, @PathVariable int toPage, @PathVariable int fromPage, @RequestParam(required = false) Optional<Integer> pageSize, @RequestParam(required = false) Optional<Integer> id) {
            // then use your parameters the way you to use then.
        }