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