簡體   English   中英

在DropWizard中使用REST API

[英]Consuming a REST API in DropWizard

我正在使用DropWizard在java中構建API。 但是,對於某些資源,我還需要使用其他RESTful API。 這些其他API不需要任何身份驗證。

DropWizard可以用來使用API​​嗎? 或者在Java應用程序中簡單地使用RESTful API有哪些其他方法? 因為我正在使用DropWizard,我已經擁有了Jackson。

因此,如果REST API是這樣的:

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

我想要一個像List<Foo>這樣的對象

我想你可以使用DropWizard的Jersey客戶端。 根據文檔,它完全符合您的要求。

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

即:

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

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

然后,在服務的run方法中,創建一個新的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 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM