gpt4 book ai didi

java - 未指定 @DefaultValue 时 @QueryParam 的默认值是什么?

转载 作者:IT老高 更新时间:2023-10-28 21:10:14 24 4
gpt4 key购买 nike

例如,具有以下 Java REST 定义:

@GET
@Path("/something")
public String somthing(
@QueryParam("valString") String valString,
@QueryParam("valInt") int valInt,
@QueryParam("valBool") boolean valBool
) {
...
}

和调用:

curl -X GET 127.0.0.1/something

如果没有在调用中指定参数值是什么? (valString=?, valInt=?, valBool=?)

最佳答案

简答

参数值为:

  • valString: null
  • valInt:0
  • valBool: false

稍长一点的回答

引用 Java EE 7 tutorial关于 extracting request parameters :

If @DefaultValue is not used in conjunction with @QueryParam, and the query parameter is not present in the request, the value will be an empty collection for List, Set, or SortedSet; null for other object types; and the default for primitive types.

原始类型的默认值在 Java Tutorials 中描述。来自甲骨文:

 Primitive       Default Value
-------------------------------
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
char '\u0000'
boolean false

如您所知,可以使用 @DefaultValue 更改此行为。注释如下:

@GET
@Path("/foo")
public String myMethod(@DefaultValue("foo") @QueryParam("valString") String valString,
@DefaultValue("1") @QueryParam("valInt") int valInt,
@DefaultValue("true") @QueryParam("valBool") boolean valBool) {
....
}

关于java - 未指定 @DefaultValue 时 @QueryParam 的默认值是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35625155/

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