0

Say I have a Dropwizard/Jersey resource defined like so:

// Pseudo-code
@Path("/fizz")
@Produces(MediaType.APPLICATION_JSON)
class FizzResource {
    FizzDao fizzDao

    @GET
    List<Fizz> getFizzesByType(@PathParam("type") String type) {
        // Do some stuff up here
        return fizzDao.getFizzesByType(type)
    }

    @POST
    Widget determineWidgetByFoobaz(@PathParam("foobaz") String foobaz) {
        // Do some stuff

        List<Fizz> fizzes = getFizzesByType(foobaz.type)
        Widget w = new Widget(fizzes, true, blah)
        // Do some more stuff

        return w
    }
}

What happens when I call one endpoint (getFizzesByType) from inside another endpoint (determineWidgetByFoobaz)?

Does the framework know to just make a Java method call? Or is an actual network call (to localhost/loopback/etc.) made? If a network call is made, does the framework provide any way to configure it so that just a local Java method invocation is called instead?

smeeb
  • 27,777
  • 57
  • 250
  • 447
  • Instead of calling your own rest method, you should call your DAO instead. Or you can create a different facade that contains your business logic and the REST endpoint can delegate to. Or if you want to do it that way, I would suggest create a private method in your Endpoint class that can be called by both methods. – pandaadb Mar 29 '16 at 10:53

2 Answers2

2

If you access the endpoint as a method (i.e. this.getFizzesByType(type)) then it will be called like any other Java method. If you access it via a URI (e.g. ClientBuilder.newClient().target("http://localhost/fizz/" + type).request().get()) then it will be accessed as a network resource.

sisyphus
  • 6,174
  • 1
  • 25
  • 35
1

The getFizzesByType call inside determineWidgetByFoobaz is just another local method call. There's nothing special in those methods, and you can also call them safely in, let's say, a unit test.

What Jersey does on it's bootstrapping process is to scan for classes annotated with @Path and then bind each method annotated with an HTTP method to it's endpoint (if any). That way, when someone fires a GET to /fizz, in a nutshell Jersey gets a FizzResource instance, call it's getFizzesByType method, serializes the returned object to JSON, creates the appropriate HTTP response and sends it back to the client.

andrepnh
  • 933
  • 8
  • 21