Java – no message body writer found for multipart body, multipart / form data
•
Java
I upload XML files by calling rest URL through CXF client:
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));
But I encountered an exception in the post call
I try to add a provider:
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);
I still get the same exception
Solution
I tested the configuration you work here. It's the whole test file
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(); } } }
Please share its failure, server or client location? Further identify errors And the CXF jaxrs version you are using
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码