gpt4 book ai didi

jersey - Dropwizard Jersey 客户端示例

转载 作者:行者123 更新时间:2023-12-01 22:56:39 30 4
gpt4 key购买 nike

Dropwizard official documentation Jersey 客户端不可测试,有人有 dropwizard Jersey 客户端样本吗?

最佳答案

我发现在 Dropwizard 中实现我的客户端也有点挑战。所以我想做出贡献,以防万一有人遇到这个问题。这是 Dropwizard (v1.0.5) 中的客户端,它使用 Multipart 调用 POST Web 服务。通过 Web 服务以及使用 GET 访问客户端。

我的 pom.xml 中的依赖项:

<dependencies>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>${dropwizard.version}</version>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-assets</artifactId>
<version>${dropwizard.version}</version>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-forms</artifactId>
<version>${dropwizard.version}</version>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-client</artifactId>
<version>${dropwizard.version}</version>
</dependency>

</dependencies>

这是我的 Dropwizard 应用程序 (Client2PostApplication.java):

    public class Client2PostApplication extends Application<Client2PostConfiguration> {
public static void main(String[] args) throws Exception {
new Client2PostApplication().run(args);
}

@Override
public void initialize(Bootstrap<Client2PostConfiguration> bootstrap) {
bootstrap.addBundle(new MultiPartBundle());
}

@Override
public void run(Client2PostConfiguration configuration,
Environment environment) throws Exception {

environment.jersey().register(MultiPartFeature.class);
JerseyClientConfiguration conf = configuration.getJerseyClientConfiguration();

conf.setChunkedEncodingEnabled(false);

final Client client = new JerseyClientBuilder(environment).using(conf).build(getName());
environment.jersey().register(new Client2Post(client));
environment.jersey().register(new MyPostResource());
}
}

这是我的配置(Client2PostConfiguration.java):

public class Client2PostConfiguration extends Configuration {

@Valid
@NotNull
private JerseyClientConfiguration jerseyClient = new JerseyClientConfiguration();

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

现在,发布 Web 服务 (MyPostResource.java):

@Path("/testpost")

public class MyPostResource {

public MyPostResource() {

}

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Timed
public String test(
@FormDataParam("foo") String testData) throws IOException {
return testData;
}
}

最后,客户端 (Client2Post.java):

@Produces(MediaType.TEXT_PLAIN)
@Path("/client")
public class Client2Post {

private Client client;

public Client2Post(Client client) {
this.client = client;
}

@GET
@Path("/test")
public String testPost() {

final Invocation.Builder request = client.target("http://localhost:8080/testpost").register(MultiPartFeature.class).request();

final FormDataMultiPart entity = new FormDataMultiPart()
.field("foo", "bar");

final String response = request.post(Entity.entity(entity, entity.getMediaType()), String.class);

return response;

}
}

完整的源代码可以从here下载.

关于jersey - Dropwizard Jersey 客户端示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30800908/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com