RestEasy Jax-RS in Jboss 7.1 doesn't work

2019-02-09 11:07发布

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!

4条回答
ら.Afraid
2楼-- · 2019-02-09 11:24

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.

查看更多
家丑人穷心不美
3楼-- · 2019-02-09 11:24

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.

查看更多
Ridiculous、
4楼-- · 2019-02-09 11:40

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.

查看更多
该账号已被封号
5楼-- · 2019-02-09 11:46

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!! :)

查看更多
登录 后发表回答