球衣2.x中的Multipart:Java客户端找不到内容类型multipart / form-data类型的作家

时间:2019-07-08 16:12:01

标签: java maven jersey-2.0 jersey-1.0

我有一个具有 jersey 1.x 依赖项的项目。我决定将其升级到 jersey 2.x ,并更改了所有与jersey 1.x相关的依赖项。

没有得到特定的错误,无论我进行什么更改,我都会收到由以下原因引起的javax.ws.rs.ProcessingException消息:

Caused by: javax.ws.rs.ProcessingException: could not find writer for content-type multipart/form-data type: org.glassfish.jersey.media.multipart.MultiPart

完整消息:

Exception in thread "main" javax.ws.rs.ProcessingException: Unable to
invoke request  at
org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:287)
    at
org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:407)
    at
 org.jboss.resteasy.client.jaxrs.internal.ClientInvocationBuilder.post(ClientInvocationBuilder.java:195)
    at
 gr.service.ws0.test.Prot_DocClient.callWs01(Prot_DocClient.java:127)
    at
 gr.service.ws0.test.Prot_DocClient.testNewProtocol(Prot_DocClient.java:63)
    at
 gr.service.ws0.test.Prot_DocClient.main(Prot_DocClient.java:45)
 Caused by: javax.ws.rs.ProcessingException: could not find writer for
 content-type multipart/form-data type:
 org.glassfish.jersey.media.multipart.MultiPart     at
> org.jboss.resteasy.core.interception.ClientWriterInterceptorContext.throwWriterNotFoundException(ClientWriterInterceptorContext.java:40)
>   at
> org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.getWriter(AbstractWriterInterceptorContext.java:138)
    at
 org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.proceed(AbstractWriterInterceptorContext.java:117)
    at
 org.jboss.resteasy.plugins.interceptors.encoding.GZIPEncodingInterceptor.aroundWriteTo(GZIPEncodingInterceptor.java:100)
    at
 org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.proceed(AbstractWriterInterceptorContext.java:122)
    at
 org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.writeRequestBody(ClientInvocation.java:341)
    at
 org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.writeRequestBodyToOutputStream(ApacheHttpClient4Engine.java:558)
    at
org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.buildEntity(ApacheHttpClient4Engine.java:524)
    at
 org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.loadHttpMethod(ApacheHttpClient4Engine.java:423)
    at
 org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:281)
    ... 5 more

错误所指向的代码行,如下所示:

//final ClientConfig config = new ClientConfig();
        final Client client = ClientBuilder.newClient();
        //client.addFilter(new LoggingFilter(System.out));
        final WebTarget target = client.target(urlString);
        MultiPart multiPart = new MultiPart();
         multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
        //multiPart.bodyPart(new BodyPart(inParams.toString(), MediaType.TEXT_PLAIN_TYPE));
        if(mode==CALL_INSERT_DOC){
            //final File fileToUpload = new File(filePath);
            FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file",
                    new File(filePath),
                    MediaType.APPLICATION_OCTET_STREAM_TYPE);
                multiPart.bodyPart(fileDataBodyPart);
            //multiPart.bodyPart(new BodyPart(fileToUpload, MediaType.MULTIPART_FORM_DATA_TYPE));// APPLICATION_OCTET_STREAM_TYPE
        }


        // POST the request
        Response clientResp = target.request(MediaType.APPLICATION_JSON_TYPE)
                .post(Entity.entity(multiPart, multiPart.getMediaType()));
        //final Response clientResp =target.request().post(Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA));

        System.out.println("Response: " + clientResp.getStatus());
        JSONObject response = clientResp.readEntity(JSONObject.class);
        System.out.println(response);

        client.close();

这是我的 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>SERVICE</groupId>
  <artifactId>SERVICE</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>KalamariaWebService</name>
  <description>...</description>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
      <resource>
        <directory>src</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.1</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>
    <dependency>
        <groupId>commons-configuration</groupId>
        <artifactId>commons-configuration</artifactId>
        <version>1.10</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jettison</groupId>
        <artifactId>jettison</artifactId>
        <version>1.4.0</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.6</version>
    </dependency>
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.10</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>2.5.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.3.3</version>
    </dependency>
    <dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId>
    <version>2.29</version>
</dependency>
<dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>2.26</version>
    </dependency>


    <dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-common</artifactId>
    <version>2.29</version>
</dependency>

  </dependencies>
</project>

我有不同的可能解决方案,但我无法弄清楚到底是什么问题。

依赖关系之间是否存在某种冲突?缺少什么吗?

我想知道球衣2 .x是否可能不完全支持multipart(至少不是我的理解方式),因为我找不到任何文档。

甚至寻找潜在客户或地点也会很有帮助。

0 个答案:

没有答案