是否可以将 Jersey JSP 模板响应重新路由到 InputStream?
总之,
我正在使用 Java/Jersey 1.9 创建一个生成 XML 的 Web 服务。我使用 JSP 模板(显式地通过 Viewable 类)生成 XML。有什么方法可以将 JSP 结果重新路由到本地输入流以进行进一步处理吗?目前,我实际上是从不同的方法调用我自己的 XML Web 服务作为 http 环回 (localhost)。
感谢您的任何见解,
伊恩
@GET @Path("kml")
@Produces("application/vnd.google-earth.kml+xml")
public Viewable getKml(
@QueryParam("lat") double lat,
@QueryParam("lon") double lon,
@QueryParam("alt") double alt) {
overflights = new SatelliteOverflightModel(
context, new SatelliteOverflightModel.Params(lat, lon, alt)
).getOverflights();
return new Viewable("kml", this);
}
@GET @Path("kmz")
@Produces("application/vnd.google-earth.kmz")
public InputStream getKmz(@Context UriInfo uriInfo,
@QueryParam("lat") double lat,
@QueryParam("lon") double lon,
@QueryParam("alt") double alt)
throws IOException {
Client client = Client.create();
WebResource webr =
client.resource(uriInfo.getBaseUri()+"overflights/kml");
InputStream result =
webr.queryParams(uriInfo.getQueryParameters()).get(InputStream.class);
// Do something with result; e.g., add to ZIP archive and return
return result;
}
All,
I am using Java/Jersey 1.9 to create a web service that generates XML. I'm generating XML using a JSP template (explicitly via the Viewable class). Is there any way to reroute the JSP results to a local InputStream for further processing? At the moment I'm actually calling my own XML web service as an http loopback (localhost) from a different method.
Thanks for any insights,
Ian
@GET @Path("kml")
@Produces("application/vnd.google-earth.kml+xml")
public Viewable getKml(
@QueryParam("lat") double lat,
@QueryParam("lon") double lon,
@QueryParam("alt") double alt) {
overflights = new SatelliteOverflightModel(
context, new SatelliteOverflightModel.Params(lat, lon, alt)
).getOverflights();
return new Viewable("kml", this);
}
@GET @Path("kmz")
@Produces("application/vnd.google-earth.kmz")
public InputStream getKmz(@Context UriInfo uriInfo,
@QueryParam("lat") double lat,
@QueryParam("lon") double lon,
@QueryParam("alt") double alt)
throws IOException {
Client client = Client.create();
WebResource webr =
client.resource(uriInfo.getBaseUri()+"overflights/kml");
InputStream result =
webr.queryParams(uriInfo.getQueryParameters()).get(InputStream.class);
// Do something with result; e.g., add to ZIP archive and return
return result;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以考虑使用 ContainerResponseFilter 而不是资源 - 请参阅例如 Gzip 过滤器 Jersey 提供。不同之处在于您的过滤器将依赖于 Accept 和 Content-Type 标头,而不是 Accept-Encoding 和 Content-Encoding 标头(就像 gzip 过滤器那样)。
如果您坚持使用该资源,您可以在您的资源上注入 Providers 接口,找到正确的 MessageBodyWritter 并调用其写入方法:
免责声明:这不是我的想法 - 未经测试:)
You can consider using a ContainerResponseFilter for this instead of a resource - see e.g. the Gzip filter Jersey provides. The difference would be that your filter would depend on Accept and Content-Type headers instead of Accept-Encoding and Content-Encoding headers (like the gzip filter does).
If you insist on using the resource, you can inject Providers interface on your resource, find the right MessageBodyWritter and call the write method on it:
DISCLAIMER: This is off the top of my head - untested :)