gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:05:23 25 4
gpt4 key购买 nike

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

@Get('/product/{country}/?

我想将排序和日期作为可选参数传递给此网址。感谢你的帮助。

最佳答案

您可以像这样将可选的排序和日期参数作为查询值传递:

@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.

关于groovy - 如何使用 groovy 传递可选的查询参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60807462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com