开发者

How can I specify a Default value dynamically in Jersey?

开发者 https://www.devze.com 2023-04-12 13:25 出处:网络
I am using Java Jersey library to create RESTful web service. 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 f

I am using Java Jersey library to create RESTful web service.

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.Produc开发者_JAVA百科es; 
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.

0

精彩评论

暂无评论...
验证码 换一张
取 消