简体   繁体   English

Java JAX-RS @DefaultValue用于列表

[英]Java JAX-RS @DefaultValue for List

I have a List of enum values as a query parameter. 我有一个枚举值列表作为查询参数。 I want to have a @DefaultValue (single value). 我想要一个@DefaultValue (单个值)。 Is it possible to have an empty list as actual parameter to overwrite this default value? 是否有一个空列表作为实际参数可以覆盖此默认值?

The reason for this question is that Swagger (2.0) shows a list of the enum string values and an additional first entry -- . 出现此问题的原因是Swagger(2.0)显示了枚举字符串值的列表以及附加的第一项-- I can select any number of entries, including the empty set. 我可以选择任意数量的条目,包括空集。 So the -- entry seems to make no much sense. 因此, --入口似乎没有多大意义。 But selecting nothing or the -- entry results in 2 different request URLs, one without the parameter and one with the parameter with empty value. 但是什么也不选择或--条目将导致2个不同的请求URL,一个不带参数,另一个带带空值的参数。

So my expectation was that the request without the parameter results in an list with the default value on service side (as it actually is). 因此,我的期望是,不带参数的请求将导致一个列表,其中包含服务端的默认值(实际上是这样)。 And I expected an empty list in the other case, so that I would have a way to overwrite the default value also with an empty list. 在其他情况下,我希望有一个空列表,这样我就可以用一个空列表覆盖默认值。 But that seems not the case. 但事实并非如此。

I also expected that the default value would be pre-selected in the Swagger UI list, which is also not the case. 我还期望默认值将在Swagger UI列表中预先选择,事实并非如此。

So the answer to my first question seems to be no . 因此,我第一个问题的答案似乎是“ no

So the second question is: What is the reason or use of the -- entry in the list in Swagger UI? 因此,第二个问题是:Swagger UI列表中的--条目的原因或用途是什么?

For problem number 1 you can try something like this: 对于问题1,您可以尝试执行以下操作:

@GET
public Response getList(@QueryParam("enumList") @DefaultValue("#{T(java.util.Collections).emptyList()}") List<EnumType> enumList) {

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM