Jef*_*rdo 5 micronaut micronaut-client micronaut-rest
我需要在请求中将参数设置为“不需要”。
我试过:
@Get(value = "/list/{username}")
HttpResponse<?> list(String username, @QueryValue(value = "actionCode") String actionCode) {
...
}
Run Code Online (Sandbox Code Playgroud)
当我发送请求http://localhost:8080/notification/list/00000000000时,会引发以下错误:
{
"message": "Required Parameter [actionCode] not specified",
"path": "/actionCode",
"_links": {
"self": {
"href": "/notification/list/00000000000",
"templated": false
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以通过javax.annotation.Nullable注释将 Micronaut 中的查询参数定义为可选:
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.QueryValue;
import javax.annotation.Nullable;
@Controller("/sample")
public class SampleController {
@Get("/list/{username}")
public String list(
String username,
@Nullable @QueryValue String actionCode
) {
return String.format("Test with username = '%s', actionCode = '%s'", username, actionCode);
}
}
Run Code Online (Sandbox Code Playgroud)
以下是示例调用及其结果。打电话时不带actionCode:
$ curl http://localhost:8080/sample/list/some-user
Test with username = 'some-user', actionCode = 'null'
Run Code Online (Sandbox Code Playgroud)
致电actionCode:
$ curl http://localhost:8080/sample/list/some-user?actionCode=some-code
Test with username = 'some-user', actionCode = 'some-code'
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,没有错误,并且在 Micronaut 版本 1 和版本 2 中都是这样工作的。
| 归档时间: |
|
| 查看次数: |
8806 次 |
| 最近记录: |