简体   繁体   English

Jersy Rest客户端到Apache CXF客户端的转换

[英]Jersy Rest client to Apache CXF client Conversion

I need to consume upload file API. 我需要使用上传文件API。 I have created client using Jersy and it gives me proper response. 我已经使用Jersy创建了客户端,它给了我适当的响应。 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. 但是在我的项目中,我应该使用CXF,但我尝试使用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. 服务器正在向CXF客户端响应401-未授权,因此这是身份验证问题。 After comparing with Jersey client, it is needed to add in request headers the credentials required by server. 与Jersey客户端进行比较之后,需要在请求标头中添加服务器所需的凭据。

These fields are not in the headers of CXF, so probably it is the reason. 这些字段不在CXF的标头中,因此可能是原因。 Add them when you configure the CXF WebClient. 配置CXF WebClient时添加它们。

webClient.header(name,value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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