使用dropwizard的客户端和post web服务
我正在使用Dropwizard(v1.0.5),我想创建一个客户端来连接接收多部分的post服务。但是,我得到了一个“坏请求”响应 这里是my pom.xml中的依赖项使用dropwizard的客户端和post web服务,post,jersey,client,dropwizard,multipart,Post,Jersey,Client,Dropwizard,Multipart,我正在使用Dropwizard(v1.0.5),我想创建一个客户端来连接接收多部分的post服务。但是,我得到了一个“坏请求”响应 这里是my pom.xml中的依赖项 <dependencies> <dependency> <groupId>io.dropwizard</groupId> <artifactId>dropwizard-core</artifactId>
<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>
现在是PostWeb服务(MyPostResource.java):
最后是客户端(Client2Post.java):
我希望能够打个通关,并收到回复,在这种情况下,将是“酒吧”。然而,我得到了“HTTP 400错误请求”
我做错了什么
如何测试邮政服务?因为我使用的是firefox插件,HttpRequester使用以下多部分内容:
--l3iPy71otz
Content-Disposition: form-data; name="foo"
bar
--l3iPy71otz--
…我得到了同样的回答。我在中找到了解决问题的方法。Jersey客户端中的请求要使用MIME多部分,似乎应该禁用分块编码 因此,我的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); //This line is new!!!
final Client client = new JerseyClientBuilder(environment).using(conf).build(getName());
environment.jersey().register(new Client2Post(client));
environment.jersey().register(new MyPostResource());
}
}
public类Client2PostApplication扩展应用程序{
公共静态void main(字符串[]args)引发异常{
新建Client2PostApplication().run(args);
}
@凌驾
公共无效初始化(引导引导引导){
addBundle(新的MultiPartBundle());
}
@凌驾
公共无效运行(Client2PostConfiguration配置,
环境)引发异常{
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(newclient2post(client));
environment.jersey().register(新MyPostResource());
}
}
而且它工作得很有针对性。可以在中检索代码您是否看到任何异常?回复中是否有任何信息。如果没有更多的信息,很难说问题出在哪里。代码看起来很正常,标题
内容处理后缺少空行。尝试curl-F“foo=bar”localhost:8080
@peeskillet我没有任何异常,我认为它甚至没有进入函数testPost,因为多部分的格式有一些问题,但我不知道该怎么做了@Ôrel执行curl的调用效果很好!我得到了一个肯定的回答,内容是:“酒吧”。所以现在,我不明白为什么使用HttpRequester不起作用(甚至插入一个空行),更重要的是,为什么我不能让我的客户端工作。。。我错过了什么?很好,服务器正在工作。你能做一个tcpdump来看看curl和客户机之间的区别吗?
@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;
}
}
@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;
}
}
--l3iPy71otz
Content-Disposition: form-data; name="foo"
bar
--l3iPy71otz--
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); //This line is new!!!
final Client client = new JerseyClientBuilder(environment).using(conf).build(getName());
environment.jersey().register(new Client2Post(client));
environment.jersey().register(new MyPostResource());
}
}