Jersey客户端API WebResource accept()没有正确设置MIME头? [英] Jersey client API WebResource accept() not setting MIME header correctly?

查看:764
本文介绍了Jersey客户端API WebResource accept()没有正确设置MIME头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

服务器端:

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

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

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.

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

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

推荐答案

大卫,我明白了。你做了同样的事情......

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

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

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

一旦我改变了我的代码:

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

一切正常,正确的'Accept'标题已发送到服务器。

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

希望有所帮助。

这篇关于Jersey客户端API WebResource accept()没有正确设置MIME头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆