【问题标题】:javax.ws.rs.ProcessingException: RESTEASY004655: Unable to invoke requestjavax.ws.rs.ProcessingException:RESTEASY004655:无法调用请求
【发布时间】:2019-07-01 12:27:15
【问题描述】:

作为客户,我正在尝试使用 RestEasy 3.0.19 将 zip 文件发布到“Restservice”。 这是代码:

public void postFileMethod(String URL)  {       
         Response response = null;              
         ResteasyClient client = new ResteasyClientBuilder().build();
         ResteasyWebTarget target = client.target(URL); 

         MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();

         FileBody fileBody = new FileBody(new File("C:/sample/sample.zip"), 
ContentType.MULTIPART_FORM_DATA);              

         entityBuilder.addPart("my_file", fileBody);            
         response = target.request().post(Entity.entity(entityBuilder, 
MediaType.MULTIPART_FORM_DATA));            
}   

我得到的错误是这个:

javax.ws.rs.ProcessingException: RESTEASY004655: Unable to invoke request
....
Caused by: javax.ws.rs.ProcessingException: RESTEASY003215: could not 
find writer for content-type multipart/form-data type: 
org.apache.http.entity.mime.MultipartEntityBuilder

我该如何解决这个问题?我查看了一些帖子,但代码与我的略有不同。

谢谢你们。

【问题讨论】:

    标签: java rest jax-rs resteasy


    【解决方案1】:

    您正在混合使用两个不同的 HTTP 客户端 RESTEasyApache HttpClient。这是仅使用 RESTEasy 的代码

    public void postFileMethod(String URL) throws FileNotFoundException {
        Response response = null;
        ResteasyClient client = new ResteasyClientBuilder().build();
        ResteasyWebTarget target = client.target(URL);
    
        MultipartFormDataOutput mdo = new MultipartFormDataOutput();
        mdo.addFormData("my_file", new FileInputStream(new File("C:/sample/sample.zip")), MediaType.APPLICATION_OCTET_STREAM_TYPE);
        GenericEntity<MultipartFormDataOutput> entity = new GenericEntity<MultipartFormDataOutput>(mdo) { };
    
        response = target.request().post(Entity.entity(entity,
                MediaType.MULTIPART_FORM_DATA));
    }
    

    你需要 resteasy-multipart-provider 让它工作:

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-multipart-provider</artifactId>
        <version>${resteasy.version}</version>
    </dependency>
    

    【讨论】:

    • 我的疑问是:我必须发送一个 zip 文件,而不是使用表单,而是以编程方式从客户端硬盘中获取。我还应该使用“MultipartFormDataOutput”和“MediaType.MULTIPART_FORM_DATA”吗?
    • 这取决于端点的服务器端实现。如果它是使用multipart/form-data 实现的,那么您必须使用MediaType.MULTIPART_FORM_DATA。更多信息here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多