Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javax.ws.rs.NotFoundException: Could not find resource for full path

Tags:

java

resteasy

Environment

Windows 7(64)
jdk1.7.0_51(64)
RESTEasy3.0.7
apache-tomcat-7.0.50
Project Name: hello

RESTEasyHelloWorldService.java:

package com.javacodegeeks.enterprise.rest.resteasy;

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

@Path("/RESTEasyHelloWorld")
public class RESTEasyHelloWorldService {

    @GET
    @Path("/{param}")
    @Produces(MediaType.TEXT_PLAIN)
    public String getMsg(@PathParam("param") String name) {
        String msg = "Rest say: good " + name;
        return msg;
    }
}

web.xml:

<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>hello</display-name>

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

    <!-- Auto scan REST service -->
    <context-param>
        <param-name>resteasy.scan</param-name>
        <param-value>true</param-value>
    </context-param>

    <!-- this should be the same URL pattern as the servlet-mapping property -->
    <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>

</web-app>

why do I get the exception when I call the http://localhost:8080/hello/rest/RESTEasyHelloWorld/a returns:

javax.ws.rs.NotFoundException: Could not find resource for full path: http://localhost:8080/hello/rest/RESTEasyHelloWorld/a
    at org.jboss.resteasy.core.registry.ClassNode.match(ClassNode.java:73)
    at org.jboss.resteasy.core.registry.RootClassNode.match(RootClassNode.java:48)
...
like image 996
user3926093 Avatar asked Aug 10 '14 01:08

user3926093


2 Answers

You could try to use http://localhost:8080/hello/RESTEasyHelloWorld/a. (Without the /rest).

If you want to use /rest, you can modify your RESTEasyHelloWorldService @Path to /rest/RESTEasyHelloWorld.


But based on the APIs versions you are using, you can do a much simpler job to get your restful service working.

I'm assuming you have resteasy-jaxrs lib on your classpath.

Since you are not using JBOSS or EAP, you also need to get resteasy-servlet-initializer. Documentation for using Servlet 3.0 Containers like TOMCAT here.

You will need to extend Application, creating for example a RESTEasyService:

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

You don't need to provide any implementation for that class, since RESTEasy will scan for all providers and resources. Documentation for using Application class here.

Leave your RESTEasyHelloWorldService just like you said on your question:

@Path("/RESTEasyHelloWorld")
public class RESTEasyHelloWorldService {

    @GET
    @Path("/{param}")
    @Produces(MediaType.TEXT_PLAIN)
    public String getMsg(@PathParam("param") String name) {
        String msg = "Rest say: good " + name;
        return msg;
    }
}

Now your web.xml doesn't need anything. Java WS-RS and RESTEasy are already doing everything.

Your web.xml can be like this:

<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>hello</display-name>

</web-app>

RESTEasy official documentation is a little confusing at start, but once you understand that the implementation is the same for JBOSS and NON-JBOSS apps (just the use of libs that change), you get things going easier.

like image 135
fasfsfgs Avatar answered Oct 05 '22 00:10

fasfsfgs


I got the same issue when I tried with 3.0.11.Final

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.11.Final</version>
</dependency>

but when I tried with another version it worked.

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

And moreover The URL(http://localhost:8080/hello/rest/RESTEasyHelloWorld/a) which you are trying is correct since you have mentioned /rest in the web.xml .Hope this helps.

like image 20
Raphael Avatar answered Oct 05 '22 00:10

Raphael