我使用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的接口吗?

最佳答案

您必须添加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);

10-04 19:00