java Jersey 客户端 API WebResource accept() 未正确设置 MIME 标头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6984193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:09:21  来源:igfitidea点击:

Jersey client API WebResource accept() not setting MIME header correctly?

javajersey

提问by David Webster

public static WebResource createWebResource()
{
    final ClientConfig  cc = new DefaultClientConfig();
    final Client        c = Client.create(cc);
    final WebResource   wr = c.resource("http://localhost:19801/wtg_inventory_war/wtg/rest")
                                  .path(inv);
    return wr;
}

public void tester()
{
final WebResource  wr = JaxrsClientUtil.createWebResource()
                                 .path("wtg-service");

    wr.accept(MediaType.APPLICATION_XML);

String   response = wr.path("get-services")
                          .type(MediaType.APPLICATION_XML)
                          .get(String.class);
    System.out.println(response);
}

Server side:

服务器端:

@Path("get-services")
@GET
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response handleFindInventoryServices(
@Context WtgSpringContainer     ioc  // Spring config for service operations
)
{
    System.out.println("Got a service listing request...");
    LOGGER.info("Got a service listing request");

    Get the app specific data formatted in JAXB XML or JSON...

    .
    .
    .


    return Response.ok(msg).build();
}

Regardless of what the client side sets for acceptable media type, JSON comes back? Using curl with -HAccept:application/json or application/xml works fine. I'd like to test my server with both without changing the server side.

不管客户端为可接受的媒体类型设置了什么,JSON 会回来吗?将 curl 与 -HAccept:application/json 或 application/xml 一起使用可以正常工作。我想在不更改服务器端的情况下测试我的服务器。

Any pointers as to why I cannot force the server to XML as my preferred MIME type?

关于为什么我不能强制服务器将 XML 作为我首选的 MIME 类型的任何指示?

回答by Riyad Kalla

David, I figured it out. You did the same thing I did...

大卫,我想通了。你做的和我做的一样...

WebResource.accept(..)is a static method and is actually returning a WebResource.Builderinstance to you that we were both ignoring, with the correct accept param set on.

WebResource.accept(..)是一个静态方法,实际上返回一个我们都忽略的WebResource.Builder实例,并设置了正确的接受参数。

Once I changed my code from:

一旦我改变了我的代码:

WebResource res = c.resource("http://localhost:5984/");
res.accept(MediaType.APPLICATION_JSON_TYPE);
System.out.println(res.get(String.class));

to:

到:

WebResource res = c.resource("http://localhost:5984/");
Builder builder = res.accept(MediaType.APPLICATION_JSON_TYPE);
System.out.println(builder.get(String.class));

Everything started working, the correct 'Accept' headers got sent to the server.

一切开始工作,正确的“接受”标头被发送到服务器。

Hope that helps.

希望有帮助。

回答by DME

I did run in the same issue as you guys.

我确实和你们遇到了同样的问题。

It works fine with using the accept header property.

使用 accept 标头属性可以正常工作。

An other interessting approach I figured out is to set the content type with the response builder as following:

我想出的另一种有趣的方法是使用响应构建器设置内容类型,如下所示:

     return Response.status(Status.OK).entity(dto).type(request.getContentType()).build();

If you do that you can use both the acceptand the content-typeproperty.

如果这样做,您可以同时使用acceptcontent-type属性。