Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not connect JAX-RS service to MVC template

I'm attempting to make use of JAX-RS' (Jersey) MVC pattern. Attempts to reach http://localhost:8080/myproject/foos/test result in an error that reads:

java.io.IOException: The template name, /view, could not be resolved to a fully qualified template name

http://localhost:8080/myproject/foos results in the same error.

What am I missing?

Resource:

package resources;

import com.sun.jersey.api.view.Viewable;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("foos")
public class FooResource {

    @GET
    @Produces(MediaType.TEXT_HTML)
    public Viewable get() {

        return new Viewable("/index", this);

    }   

    @GET
    @Path("{id}")
    @Produces(MediaType.TEXT_HTML)
    public Viewable get(@PathParam("id") String id) {

        return new Viewable("/view", id);

    } 

}

Views:

WEB-INF / jsp / resources / FooResource

  • index.jsp
  • view.jsp

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <filter>
        <filter-name>jersey</filter-name>
        <filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
            <param-value>/(resources|images|js|styles|(WEB-INF/jsp))/.*</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>jersey</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>ServletAdaptor</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <description>Set the default, base template path to the WEB-INF folder.</description>
            <param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
            <param-value>/WEB-INF/jsp</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>ServletAdaptor</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>

</web-app>
like image 639
craig Avatar asked Jan 23 '12 20:01

craig


2 Answers

Made the following changes:

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

    <welcome-file-list>
        <welcome-file>welcome.jsp</welcome-file>
    </welcome-file-list>

    <filter>
        <filter-name>jersey</filter-name>
        <filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>controllers</param-value>
        </init-param>        
        <init-param>
            <param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
            <param-value>/((WEB-INF/views))/.*</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
            <param-value>/WEB-INF/views/</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.config.feature.Redirect</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.config.feature.FilterForwardOn404</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>

Resource:

@Path("foos")
public class FooResource {

    @GET
    @Produces(MediaType.TEXT_HTML)
    public Viewable index() {

        return new Viewable("/foos/index", this);

    }   

    @GET
    @Path("{id}")
    @Produces(MediaType.TEXT_HTML)
    public Viewable view(@PathParam("id") String id) {

        return new Viewable("/foos/view", id);

    } 

}

Views:

  • \welcome.jsp
  • \WEB-INF\views\foos\
    • index.jsp
    • view.jsp
like image 124
craig Avatar answered Nov 11 '22 07:11

craig


I had this same error running under Jetty 9. The application ran fine using mvn clean jetty:run but had this error when packaged as a war and deployed under Jetty. This is the fix in web.xml that worked for me:

         <init-param>
             <param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
-            <param-value>/WEB-INF/views/</param-value>
+            <param-value>/WEB-INF/views</param-value>
         </init-param>

Yep, that's it. So, hopefully this helps someone who stumbles across this. My config is basically the same as craig's, but had the extra slash.

like image 1
Don Branson Avatar answered Nov 11 '22 07:11

Don Branson