没有为MultipartBody,multipart / form-data找到消息正文编写器

时间:2015-09-30 09:41:42

标签: java web-services rest jax-rs cxf

我通过CXF客户端调用REST URL来上传xml文件:

WebClient webClient = WebClient.create("some base uri")
                                .header("Authorization",createAuthorizationHeader);
webClient.encoding("UTF-8");
webClient.type(MediaType.MULTIPART_FORM_DATA);
ContentDisposition cd = new ContentDisposition("attachment;filename=abc.xml");
Attachment att = new Attachment("root", stream, cd);
Response response = webClient.post(new MultipartBody(att));

但我在POST电话

时遇到异常
  

javax.ws.rs.ProcessingException:找不到类org.apache.cxf.jaxrs.ext.multipart.MultipartBody,ContentType:multipart / form-data

的消息正文编写器

我尝试添加提供商:

List providers = new ArrayList();
providers.add(new org.codehaus.jackson.jaxrs.JacksonJsonProvider());
providers.add(new org.apache.cxf.jaxrs.provider.MultipartProvider());
WebClient webClient = WebClient.create(constant.getUploadURI(),providers)
                               .header("Authorization",createAuthorizationHeader);

我仍然得到同样的例外

1 个答案:

答案 0 :(得分:0)

我测试了你在这里工作的配置是整个测试文件

import java.io.FileInputStream;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.cxf.jaxrs.ext.multipart.Attachment;
import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition;
import org.apache.cxf.jaxrs.ext.multipart.MultipartBody;
import org.junit.Test;

public class KPUploadTest {

    @Test
    public void testUpload() {

        try (FileInputStream stream = new FileInputStream("d:\\uploadtest.txt");) {
            WebClient webClient = WebClient
                    .create("http://localhost:8080/api/kp/rest/upload");
            webClient.encoding("UTF-8");
            webClient.type(MediaType.MULTIPART_FORM_DATA);
            ContentDisposition cd = new ContentDisposition(
                    "attachment;filename=abc.xml");
            Attachment att = new Attachment("root", stream, cd);
            Response response = webClient.post(new MultipartBody(att));
            System.out.println(response.readEntity(String.class));
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

}

请分享其失败,服务器端或客户端的位置?进一步查明错误。以及您正在使用的cxf JAXRS版本