8

First off, I am new to this environment. I've developed Java before, but not for an application server. Having never done that, I've never worked with JBoss or WildFly previously.

I've been able to set up and run the WildFly server, and access it at 127.0.0.1:9990. When I deploy my .war file, the server doesn't react and I can't access the URLs.

The WildFly server does state that my deployment succeeded and is active, then I try to access: 127.0.0.1:8080/RECAPP-API/rest/message/test and I get a 404 (Page not found error).

I'm using Maven, so first, 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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test.recapp.rest</groupId>
  <artifactId>RECAPP-API</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxrs</artifactId>
        <version>3.0.6.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jackson-provider</artifactId>
        <version>3.0.6.Final</version>
    </dependency>
  </dependencies>
</project>

My JSONService.java:

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.Response;

@Path("/message")
public class JSONService {

    @GET
    @Path("/{param}")
    @Produces("application/json")
    public Response printMessage(@PathParam("param") String msg) {
        String result = "Restful example: " + msg;
        return Response.status(200).entity(result).build();
    }

}

And finally, my web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    id="WebApp_ID" version="3.0">
  <display-name>RECAPP-API</display-name>

  <context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>true</param-value>
  </context-param>

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

  <listener>
    <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
  </listener>

  <servlet>
    <servlet-name>resteasy-servlet</servlet-name>
    <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>resteasy-servlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>


</web-app>

Thanks for your help.

TT.
  • 15,774
  • 6
  • 47
  • 88
Honus Wagner
  • 2,830
  • 13
  • 44
  • 61
  • 2
    The first Rest-Application might be a little confusing at the beginning. It looks like you're missing the JaxRsActivator. You need this to create a valid context for the application. `@ApplicationPath("/rest") public class JaxRsActivator extends Application { /* class body intentionally left blank */ }`Also you might take a look at the TicketMonster tutorial: http://www.jboss.org/jdf/examples/ticket-monster/tutorial/Introduction/ – Lama Mar 19 '14 at 11:48
  • Too bad that tutorial doesn't make a single mention what you are trying to explain though; it depends on code generation and so doesn't really explain much about the foundations. – Gimby Mar 19 '14 at 12:07
  • @Goot - why does WildFly make you do this??? I know older versions of JBoss you only have to configure web.xml. On my machine that didn't work, so I went with what you said to do and it worked. – Rob L Apr 15 '15 at 20:47

3 Answers3

7

Best way to quick start is use this dependency .

<dependency>
  <groupId>javax</groupId>
  <artifactId>javaee-api</artifactId>
  <version>7.0</version>
  <scope>provided</scope>
</dependency>

And add a class that Extends Application Class

@ApplicationPath("rest")
public class ConfigApp extends Application {
   public ConfigApp(){
   }
}

Thats it. No web.xml changes (web.xml is not required only).

And access your rest endpoint using host:port/<warname>/rest/<endpoint path>

cubuspl42
  • 7,833
  • 4
  • 41
  • 65
Shettyh
  • 1,188
  • 14
  • 27
5

In a Wildfly Quickstart they seem to prefer using a JaxRsActivator class: Add this to configure your REST service.

/**
 * A class extending {@link Application} and annotated with @ApplicationPath is the Java EE 6 "no XML" approach to activating
 * JAX-RS.
 *
 * <p>
 * Resources are served relative to the servlet path specified in the {@link ApplicationPath} annotation.
 * </p>
 */
@ApplicationPath("/rest")
public class JaxRsActivator extends Application {
    /* class body intentionally left blank */
}

As the comments state, this is a non-xml approach.

K.Nicholas
  • 10,956
  • 4
  • 46
  • 66
1

Your example looks correct.

It helps to restart your jboss server and redeploy your WAR to rule out potential caching.

Also, your web.xml could be shortened to use javax.ws.rs.core.Application as shown below.

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns="http://java.sun.com/xml/ns/javaee" 
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
      id="WebApp_ID" version="3.0">
   <display-name>RECAPP-API</display-name>

   <servlet-mapping>
       <servlet-name>javax.ws.rs.core.Application</servlet-name>
       <url-pattern>/rest/*</url-pattern>
   </servlet-mapping>
</web-app>
whyceewhite
  • 6,317
  • 7
  • 43
  • 51