RESTEasy - 如何设置代理类的表示

时间:2012-07-18 10:46:30

标签: java rest jax-rs resteasy

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

1 个答案:

答案 0 :(得分: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);