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

时间:2016-07-20 13:44:56

标签: java web-services rest jersey cxf

我需要使用上传文件API。我使用Jersy创建了客户端,它给了我适当的响应。以下是示例代码:

/// The total distance travelled by the bicycle, in meters.
private(set) var distanceTravelled: Double

/**
    Initializes a new bicycle with the provided parts and specifications.

    - Parameters:
        - style: The style of the bicycle
        - gearing: The gearing of the bicycle
        - handlebar: The handlebar of the bicycle
        - frameSize: The frame size of the bicycle, in centimeters

    - Returns: A beautiful, brand-new bicycle, custom built
      just for you.
*/
init(style: Style, gearing: Gearing, handlebar: Handlebar, frameSize centimeters: Int) {
    self.style = style
    self.gearing = gearing
    self.handlebar = handlebar
    self.frameSize = centimeters

    self.numberOfTrips = 0
    self.distanceTravelled = 0
}

但在我的项目中我应该使用CXF,我试图用CXF实现同样的目的。这就是我的尝试。

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()));

但在这里我没有得到任何回应。它继续加载。即使在我的日志中也没有出现任何错误。

我遗失了什么?请帮助我得到适当的回应。

1 个答案:

答案 0 :(得分:1)

[评论中的答案]

服务器正在响应CXF客户端401-Unauthorized,因此这是一个身份验证问题。与Jersey客户端进行比较后,需要在请求标头中添加服务器所需的凭据。

这些字段不在CXF的标题中,所以可能是原因。在配置CXF WebClient时添加它们。

webClient.header(name,value)
相关问题