我使用Dropwizard(V1.0.5),我想创建一个客户端连接接收一个多后服务后的Web服务。但是,我获得了“错误请求”响应。客户端和使用dropwizard
在这里,在我的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);
final Client client = new JerseyClientBuilder(environment).using(configuration.getJerseyClientConfiguration()).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;
}
}
我希望能够通过http://localhost:8080/client/test做出GET和接收的http://localhost:8080/testpost的答复中说,在这种情况下,将“酒吧”。但是我收到“HTTP 400错误请求”。
我在做什么错?
我怎么能考后服务?因为我正在使用Firefox插件,所以使用此多部分内容的HttpRequester:
--l3iPy71otz
Content-Disposition: form-data; name="foo"
bar
--l3iPy71otz--
...并且我得到相同的响应。
你看到任何异常检索?是否有任何消息与答复一致。没有更多的信息很难说问题是什么。代码看起来不错 –
Header'Content-Disposition'后面缺少空行。尝试'卷曲-F“富=酒吧”本地主机:8080' –
@peeskillet我没有得到任何异常,我认为它甚至没有进入功能testPost因为有些问题多部分的格式,但我不不知道该做什么了! – impulso