ElM*_*i25 2 jersey dropwizard microservices
Dropwizard 官方文档 jersey客户端无法测试,有人拥有dropwizard jersey客户端样本吗?
小智 5
我发现在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>
Run Code Online (Sandbox Code Playgroud)
这是我的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());
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的配置(Client2PostConfiguration.java):
public class Client2PostConfiguration extends Configuration {
@Valid
@NotNull
private JerseyClientConfiguration jerseyClient = new JerseyClientConfiguration();
@JsonProperty("jerseyClient")
public JerseyClientConfiguration getJerseyClientConfiguration() {
return jerseyClient;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,发布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;
}
}
Run Code Online (Sandbox Code Playgroud)
最后,客户端(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;
}
}
Run Code Online (Sandbox Code Playgroud)
完整的源代码可以从这里下载。
| 归档时间: |
|
| 查看次数: |
3344 次 |
| 最近记录: |