使用 dropwizard 的客户端和 post 网络服务

A client and a post web service using dropwizard

我正在使用 Dropwizard (v1.0.5),我想创建一个客户端来连接接收多部分的 post 服务。但是我得到了 "Bad Request" 响应。

这里是我的 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;
    }
}

现在,post 网络服务 (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 and receive the response of http://localhost:8080/testpost 进行 GET,在本例中为 "bar"。但是我得到 "HTTP 400 Bad Request".

我做错了什么?

如何测试 post 服务?因为我使用的是 firefox 插件,所以 HttpRequester 使用了这个多部分内容:

--l3iPy71otz
Content-Disposition: form-data; name="foo"
bar
--l3iPy71otz--

...我得到了相同的回复。

我在 https://github.com/dropwizard/dropwizard/issues/1094 中找到了问题的解决方案。似乎应该为 Jersey 客户端中的请求禁用分块编码以使用 MIME Multipart。

所以我的 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());       
    }
}

而且效果很好。可以在https://github.com/esparig/dwclient2post

中检索代码