如何使用 groovy 传递可选的查询参数?
How to pass optional query parameters with groovy?
我想使用带 groovy 的 micronaut 将可选参数传递给 url。我做了很多研究,但找不到任何相关的答案。
@Get('/product/{country}/?
我想将排序和日期作为可选参数传递给此 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
注释的 POST 示例:
@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);
}
我想使用带 groovy 的 micronaut 将可选参数传递给 url。我做了很多研究,但找不到任何相关的答案。
@Get('/product/{country}/?
我想将排序和日期作为可选参数传递给此 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
注释的 POST 示例:
@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);
}