/java/

JAX-RS DefaultValue and QueryParam Annotation

2015-08-04 20:03:59

Here you can find, how to work with DefaultValue and QueryParam. Example controller use parameters: int, string, custom object and enum. More in docs MyCtrl

package pl.btbw.ctrl;

import pl.btbw.ArticleType;
import pl.btbw.Content;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;

@Path("/")
public class MyCtrl {

    @GET
    public String queryParamTest(
            @DefaultValue("-1") @QueryParam("id") int id,
            @DefaultValue("lorem") @QueryParam("title") String title,
            @DefaultValue("post") @QueryParam("content") Content content,
            @DefaultValue("ARTICLE") @QueryParam("type") ArticleType type
    ) {

        String text = "";
        text += "  
@DefaultValue('-1') @QueryParam('id'): " + id;
        text += "  
@DefaultValue('lorem') @QueryParam('title'): " + title;
        text += "  
@DefaultValue('post') @QueryParam('content'): " + content.getName();
        text += "  
@DefaultValue('ARTICLE') @QueryParam('type'): " + type;

        return text;
    }

}

ArticleType

package pl.btbw;

public enum ArticleType {
    ARTICLE, POST, BLOG, COMMENT
}

Content

package pl.btbw;

public class Content {

    private String name;

    public Content(String name) {
        this.name = name;
    }

    public String getName() {
        return " :" + name;
    }
}