Frage

I'm building an API in java using DropWizard. However, for certain resources I also need to consume other RESTful API's. These other API's do not require any authentication.

Can DropWizard be used to consume API's? Or what are some other ways to simply consume a RESTful API in a java application? Since I'm using DropWizard I already have Jackson.

So if the REST API is something like this:

[ {"id": "0",
   "name" : "Joe"
]

I'd like to have an object like this List<Foo>

War es hilfreich?

Lösung

I suppose you can use a DropWizard's Jersey Client. According to the documentation, it does exactly what you are looking for.

http://www.dropwizard.io/1.0.3/docs/manual/client.html

I.e.:

public class ExampleConfiguration extends Configuration {
    @Valid
    @NotNull
    private JerseyClientConfiguration jerseyClient = new JerseyClientConfiguration();

    @JsonProperty("jerseyClient")
    public JerseyClientConfiguration getJerseyClientConfiguration() {
        return jerseyClient;
    }
}

Then, in your service’s run method, create a new JerseyClientBuilder:

@Override
public void run(ExampleConfiguration config,
                Environment environment) {

    final Client client = new JerseyClientBuilder(environment).using(config.getJerseyClientConfiguration())
                                                              .build(getName());
    environment.jersey().register(new ExternalServiceResource(client));
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top