Skip to content

Instantly share code, notes, and snippets.

@danbev
Created October 23, 2012 11:13
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save danbev/3938238 to your computer and use it in GitHub Desktop.
Save danbev/3938238 to your computer and use it in GitHub Desktop.
RestEasy Programmatic Configuration

Programmatic endpoint configuration in RestEasy

This section will try to sort out the best way to implement programmatic endpoint configuration. The approach taken is to make as few changes as possible to RestEasy, which is means that when changes were required whole methods have been copied and those new methods changed were possible.

The following branch contains the code discussed in this document: programmatic-config

Lets starting with a very basic POJO like this:

public class Pojo
{
   public String doit()
   {
      return "Pojo done";
   }
   
   public String process(String msg)
   {
      return msg;
   }
}

This class will simulate our the target endpoint and as you can see it has no annotations. Next, step is to figure out how we describe such a class and get RestEasy to be able to handle it.

Building a REST endpoint for the class above could be done in the following manner.
Start by:

ResourceClassConfig config = ResourceConfig.resourceClass(Pojo.class)

Next, using code completion suggestions you should see the following options:

classOptions

In this case we don't require anything except the rootPath:

ResourceClassConfig config = ResourceConfig.resourceClass(Pojo.class)
                      .rootPath("/")

To configure the method doit in the Pojo.class we specify that we want to add a new resource method:

ResourceClassConfig classConfig = ResourceConfig.resourceClass(Pojo.class)
                      .rootPath("/")
                      .resourceMethod().

You should now see the code completion options available for configuring the method:

methodsSuggestions

For this example we'll add the following:

ResourceClassConfig classConfig = ResourceConfig.resourceClass(Pojo.class)
                      .rootPath("/")
                      .resourceMethod()
                          .httpMethods(GET)
                          .path("some/path/doit")
                          .consumes("*/*")
                          .produces("*/*")
                          .name("doit").

methodParamOptions

And to add another method just continue to use the code completion after the returns method call.

The end result would then be:

ResourceClassConfig classConfig = ResourceConfig.resourceClass(Pojo.class)
                      .rootPath("/")
                      .resourceMethod()
                         .httpMethods(GET)
                         .path("some/path/doit")
                         .consumes("*/*")
                         .produces("*/*")
                         .name().
                         .returns(String.class)
                      .resourceMethod()
                         .httpMethods(GET)
                         .name("process")
                         .queryParam(String.class, "msg")
                         .returns(String.class)
                      .build();

The end result of this configuration should be the equivalent JAX-RS annotated class:

@Path ("/")
@Consumes ("*/*")
@Produces ("*/*")
public class Pojo
{
   @GET
   @PATH ("some/path/doit")
   @Consumes ("*/*)
   @Produces ("*/*")
   public String doit()
   {
      return "Pojo done";
   }
   
   @GET
   public String process(@QueryParam("msg") String msg)
   {
      return msg;
   }
}

The full path to the method doit would be "/some/path/doit", and since we have not specified a path for the process method it's path with default to the name of the method, "/process".

Note The type specified in the returns method is currently ignored, and what is returned is whatever the target method returns. This is something that we are currently working on and we'll post back with a suggestion as soon as we have something to show.

We would be interested to hear what people think about this approach. Any feedback is most welcome.

@bbq2100
Copy link

bbq2100 commented Oct 1, 2014

Hi Daniel, IMHO it woud be very nice if your "programmatic-resource-configuration-feature" would come out of the box (pull request) or at least it would be obtainable via a MVN repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment