如何覆盖球衣客户端中的响应标头球衣、客户端

2023-09-06 15:03:39 作者:阳光刺破心脏

我有一个 jersey 客户端,我正试图解组一个响应实体.问题是远程 Web 服务将 application/octet-stream 作为内容类型发回,所以 Jersey 不知道如何解组它(对于 XML 等返回的 text/html 有类似的错误).我无法更改网络服务.

I have a jersey client that I am trying to unmarshall a response entity with. The problem is the remote web service sends back application/octet-stream as the content type so Jersey does not know how to unmarshall it (I have similar errors with text/html coming back for XML and such). I cannot change the web service.

我想要做的是覆盖内容类型并将其更改为 application/json 以便 jersey 知道要使用哪个编组器.

What I want to do is override the content-type and change it to application/json so jersey will know which marshaller to use.

我无法向 json 编组器注册 application/octet-stream,因为对于给定的内容类型,我实际上可能会得到各种奇怪的东西.

I cannot register application/octet-stream with the json marshaller as for a given content type I actually might be getting back all kinds of oddities.

推荐答案

正如 laz 所指出的,ClientFilter 是要走的路:

As laz pointed out, ClientFilter is the way to go:

client.addFilter(new ClientFilter() {
    @Override
    public ClientResponse handle(ClientRequest request) throws ClientHandlerException {
        request.getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, "application/json");
        return getNext().handle(request);
    }
});