javax.ws.rs.ProcessingException:RESTEASY004655:无法调用请求

时间:2019-02-07 11:35:41

标签: java rest jax-rs resteasy

我正在尝试作为客户端使用 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

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

谢谢你们。

1 个答案:

答案 0 :(得分:2)

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

const router = express();
const bodyParser = require('body-parser');

router.use(bodyParser.json())
.use(bodyParser.urlencoded({
    extended: true
}));

您需要具有resteasy-multipart-provider才能使其正常工作:

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