javax.ws.rs.ProcessingException: RESTEASY004655: Unable to invoke request

被刻印的时光 ゝ 提交于 2021-01-01 03:58:55

问题


I'm trying, as a client, to post a zip file to a "Restservice", using RestEasy 3.0.19. This is the code:

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

The error I get is this one:

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

How can I solve this problem? I looked into some posts, but the code is slightly different from mine.

Thank you guys.


回答1:


You are mixing two different HTTP clients RESTEasy and Apache HttpClient. Here is the code which uses RESTEasy only

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

You need to have resteasy-multipart-provider to have it working:

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


来源:https://stackoverflow.com/questions/54572476/javax-ws-rs-processingexception-resteasy004655-unable-to-invoke-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!