[英]How can I specify a Default value dynamically in Jersey?
I am using Java Jersey library to create RESTful web service.我正在使用 Java Jersey 库来创建 RESTful Web 服务。
I am using query param for method.我正在使用查询参数作为方法。 I want to specify the Default value for that query param.我想为该查询参数指定默认值。 If I specify a constant string, then it is fine.如果我指定一个常量字符串,那就没问题了。 But how can I specify a runtime value as the default value?但是如何将运行时值指定为默认值?
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
@Path( "Hello" )
public class HelloWorld
{
private String defaultValue = "Default";
@GET
@Produces( MediaType.APPLICATION_XML )
public String greet( @QueryParam( "User" ) @DefaultValue( "defaultValue" )String userName )
{
String returnValue = "Hello " + userName;
System.out.println( returnValue );
return returnValue;
}
}
Instead of a constant how can I use a variable here?我如何在这里使用变量而不是常量? Is it possible at all?有可能吗?
No, it is not possible - at least not using the annotation.不,这是不可能的 - 至少不使用注释。 Can you say more about why you need to do that?你能说更多为什么你需要这样做吗? Maybe I can then suggest some alternative approach.也许我可以建议一些替代方法。
Not sure if this is what you were looking for but did you tried to make the default value final?不确定这是否是您要找的,但您是否尝试将默认值设为最终值?
final String defaultValue = "Default";
It works for me.这个对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.