简体   繁体   English

如何在 Jersey 中动态指定默认值?

[英]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.

相关问题 如何从jersey的属性文件中指定路径值? - How can i specify path value from property file in jersey? 如何为appengine上的JDO实体中的字段指定默认值? - How can I specify a default value for a field in a JDO entity on appengine? 如何干净地覆盖Jersey使用的默认ServiceLocator? - How can I cleanly override the default ServiceLocator used by Jersey? 如何在Java Web应用程序中为系统属性指定默认值? - How can I specify a default value for a system property in a Java web app? 如何为 JSON 作业动态指定 ItemProcessor? - How can I specify dynamically the ItemProcessor for a JSON Job? 我可以让Jersey在全局/默认情况下使用自然JSON表示法吗? - Can I get Jersey to use natural JSON notation globally/as default? 如何指定嵌套属性的默认值? - How to specify the default value for nested property? PreparedStatement - 如何指定使用列的默认值 - PreparedStatement - how specify to use default value of column 如何从属性文件初始化Jersey查询参数的默认值 - How to initialized the default value of a Jersey query parameter from properties file 如何使用带参数化的注释指定方法,并通过@Pointcut指定其值 - How can I specify method with an parameterized annotation and its value with @Pointcut
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM