web-servicesmavenjax-rswildfly-10

Service could not find resource (Wildfly 10.0, JAX-RS)


I am learning REST services and face the problem: RestEasy cannot find my resources, even though I've tried various ways to demonstrate them.

Exception:

Failed to execute: javax.ws.rs.NotFoundException: RESTEASY003210: Could not find resource for full path: http://localhost:8080/RestfullService/services/hello

where RestfullService is the name of my project, /service - applicationPath, hello - resource. None of the errors (404 etc.) are shown on this pages.

My web.xml (by the way, I've met controversial approaches regarding the content of web.xml while using web app server 3.0. According to official manuals, it is not required, but lots of people argue for it. What is the best practice?)

<?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" version="3.0">
  <display-name>RestEasy sample Web Application</display-name>
  <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>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.store.model.WebConfig</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>resteasy-servlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <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>/</param-value>
    </context-param> 
</web-app>

My pom.xml (Although I've added all dependencies manually, the problem wasn't eliminated)

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <groupId>RestfullService</groupId>
    <artifactId>RestfullService</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <webXml>webapp\WEB-INF\web.xml</webXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <!-- <executions> <execution> <phase>package</phase> <configuration> <webXml>\webapp\WEB-INF\web.xml</webXml> 
                </configuration> </execution> </executions> -->
            <plugin>
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <modules>
                        <ejbModule>
                            <!-- property configurations goes here -->
                        </ejbModule>
                    </modules>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>JBoss repository</id>
    <url>https://repository.jboss.org/nexus/content/groups/public/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>3.0.4.Final</version>
            <scope>provided</scope>
        </dependency>
         <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jackson-provider</artifactId>
            <version>3.0.4.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-servlet-initializer</artifactId>
            <version>3.0.4.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-client</artifactId>
            <version>3.0.4.Final</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxb-provider</artifactId>
            <version>3.0.4.Final</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>jaxrs-api</artifactId>
            <version>3.0.4.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-multipart-provider</artifactId>
            <version>3.0.4.Final</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
                <groupId>org.jboss.spec</groupId>
                <artifactId>jboss-javaee-7.0</artifactId>
                <version>1.0.0.Final</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.annotation</groupId>
            <artifactId>jboss-annotations-api_1.2_spec</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
        </dependency>
        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <version>2.0-EDR1</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.servlet</groupId>
            <artifactId>jboss-servlet-api_3.0_spec</artifactId>
            <version>1.0.2.Final</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20140107</version>
        </dependency>
    </dependencies>
</project>

ServiceInitialisation.java (both options with and without the body of this class didn't help):

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

@ApplicationPath("/services")
public class ServiceInitialisation extends Application{
//  private Set<Object> singletons = new HashSet<Object>();
//    private Set<Class<?>> classes = new HashSet<Class<?>>();
//
//  public ServiceInitialisation() {
//      singletons.add(new GoodsWebServiceImpl());
//  }
//
//  @Override
//  public Set<Object> getSingletons() {
//      return singletons;
//  }
//
//    @Override
//    public Set<Class<?>> getClasses() {
//        return classes;
//    }
    
}

And finally services:

import java.util.List;
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;
import com.store.entity.Item;
@Path("/")
public interface GoodsWebService {
    
    @GET
    @Path("/hello")
    @Produces("application/json")
    public Response greet();

     // id: \\d+ - regex for cheching if it is int  @GET    @Path("{id:
     \\d+}")    
    @Produces("application/json")   
    public Response
    getInfoById(@PathParam("id") int id);

    @GET
    @Path("/all")
    @Produces("application/json")
    public List<Item> getAllItems();
}

    package com.store.restService;

import java.util.ArrayList;
import java.util.List;

//import org.json.JSONException;
//import org.json.JSONObject;
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;

import org.json.JSONObject;

import com.store.entity.Item;
import com.store.entity.Shop;

@Path("/")
public class GoodsWebServiceImpl implements GoodsWebService
{
    private static List<Item>data;
    private static List<Shop>shops1;
    private static List<Shop>shops2;
    private static List<Shop>shops3;
    private static Shop shop1;
    private static Shop shop2;
    private static Shop shop3;
    static {
        shop1 = new Shop(1,4.55, 1);
        shop2 = new Shop(2,9.99, 2);
        shop3 = new Shop(3,6,0);
        shops1.add(shop1); shops1.add(shop2);
        shops2.add(shop3); shops2.add(shop1);
        shops3.add(shop2); shops3.add(shop3);
        data = new ArrayList<>();
        data.add(new Item(1, "hand", shops1));
        data.add(new Item(2, "pen", shops2));
        data.add(new Item(3, "ball", shops2));
    }
    public static Item getItemById(int id){
        for(Item e: data){
            if(e.getId()==id) return e;
        }
        throw new RuntimeException("Can't find Item with id -" + id);
    }
    public static List<Item> getAll(){
        return data;
    }
    @Override
    public Response greet(){
        String result = "Hello!";
        return Response.status(200).entity(result).build();
    }
    @Override
    public Response getInfoById(int id){
        JSONObject jsonObject = new JSONObject();
        Item item = getItemById(id);
        jsonObject.put("Id ", item.getId()); 
        jsonObject.put("Mpn ", item.getMpn());
        jsonObject.put("Shop ", item.getShops());

        String result = "Output:\n" + jsonObject;
        return Response.status(200).entity(result).build();
    }
    @Override
    public List<Item> getAllItems(){
//      JSONObject jsonObject = new JSONObject();
//      List<Item> items = getAll();
//      jsonObject.put("List", items);
//      JSONArray jArray = jsonObject.getJSONArray("List");
//      for(int i=0; i<jArray.length(); i++){
//          System.out.println(jArray.getJSONObject(i).getString("id"));
//          System.out.println(jArray.getJSONObject(i).getString("mpn"));
//          System.out.println(jArray.getJSONObject(i).getString("shop"));
//      }
        return getAll();
    }
}

I would be gratefull for any comments, since all the available solutions in the web were already been unsuccessfully tried.

Update: Due to the fact that only a few manuals were succesfully deployed on my machine, I suppose I choose incorrect setting during the process of creation of the project. I create the project for REST in this way: create Dynamic web project -> convert it to Maven -> add dependencies etc. -> trying to deploy

Screenshot of them: The image is here

Update2 Moreover, I've noticed that every time I start WildFly I meet the same error. I have tried to create a few project with various dependencies, but the result is the same. How to deal with it?

    22:45:09,109 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-3) MSC000001: Failed to start service jboss.deployment.unit."RestService.war".POST_MODULE: org.jboss.msc.service.StartException in service jboss.deployment.unit."RestService.war".POST_MODULE: WFLYSRV0153: Failed to process phase POST_MODULE of deployment "RestService.war"
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:163)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer from [Module "deployment.RestService.war:main" from Service Module Loader]
    at org.jboss.as.jaxrs.deployment.JaxrsScanningProcessor.checkDeclaredApplicationClassAsServlet(JaxrsScanningProcessor.java:292)
    at org.jboss.as.jaxrs.deployment.JaxrsScanningProcessor.scanWebDeployment(JaxrsScanningProcessor.java:153)
    at org.jboss.as.jaxrs.deployment.JaxrsScanningProcessor.deploy(JaxrsScanningProcessor.java:104)
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:156)
    ... 5 more
Caused by: java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer from [Module "deployment.RestService.war:main" from Service Module Loader]
    at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:205)
    at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:455)
    at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:404)
    at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:385)
    at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:130)
    at org.jboss.as.jaxrs.deployment.JaxrsScanningProcessor.checkDeclaredApplicationClassAsServlet(JaxrsScanningProcessor.java:290)
    ... 8 more

22:45:09,114 ERROR [org.jboss.as.controller.management-operation] (DeploymentScanner-threads - 1) WFLYCTL0013: Operation ("full-replace-deployment") failed - address: ([]) - failure description: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"RestService.war\".POST_MODULE" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"RestService.war\".POST_MODULE: WFLYSRV0153: Failed to process phase POST_MODULE of deployment \"RestService.war\"
    Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer from [Module \"deployment.RestService.war:main\" from Service Module Loader]
    Caused by: java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer from [Module \"deployment.RestService.war:main\" from Service Module Loader]"}}

Solution

  • You don't need web.xml, the resteasy-servlet, or any explicit RESTEasy dependencies in your POM.

    You'd better start with a simple working example, e.g. https://github.com/wildfly/quickstart/tree/10.x/jaxrs-client.

    Despite its name, this sample not only includes a JAX-RS client, but also a JAX-RS service.