OrenM
OrenM

Reputation: 369

serving HTML/JSP in a restful webapp using jersey & java

I Can't seem to figure it out. tried to use annotations and web.xml to configure paths to webcontent but keep getting unknown resources.

Jersey main:

@ApplicationPath("/")
public class App extends PackagesResourceConfig {
   public App() {
        super("webapp.resources");
    }
}

Jersey default path "/": (hello world works! index.html\jsp does not)

@Path("/")
@Produces(MediaType.TEXT_HTML)
public class RootResource
{
    @GET 
    public String index(@Context HttpServletRequest request) {
        return "hello world";
    }
}

What I've tried:

  1. multiple web.xml config
  2. serving viewables (error cannot resolve template index.jsp)

what do you think can be a solution to serve pages like html or jsp?

is there a way to do it with jersey (no spring!) and viewable\templates?

Upvotes: 2

Views: 9691

Answers (3)

Septem
Septem

Reputation: 3622

Jersey provides support for JSP templates in jersey-mvc-jsp extension module

check out the official doc

Upvotes: 0

OrenM
OrenM

Reputation: 369

to answer my question. futuretelematics's solution should work for most people. it is a known and valid solution. so WHY didn't work when i did it:

question 1: after much fiddling around i discovered that once i changed the application root path from "/" to "/api" (or /???) suddenly everything started to play along. must be a jersey thing. i read somewhere that in order to make the "/" work you should map with filter in the web.xml. I would love to hear if anyone has done that successfully.

so right now it serves my initial page with JSP. that page i can manipulate with jsons in a rest fashion.

question 2: making @viewable work was just a matter of creating the right folder structure path in WebContent (com/webapp/model/index(<-- jsp)). that's how the viewable page redirect works.

Upvotes: 3

futuretelematics
futuretelematics

Reputation: 1495

This should be easy; try the following:

The /WEB-INF/web.xml file:

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

    <display-name>Your REST end Point</display-name>

    <!-- /////////////////////// JERSEY (NO SPRING) ///////////////////////// -->
    <servlet>
        <servlet-name>MyRESTEndPoint</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

        <!- Package where jersey will scan for resources ->
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.mycompany</param-value>
        </init-param>

        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyRESTEndPoint</servlet-name>
        <url-pattern>/MyRESTEndPoint/*</url-pattern>
    </servlet-mapping>

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

</web-app>

AT package com.mycompany (look at at the web.xml) place the Application class:

package com.mycompany;

public class MyApp 
     extends Application {
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> s = new HashSet<Class<?>>();
        s.add(MyResource.class);
        // ... add other resources
        return s;
    }
}

Create the resource like:

@Path("myResourcePath")
public class R01ERESTResourceForStructure {
    @GET @Path("{myResourceId}") 
    @Produces(MediaType.APPLICATION_XML)
    public Response load(@PathParam("myResourceId") final String id) {
         ....
    }
}

Your urls should be: like /MyRESTEndPoint/myResourcePath/myResourceId

If you're using SPRING or GUICE the web.xml should be a bit different

Upvotes: 1

Related Questions