Some content

@Web[REST] Annotations (since 2.0.0)

@Web[REST]

A @Web[REST] Methods is a methods annotated with @WebGet, @WebPost, @WebPut, @WebDelete. Snow will bind the matching HTTP request to this annotated method as

@Web[Get|Post|Put|Delete]([path])
public [ReturnName] methodName(/* ... dynamic parameters (see WebParams) ... */){
  
}
  • path is optional. If not present, the path will be the "/[methodName]".

    So, a HTTP-GET on the URI "/getSomething" would match to

    @WebGet
    public Object getSomething(/* ... dynamic parameters (see WebParams) ... */){
    
  • path can have {varName} to define some @PathVar.

    So, the URI "/delete-item-123" would match to

    @WebPost("/delete-item-{id}")
    public Object aRestGet(@PathVar("id")Long id){
      // id == 123
    }
    
  • The method parameters is dynamic, see WebParam for more information. In short, you can use @WebParam and @PathVar and even define your custom param resolvers, and none of those match, Snow will attempt to resolve the parameter with Google Guice (allowing you to inject Guice bound object as well)
  • ReturnType is optional as well and can be void.

    So, a HTTP-POST on "/create-item" will return an empty body.

    @WebPost("/create-item")
    public void createAnItem(@WebParam("title")String title)...
    
  • The return ResultType instance will be serialized to JSON with the default JsonRenderer.

    So, a HTTP-GET on "/item?id=123" will get the JSON for the item.

    @WebPost("/item")
    public Item getAnItem(@WebParam("id")Long itemId){
      return itemManager.getItem(itemId);
    }
    

    Would return a json like.

    {id: 123,
     title: "A very interesting title"}
    
  • If ReturnType is of type String, then, it is assumed that the String is the JSON.

    So, a HTTP-GET on "/items-count".

    @WebGet("/items-count")
    public String getItemsCount(){
      return "{\"count\":1001}";
    }