如何使用groovy传递可选查询参数?

如何使用groovy传递可选查询参数?,groovy,micronaut,Groovy,Micronaut,我想使用micronaut和groovy将可选参数传递到url。我做了很多研究,但没有找到任何相关的答案 @Get('/product/{country}/? 我想把sort和date作为可选参数传递给这个url。 谢谢你的帮助 您可以将可选的排序和日期参数作为查询值传递,如下所示: @Controller('/') @CompileStatic class WithOptionalParameterController { @Get('/product/{country}{?sort

我想使用micronaut和groovy将可选参数传递到url。我做了很多研究,但没有找到任何相关的答案

@Get('/product/{country}/?
我想把sort和date作为可选参数传递给这个url。
谢谢你的帮助

您可以将可选的排序和日期参数作为查询值传递,如下所示:

@Controller('/')
@CompileStatic
class WithOptionalParameterController {
    @Get('/product/{country}{?sort,date}')
    String productsForCountry(String country, 
                              @Nullable @Pattern(regexp = 'code|title') String sort, 
                              @Nullable String date) {
        "Products for $country sorted by $sort and there is also date $date."
    }
}
可以通过指定排序和日期的方式调用:

$ curl 'http://localhost:8080/product/chile?sort=code&date=23.3.2020'
Products for chile sorted by code and there is also date 23.3.2020.
或无日期:

$ curl 'http://localhost:8080/product/chile?sort=code'
Products for chile sorted by code and there is also date null.
或无排序和日期:

$ curl 'http://localhost:8080/product/chile'
Products for chile sorted by null and there is also date null.
必须为查询参数添加@QueryValue注释的帖子示例:

@Consumes([MediaType.TEXT_PLAIN])
@Post('/product/{country}{?sort,date}')
String productsForCountry(String country, 
                          @Nullable @Pattern(regexp = 'code|title') @QueryValue String sort,
                          @Nullable @QueryValue String date,
                          @Body String body) {
    "Products for $country sorted by $sort and there is also date $date. Body is $body."
}
可以这样说:

$ curl -X POST 'http://localhost:8080/product/chile?sort=code&date=23.3.2020' -H "Content-Type: text/plain" -d 'some body'
Products for chile sorted by code and there is also date 23.3.2020. Body is some body.

您可以将可选的排序和日期参数作为查询值传递,如下所示:

@Controller('/')
@CompileStatic
class WithOptionalParameterController {
    @Get('/product/{country}{?sort,date}')
    String productsForCountry(String country, 
                              @Nullable @Pattern(regexp = 'code|title') String sort, 
                              @Nullable String date) {
        "Products for $country sorted by $sort and there is also date $date."
    }
}
可以通过指定排序和日期的方式调用:

$ curl 'http://localhost:8080/product/chile?sort=code&date=23.3.2020'
Products for chile sorted by code and there is also date 23.3.2020.
或无日期:

$ curl 'http://localhost:8080/product/chile?sort=code'
Products for chile sorted by code and there is also date null.
或无排序和日期:

$ curl 'http://localhost:8080/product/chile'
Products for chile sorted by null and there is also date null.
必须为查询参数添加@QueryValue注释的帖子示例:

@Consumes([MediaType.TEXT_PLAIN])
@Post('/product/{country}{?sort,date}')
String productsForCountry(String country, 
                          @Nullable @Pattern(regexp = 'code|title') @QueryValue String sort,
                          @Nullable @QueryValue String date,
                          @Body String body) {
    "Products for $country sorted by $sort and there is also date $date. Body is $body."
}
可以这样说:

$ curl -X POST 'http://localhost:8080/product/chile?sort=code&date=23.3.2020' -H "Content-Type: text/plain" -d 'some body'
Products for chile sorted by code and there is also date 23.3.2020. Body is some body.

使用可选注释,如

@Get("/optional")
String optional(@QueryValue Optional<Integer> max) {
  return "Parameter Value: " + max.orElse(10);
}

使用可选注释,如

@Get("/optional")
String optional(@QueryValue Optional<Integer> max) {
  return "Parameter Value: " + max.orElse(10);
}

伟大的谢谢,这很有效。有没有办法通过@Post-request传递参数?我还添加了Post-request的示例。您必须为查询参数添加@QueryValue注释,否则它将尝试从body加载它们。太好了!谢谢,这很有效。有没有办法通过@Post-request传递参数?我还添加了Post-request的示例。您必须为查询参数添加@QueryValue注释,否则它将尝试从正文中加载这些参数。