Search code examples
javarestrestlet

Restlet service with multiple post resources


I have a restlet application, apart of this i have two post service with slimier except tail of end point.

Example

POST /example.com/api/myservice.json and POST /example.com/api/myservice2.json

I have written both resources's business in separate file .Now i have a doubt that is it possible to write both business in same file .Thanks in advance


Solution

  • You need to follow the way Restlet organize applications. I mean resources are attached on one or several paths. Within resources, you can define methods to serve requests, as summarize below:

    • resource attached to a path
      • method GET (for example)
      • method POST (for example)

    If your processing is exactly the same for two paths, you can attach the same resource class for these two paths. Something like that:

    Router router = new Router(getContext());
    router.attach("/api/myservice", MyServerResource.class);
    router.attach("/api/myservice2", MyServerResource.class);
    

    You can notice that the extension can be managed by the tunnel service of Restlet. See getTunnelService().setExtensionsTunnel(true) within the application.

    Edited

    In contrary, if you want to gather some processing into a single entity, there are two options:

    • With Restlet server resources, you can't. The only possible thing is to define a class containing all your processing and reference / use it from different server resources.
    • You can consider to use the JAX-RS support of Restlet. This allows to define within a same class several REST endpoints, as described below. This link could give you more details: http://restlet.com/technical-resources/restlet-framework/guide/2.2/extensions/jaxrs.

      public class MyResource {
          @POST
          @Path("/api/myservice")
          public SomeObject1 handleRequest1() {
              (...)
          }
      
          @POST
          @Path("/api/myservice2")
          public SomeObject2 handleRequest2() {
              (...)
          }
      }
      

    Hope it helps you, Thierry