简体   繁体   中英

Jersy Rest client to Apache CXF client Conversion

I need to consume upload file API. I have created client using Jersy and it gives me proper response. Here is that sample code:

final Client client = ClientBuilder.newBuilder().register(MultiPartFeature.class).build();
FileDataBodyPart filePart = new FileDataBodyPart("file", new File("somePath/fileName.txt"));
FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
FormDataMultiPart multipart = (FormDataMultiPart) formDataMultiPart.field("someField", "001").bodyPart(filePart);
final WebTarget target = client.target("http://serverUrl.com:8000/cq5/uploadApi");
final Response response = target.request().post(Entity.entity(multipart, multipart.getMediaType()));

But in my project I was supposed to use CXF, I tried to achieve the same thing with CXF. This is what I have tried.

String path = "/uploadApi";
WebClient topicWebClient = WebClient.fromClient(webClient, true)
        .type(MediaType.MULTIPART_FORM_DATA).path(path);
ContentDisposition cd = new ContentDisposition("attachment;filename=fileName.txt");
Attachment att = new Attachment("file", new ByteArrayInputStream("testContent".getBytes()), cd);
final Response response = topicWebClient.post(att);

But here I am not getting any response. Its keep on loading. Not getting any error even in my logs also.

Anything am I missing? Please help me to get proper response.

[Answer in comments]

The server is responding to the CXF client a 401-Unauthorized, so it is an authentication issue. After comparing with Jersey client, it is needed to add in request headers the credentials required by server.

These fields are not in the headers of CXF, so probably it is the reason. Add them when you configure the CXF WebClient.

webClient.header(name,value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM