【问题标题】:redirect to static content using JAX-RS UriBuilder使用 JAX-RS UriBuilder 重定向到静态内容
【发布时间】:2016-06-01 11:39:59
【问题描述】:

我有一个 Jersey 应用程序,我试图重定向到一个 html 页面,当我访问“/apiconsole”端点时显示 api 控制台,如下所示。

/**
 * Redirect to API Console
 *
 * @return API Console
 */
@GET
@Path("/apiconsole")
public Response redirectToApiConsole() {
    //redirect path from baseURI to the api console
    URI redirectedURL = UriBuilder.fromPath("/api/console/index.html").build();
    return Response.seeOther(redirectedURL).build();
}

这与在 Spring 中执行此操作相同吗?

/**
 * Redirect to API Console 
 *
 * @return API Console
 */
@RequestMapping(value = "/apiconsole", method = RequestMethod.GET)
public View redirectToApiConsole() {
    return new RedirectView("/api/console/index.html?raml=/api/console/api.raml");
}

【问题讨论】:

    标签: spring rest jersey jax-rs


    【解决方案1】:

    您必须使用Viewable 来实现这一点,并且必须更新 web.xml 以包含 servlet 过滤器而不是 servlet。我已经提供了答案here 和here。希望对您有所帮助。

    【讨论】:

    • 我收到一个错误o.g.j.m.i.WriterInterceptorExecutor : MessageBodyWriter not found for media type=text/html, type=class org.glassfish.jersey.server.mvc.Viewable, genericType=class org.glassfish.jersey.server.mvc.Viewable.
    • 我添加了以下配置 Jersey Config : property(ServletProperties.FILTER_STATIC_CONTENT_REGEX,"/((api/console/.*))"); customConfig: property(MvcFeature.TEMPLATE_BASE_PATH,"/api/console/"); register(org.glassfish.jersey.server.mvc.MvcFeature.class); 并简单地返回这个 return new Viewable("index");
    • TEMPLATE_BASE_PATH 应该是 html 文件目录的位置。类似'/WEB-INF'的东西,然后将新的可视化更改为(“/index”)。另外,去掉 ServletProperties.FILTER_STATIC_CONTENT_REGEX 属性。
    • 我的静态资源脚本和 css 位于 src/main/java/resources/static/api/console/ 文件夹中,我的 index.html 也是如此。我正在使用这个 FILTER_STATIC_CONTENT_REGEX 属性来允许 jersey 提供 /api/console/ 目录下的所有文件。没有这个,我无法直接从本地主机查看我的静态资源。请注意,我没有使用 FILTER_FORWARD_ON_404 过滤器。感谢您的帮助!
    • MvcFeature ust 提供了 mvc 支持基础设施,您必须实现对模板引擎的自定义支持。 Jersey 为 jsp、mustache 和免费标记模板提供模板实现。请阅读此链接jersey.java.net/documentation/latest/mvc.html。您可以使用 JspMvcFeature 代替 MvcFeature 来提供 html 页面。
    猜你喜欢
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    • 2015-07-18
    相关资源
    最近更新 更多