RestEasy Jax-RS in Jboss 7.1 doesn't work
Asked Answered
G

4

8

I'm trying to deploy a simple web application under JBoss AS 7.1 which comes bundled with resteasy. According to the documentation all that is needed is (at bare minimum) is an empty web.xml, a class with annotated @ApplicationPath("/mypath") and @Path("/other_stuff") for your other classes

The documentation I'm following is here:

Still, when I hit:

host:8080/warname/applicationpath/path

I receive a 404 error on the webpage but nothing in the logs.

Is there a configuration file I need to change in order for JAX-RS to work?

Thank you in advance for any help!

Gordongordy answered 25/4, 2013 at 20:17 Comment(1)
I already have the suggested solution by acdcjunior in my webapp but It seems that Jboss doesn't register the ApplicationPath or Path. I don't see anything relating to JAX-RS or Rest in my logs when starting up.Gordongordy
S
16

Empty web.xml will do.

Just add some resteasy dependency to your classpath. For instance, if you use maven you can add this to your pom.xml:

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jaxrs</artifactId>
    <version>2.3.1.GA</version>
    <scope>provided</scope> <!-- provided if youre deploying to jboss as 7.1 -->
</dependency>

Then set up the application using only this class:

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

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

Just to make sure, add a resource like this:

@Path("/hello")
public class HelloResource {
    @GET
    @Produces("text/plain")
    public String helloResource() {
        return "Hello! It's "+new Date();
    }
}

And that's all you need. Deploy it at a JBoss AS 7.1 and get to it, say:

http://127.0.0.1:8080/mywarname/rest/hello

Edit:

I have created a java war maven project with the bare minimum strucutre:

-pom.xml
-src
 |
  --main
    |
     --java
       |
        --rest
          |
           --HelloResource.java
           --JaxRsActivator.java

I called it simpleRest as seen below. All the archives are exactly as shown:

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>simpleRest</groupId>
    <artifactId>simpleRest</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>2.3.1.GA</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <!-- So maven doesn't require web.xml -->
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>

HelloResource.java

package rest;

import java.util.Date;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("/hello")
public class HelloResource {
    @GET
    @Produces("text/plain")
    public String helloResource() {
        return "Hello! It's "+new Date();
    }
}

JaxRsActivator.java:

package rest;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/rest")
public class JaxRsActivator extends Application {

}

This generates a simpleRest.war (through mvn clean package). I then deploy it to a freshly installed JBoss AS 7.1.1.Final. As you can see, no reference is made to JAX-RS in the log during the deploy:

22:48:08,677 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-1) JBAS015876: Starting deployment of "simpleRest.war"
22:48:09,318 INFO  [org.jboss.web] (MSC service thread 1-4) JBAS018210: Registering web context: /simpleRest
22:48:09,492 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "simpleRest.war"

After that, the URL is available as expected:

http://127.0.0.1:8080/simpleRest/rest/hello

Notice that everything else gives a 404 error. But it is a different kind of 404.

http://127.0.0.1:8080/simpleRest/ gives:

HTTP Status 404 - /simpleRest/

That is a page not found error. On the other hand, http://127.0.0.1:8080/simpleRest/rest gives:

HTTP Status 404 - Could not find resource for relative : / of full path: http://127.0.0.1:8080/simpleRest/rest

That is a resource (REST service) not found error. This way you know JAX-RS is acting, though it did not have a handler for that path.

Stereotypy answered 26/4, 2013 at 3:5 Comment(7)
Thanks for the reply, That's exactly what I have done but when I try to access the link I'm sent to a 404 page. There are no error logs for the request and when I start Jboss 7.1 up I don't see my rest paths registeringGordongordy
@Gordongordy Okay, I'll take a look at what may be going wrong.Stereotypy
@Gordongordy I edited the answer. Tried this myself just now. As you can see, do not expect the server to annouce during deploy that there is a JAX-RS service in the war file (as it would when there is a WSDL). If this .war setup doesn't work for you, try on a fresh new JBoss (get it at the link I pointed out). In this new one, it must work.Stereotypy
I've done this on a seperate project and it works! I haven't been able to integrate it in to my current project but I believe it's because I'm using BlazeDS. I will create another post for Integration of BlazeDS and RestEasy. Thank you again for all of your help!Gordongordy
Awesome guidance @acdcjunior, because of you, I have finally managed to get my own JAX-RS app working with Wildfly via IntelliJ ;-) after much headbanging about the web...Broadsword
you may get 'Cannot start application' - InvalidDeploymentException manifest may not be valid XmlException Data at the root level. Then to solve this add the @Produces on the Mapped Method. @Produces("text/html") public String hello() {Paapanen
I used this exact example with an empty web.xml, and I only get a 404 on visiting that endpoint. RestEASY also logs absolutely nothing even though I have log4j set up with "log4j.logger.org.jboss.resteasy=INFO". I don't have maven or a pom.xml, though to my knowledge that doesn't matter once it's deployed. Please help, resteasy has been a nightmare.Cedrickceevah
B
1

Take a look at jboss quickstarts: http://www.jboss.org/jdf/quickstarts/jboss-as-quickstart/ You can get them from http://www.jboss.org/jbossas/downloads

These are working out of box. For helloword-rs quickstart I can see web.xml with content:

     <servlet-mapping>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <url-pattern>/rest/*</url-pattern>
      </servlet-mapping>

and also comment: One of the way of activating REST Servises is adding these lines, the server is responsible for adding the corresponding servlet automatically. If the src folder, org.jboss.as.quickstarts.rshelloworld.HelloWorld class has the Annotations to receive REST invocation.

Betatron answered 25/4, 2013 at 22:18 Comment(0)
M
0

If you are using JBoss AS 7.1, you also need to add the "resteasy.resources" context parameter. You also need to send the init-param to the HttpServletDispatcher servlet.

I found the solution at the following link : http://www.javaroots.com/2013/05/creating-rest-services-with-rest-easy.html

Just one typo error(maybe!!) in the above link. In the "RootRestService" change the method signature of the getClasses() method to public Set<Class<?>> getClasses().

Thats it! It solved my 3 day headache. Hope it helps you too!! :)

Matrilineage answered 23/3, 2015 at 6:23 Comment(0)
R
0

Even though acdcjunior's answer is great and very thorough, i'd like to reinfoce Andrzej's answer.

It works as a charm and it is by far the more straightforward one. The JBoss quickstart samples (also available on GitHub) are always a great resource to answer questions like this one.

Radom answered 5/7, 2015 at 22:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.