Jeremy S.
Jeremy S.

Reputation: 125

Display html page in tomcat from maven RESTEasy webapp

I have a problem with my web app created with maven. I use RESTEasy to create some webservices in this app and I want to create a simple html page with a form in this webapp in order to test the @post @FormParam annotations. I created the login.html page located in the 'webapp' folder of my webapplication.

but when I tried to reach the page at /resteasyWebapp/login.html or the default jsp page at /resteasyWebapp/index.jsp, it doesn't work. I get a 404 error code. I took a look at the package of my application and the both files (login.html and index.jsp) are located at the root of my application. My webservice works but I'm not able to reach the html page through chrome/firefox...

project hierarchy: here

login.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>login example</title>
</head>
<body>
<form method="POST" action="login">
<table>
<tr>
<td>Email Address:</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td><input type="submit"></td>
</tr>
</table>
</form>
</body>
</html>

my pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spanier.resteasy</groupId>
<artifactId>resteasyWebapp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>resteasyWebapp Maven Webapp</name>
<url>http://maven.apache.org</url>

<repositories>
    <!-- Obligatoire pour fournir les bons providers -->
    <repository>
        <id>JBoss repository</id>
        <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
    </repository>
</repositories>

<dependencies>

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxrs</artifactId>
        <version>3.0.4.FINAL</version>
    </dependency>

    <!-- XML provider -->
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxb-provider</artifactId>
        <version>3.0.4.FINAL</version>
    </dependency>

    <!-- JSON provider -->
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jettison-provider</artifactId>
        <version>3.0.4.FINAL</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>


    <!-- Pour conteneur servlet 3.0 // jetty 8.x ou tomcat, preconisé par la 
        doc -->
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-servlet-initializer</artifactId>
        <version>3.0.4.Final</version>
    </dependency>

</dependencies>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.0</version>
            </plugin>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>8.1.5.v20120716</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

my 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">

    <display-name>My RestEasy sample Web Application</display-name>

    <!-- <listener> <listener-class> org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap 
        </listener-class> </listener> -->
    <servlet>
        <servlet-name>Resteasy</servlet-name>
        <servlet-class>
            org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
        </servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>HelloWorldApplication</param-value>
        </init-param>
    </servlet>

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

</web-app>

the HelloWorldApplication, where I define my root resources:

public class HelloWorldApplication extends Application {
private Set<Object> singletons = new HashSet();
private Set<Class<?>> empty = new HashSet();

public HelloWorldApplication() {
    // ADD YOUR RESTFUL RESOURCES HERE
    this.singletons.add(new HelloWorld());
    this.singletons.add(new LoginService());
}

public Set<Class<?>> getClasses()
{
    return this.empty;
}

public Set<Object> getSingletons()
{
    return this.singletons;
}
}

I think there is something I missing with the configuration of RESTEasy but I don't figure out what...

Upvotes: 4

Views: 3394

Answers (1)

Fabricio Lemos
Fabricio Lemos

Reputation: 2955

If you want to serve html or jsp pages in your application, you shouldn´t map <url-pattern> to /*. If you do so, Resteasy will intercept the request for your page and, since it is not a rest service, the page will not be displayed. The solution is to add a prefix do your services URLs.

Change <url-pattern>/*</url-pattern> to something like <url-pattern>/service/*</url-pattern> and add

<context-param>
  <param-name>resteasy.servlet.mapping.prefix</param-name>
  <param-value>/service</param-value>
</context-param>

to your web.xml. Also remember to add the "/service" prefix when you call your webservices. That way you should be able to access your pages and also have a logical separation for the different kinds of content the your application provides.

Upvotes: 1

Related Questions