gpt4 book ai didi

java - 未找到 MultipartBody、multipart/form-data 的消息正文编写器

转载 作者:搜寻专家 更新时间:2023-11-01 03:49:17 24 4
gpt4 key购买 nike

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

}

}

请分享它失败的地方,服务器端还是客户端?以进一步查明错误。以及您正在使用的 cxf JAXRS 版本

关于java - 未找到 MultipartBody、multipart/form-data 的消息正文编写器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32862848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com