문제

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>

도움이 되었습니까?

해결책

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));
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top