javax.ws.rs.NotFoundException: Could not find resource for full path with RESTEasy and Wildfly 8.1.0.Final

前端 未结 7 2149
长情又很酷
长情又很酷 2020-12-06 01:59

I am facing following problem. I have spent more than 3 days on this but cannot find a solution. Please guide me what I am doing wrong here. I am new to Resteasy with wildfl

相关标签:
7条回答
  • 2020-12-06 02:10

    This works for all my services.

    This is a runtime exception indicating a resource requested by a client was not found on the server.
    Add below entry into your web.xml :
    
    <context-param>
            <param-name>resteasy.resources</param-name>
            <param-value>com.org.abc.xyz.MainClassName</param-value>
    </context-param>

    You can specify fully qualified name of your JAX-RS resource class name you want to register. If you have multiple classes entries, use comma delimiter.

    0 讨论(0)
  • 2020-12-06 02:18
    • Base:

      (1) http://localhost:8080/admin-ws  (I assume `admin-ws` is the app name)
      
    • @ApplicationPath("/services") == append /services to Base

      (2) http://localhost:8080/admin-ws/services
      
    • @Path("/user") == append /user to previous

      (3) http://localhost:8080/admin-ws/services/user
      
    • @Path("/services/user/getUser") == append /services/user/getUser to previous

      (4) http://localhost:8080/admin-ws/services/user/services/user/getUser
                 //This is the winner with your current set up
      
    • What you are using:

      http://localhost:8080/admin-ws/services/user/getUser
      

      What's different?

    How it Should Look : to follow good REST (naming included) practices

    @ApplicationPath("/services")
    public class WebConfig extends Application {
    }
    
    @Path("/users")
    public class UserResource implements Serializable {
    
        @Inject
        private UserService userService;
    
        @GET
        @Path("/{id}")
        @Produces(MediaType.APPLICATION_JSON)
        public UserWsPojo getUser(@PathParam("id") String id) {
            UserWsPojo uwp = userService.getUserById(id);
            return uwp;
        }
    }
    

    Access:

    http://localhost:8080/admin-ws/services/users/12344
                                           // some user id
    
    0 讨论(0)
  • 2020-12-06 02:25

    Its hard to say for sure, but one thing I see is that you are passing a parameter but not mapping any parameter in the URL. This would be more correct:

    @GET
    @Path("/getUser/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public UserWsPojo getUser(@PathParam("id") String id) {
      // do things with stuff
    }
    

    And then the full URL would be /admin-ws/services/user/getUser/1 to get a user with ID 1.

    0 讨论(0)
  • 2020-12-06 02:32

    Consolidating all the response and below code should work. Tested in tomcat 7.0.56, RESTEasy 3.0.10, Netbeans 8.0.2

    WebConfig.java:

    import javax.ws.rs.ApplicationPath;
    import javax.ws.rs.core.Application;
    @ApplicationPath("/services")
    public class WebConfig extends Application {
     // No methods defined inside
    }
    

    SayHello.java:

    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.core.Response;
    import javax.ws.rs.Produces;
    @Path("/greet")
    public class SayHello {
     @GET
        @Path("/{username}")
        @Produces("text/html")
        public Response printMessage(@PathParam("username") String username) {
            return Response.status(200).entity("Hello " + username).build();
        }
    
    }
    

    Netbeans generated web.xml: no change needed

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- With RESTEasy no web.xml configuration required-->
    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>
    </web-app>
    

    URL to Your REST SERVICE:

    http://hostname:<port>/<yourappcontext>/services/greet/<enterusername>
    

    Expected Response:

    Hello <username>
    

    Netbeans generated POM.XML: (make sure to include resteasy-servlet-initializer dependency if using tomcat and version 2.3 or greater for maven-war-plugin)

    <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.easyrest</groupId>
        <artifactId>easyrest</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>war</packaging>
    
        <name>easyrest</name>
    
        <properties>
            <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
    
        <!-- Add POM dependencies -->
        <repositories>
            <repository>
                <id>org.jboss.resteasy</id>
                <url>http://repo.maven.apache.org/maven2/</url>
            </repository>
        </repositories>
    
        <dependencies>
            <dependency>
                <groupId>org.jboss.resteasy</groupId>
                <artifactId>resteasy-jaxrs</artifactId>
                <version>3.0.10.Final</version>
            </dependency>
            <dependency>
                <groupId>org.jboss.resteasy</groupId>
                <artifactId>resteasy-servlet-initializer</artifactId>
                <version>3.0.10.Final</version>
            </dependency>
            <dependency>
                <groupId>javax</groupId>
                <artifactId>javaee-web-api</artifactId>
                <version>6.0</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.3.2</version>
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                        <compilerArguments>
                            <endorseddirs>${endorsed.dir}</endorseddirs>
                        </compilerArguments>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.3</version>
                    <configuration>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>2.1</version>
                    <executions>
                        <execution>
                            <phase>validate</phase>
                            <goals>
                                <goal>copy</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${endorsed.dir}</outputDirectory>
                                <silent>true</silent>
                                <artifactItems>
                                    <artifactItem>
                                        <groupId>javax</groupId>
                                        <artifactId>javaee-endorsed-api</artifactId>
                                        <version>6.0</version>
                                        <type>jar</type>
                                    </artifactItem>
                                </artifactItems>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    0 讨论(0)
  • 2020-12-06 02:33

    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>
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-06 02:34

    I've got same issue (NotFound). After debugging, it turned out that I missed the method. (@GET, @POST...) on the controller

    @Path("/create")
    public Response createObject(){
    }
    //correct
    @GET // or @POST
    @Path("/create")
    public Response createObject(){
    }
    
    0 讨论(0)
提交回复
热议问题