1

我用 RESTEasy 设置了一个客户端代理类:

 ResourceIF resource = ProxyFactory.create(resourceIF.class, PATH, clientExecutor);

当我调用

 ClientResponse res = (ClientResponse) resource.getObject();

我的界面如下所示:

 @GET
 @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
 public Response getObject()

我自动得到一个 XML 表示。如何将其设置为 JSON?仅仅是声明一个新的仅支持 JSON 的接口吗?

4

1 回答 1

0

您必须添加一个 MediaTypeInterceptor,如下所示:

    ClientExecutor executor=ClientRequest.getDefaultExecutor();
    ResteasyProviderFactory factory=ResteasyProviderFactory.getInstance();
    InterceptorRegistry<ClientExecutionInterceptor> registry=factory.
        getClientExecutionInterceptorRegistry();
    registry.register(new MediaTypeInterceptor("application/json"));
    ResourceIF resource = ProxyFactory.create(ResourceIF.class,PATH,executor,factory);
于 2013-07-02T21:06:33.477 回答