加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 没有为MultipartBody,multipart / form-data找到消息正

发布时间:2020-12-15 02:10:16 所属栏目:Java 来源:网络整理
导读:我通过CXF客户端调用REST URL来上传xml文件: WebClient webClient = WebClient.create("some base uri") .header("Authorization",createAuthorizationHeader);webClient.encoding("UTF-8");webClient.type(MediaType.MULTIPART_FORM_DATA);ContentDisposit
我通过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: No message body writer has been found for class 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);

我仍然得到同样的例外

解决方法

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

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",cd);
            Response response = webClient.post(new MultipartBody(att));
            System.out.println(response.readEntity(String.class));
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

}

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读