用Jersey 2.6返回jsp

时间:2014-03-02 08:02:18

标签: java jersey

我正在尝试使用Jersey 2.6返回一个Viewable类。 我已经阅读了很多例子,但似乎没有任何效果。我只得到404响应,虽然我可以看到请求映射确实有效。详情如下:

的web.xml

 <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <module-name>helloworld-spring</module-name>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>SpringApplication</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.proj.configuration.JerseyConfiguration</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.mvc.templateBasePath.jsp</param-name>
            <param-value>/WEB-INF/jsp</param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.mvc.templateBasePath</param-name>
            <param-value>/WEB-INF/jsp</param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>org.glassfish.jersey.server.mvc.jsp.JspMvcFeature</param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>rest</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringApplication</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

JerseyConfiguration类

public class JerseyConfiguration extends ResourceConfig {

    /**
     * Register JAX-RS application components.
     */
    public JerseyConfiguration () {
        register(HomeResource.class);

        register(JspMvcFeature.class);

        // Logging.
        register(LoggingFilter.class);

        // Tracing support.
        property(ServerProperties.TRACING, TracingConfig.ON_DEMAND.name());
    }
}

HomeResource类

@Path("jersey-hello")
public class HomeResource {

    @GET
    @Path("/myPage")
    @Produces(MediaType.TEXT_HTML)
    public Viewable getHello() {
        return new Viewable("/myPage.jsp", this);
    }
}

我在/ WEB-INF / jsp文件夹中有myPage.jsp

我尝试了许多组合,例如将myPage放在没有.jsp以及更多......似乎没有任何工作

这里是我打电话的网址:

http://localhost:8080/jersey-hello/myPage

1 个答案:

答案 0 :(得分:1)

通常,如果资源文件(如JSP中的"/myPage.jsp")提供了HomeResource.getHello文件的绝对路径,则此路径相对于{templateBasePath定义的基本路径1}} web.xml(在您的情况下,它会像/WEB-INF/jsp/myPage.jsp中的预期一样)。

我正在Weblogic(12.2.1)正在处理Jersey(2.21.x)/JSP,我看到的问题是jersey.config.server.mvc.templateBasePath web.xml因某种原因导致内部配置失败。事实上,许多<init-param>属性都没有。

或者,可以在ResourceConfig派生类中直接添加所有必需属性,然后Jersey识别这些属性:

import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.server.mvc.jsp.JspMvcFeature;
import org.glassfish.jersey.server.ResourceConfig;

@ApplicationPath("/")
public class MyResourceConfig extends ResourceConfig {
    public MyResourceConfig() {
        packages("foo.bar");
        register(LoggingFilter.class);
        register(JspMvcFeature.class);
        // it may be also jersey.config.server.mvc.templateBasePath
        property("jersey.config.server.mvc.templateBasePath.jsp", "/WEB-INF/jsp");
        // ... more properties as needed ...
    }
}

我最终得到的web.xml如下:

<web-app version="3.0" ...>
    <filter>
        <filter-name>jersey</filter-name>
        <filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class>
        <init-param>
            <param-name>jersey.config.servlet.filter.forwardOn404</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>jersey</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>