如何解决如何在micronaut GET请求中设置不需要的参数?
我需要在请求中将参数设置为“不需要”。
我尝试过:
@Get(value = "/list/{username}")
HttpResponse<?> list(String username,@QueryValue(value = "actionCode") String actionCode) {
...
}
当我发送请求 http:// localhost:8080 / notification / list / 00000000000 时,抛出以下错误:
{
"message": "Required Parameter [actionCode] not specified","path": "/actionCode","_links": {
"self": {
"href": "/notification/list/00000000000","templated": false
}
}
}
解决方法
您可以通过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);
}
}
这是示例调用及其结果。不使用actionCode
进行呼叫:
$ curl http://localhost:8080/sample/list/some-user
Test with username = 'some-user',actionCode = 'null'
致电actionCode
:
$ curl http://localhost:8080/sample/list/some-user?actionCode=some-code
Test with username = 'some-user',actionCode = 'some-code'
如您所见,没有错误,并且它在Micronaut版本1和版本2中都是这样。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。