我正在使用 RESTEasy,更具体地说,是他们框架的客户端。
我正在调用第三方 Web 服务,它会返回一些 JSON 代码。
但是,出于某些充分的原因,他们响应中的内容类型是“text/javascript”。
我如何告诉 RESTEasy 它应该使用 JSON 提供程序(解组目的)来实现“text/javascript”内容类型?
那可能吗 ?
我的代码:
public interface XClient {
@GET
@Produces("application/json")
@Path("/api/x.json")
public Movie getMovieInformation(
@QueryParam("q") String title);
}
解决方案可能是什么样的:
public interface XClient {
@GET
@Produces("text/javascript")
// Tell somehow to use json provider despite the produces annotation
@Path("/api/x.json")
public Movie getMovieInformation(
@QueryParam("q") String title);
}
我通过使用替换传入内容类型的拦截器来解决,如下所示:
this.requestFactory.getSuffixInterceptors().registerInterceptor(
new MediaTypeInterceptor());
static class MediaTypeInterceptor implements ClientExecutionInterceptor {
@Override
public ClientResponse execute(ClientExecutionContext ctx) throws Exception {
ClientResponse response = ctx.proceed();
String contentType = (String) response.getHeaders().getFirst("Content-Type");
if (contentType.startsWith("text/javascript")) {
response.getHeaders().putSingle("Content-Type", "application/json");
}
return response;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)