2017-05-31 8 views
0

Dropwizard(v1.0.5)を使用しています。マルチパートを受け取るポストサービスに接続するクライアントを作成します。しかし、私は "悪い要求"の応答を取得します。dropwizardを使用するクライアントとポストWebサービス

ここにここに私の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 Bad Request"を取得しています。

私は間違っていますか?

投稿サービスをテストするにはどうすればよいですか?私はfirefoxアドオンを使用しているので、このmultipartコンテンツを使用するHttpRequester:

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

...私は同じ応答を得ます。

+0

例外はありますか?レスポンスを送るメッセージはありますか?それ以上の情報なしに問題が何であるか言うのは難しいです。コードはOKのようです。 –

+0

ヘッダー 'Content-Disposition'の後に空白行がありません。 '' curl -F "foo = bar" localhost:8080'を試してください –

+0

@peeskillet何も例外はありません。私はmultipartのフォーマットにいくつかの問題があるのでtestPost関数を入力しないと思います。もう何をすべきか知っているよ! – impulso

答えて

0

https://github.com/dropwizard/dropwizard/issues/1094に私の問題の解決策が見つかりました。 Jersey ClientでMIME Multipartを使用するためのリクエストでは、チャンクエンコーディングを無効にする必要があります。

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());  
    } 
} 

そして、それはpertectly作品:

だから私のClient2PostApplication.javaは今です。コードを取得できますhttps://github.com/esparig/dwclient2post