user697911
user697911

Reputation: 10531

JaxRS path in HelloWorld example

This is the first example in JEE 7 tutorials for Restful services, simply printing out "hello world" to the screen. The tutorial is configured to run in Glassfish, but I am testing it in Eclipse and Wildfly. When I start the application Wildfly, it dispalys "http://localhost:8080/hello/", the error message is:

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

Then I run this URL: "http://localhost:8080/hello/helloworld", it works correctly. The original tutorial has this configuration for Glassfish below, so it can directly executes "http://localhost:8080/hello" to run the example.

How can I change the configuration or source code so that I can directly run "http://localhost:8080/hello" with appending the "helloworld" to it in Wildfly and Eclipse? The original tutorial doesn't even has a web.xml file to configuration. Instead it uses nbactions.xml to do the configuraiton.

<action>
            <actionName>profile</actionName>
            <goals>
                <goal>package</goal>
            </goals>
            <properties>
                <netbeans.deploy>true</netbeans.deploy>
                <netbeans.deploy.profilemode>true</netbeans.deploy.profilemode>
                <netbeans.deploy.clientUrlPart>/helloworld</netbeans.deploy.clientUrlPart>
            </properties>
        </action>

HelloWorldApplication.java

@ApplicationPath("/")
public class HelloApplication extends Application {

}

HelloWorld.java

@Path("helloworld")
public class HelloWorld {
    @Context
    private UriInfo context;

    /** Creates a new instance of HelloWorld */
    public HelloWorld() {
    }

    /**
     * Retrieves representation of an instance of helloWorld.HelloWorld
     * @return an instance of java.lang.String
     */
    @GET
    @Produces("text/html")
    public String getHtml() {
        return "<html lang=\"en\"><body><h1>Hello, World!!</h1></body></html>";
    }

    /**
     * PUT method for updating or creating an instance of HelloWorld
     * @param content representation for the resource
     */
    @PUT
    @Consumes("text/html")
    public void putHtml(String content) {
    }
}

Pom.xml

<modelVersion>4.0.0</modelVersion>
    <groupId>org.glassfish.javaeetutorial</groupId>
    <artifactId>hello</artifactId>
    <packaging>war</packaging>
    <name>hello</name>

    <parent>
      <groupId>org.glassfish.javaeetutorial</groupId>
      <artifactId>jaxrs</artifactId>
      <version>7.0.5</version>
    </parent>

Upvotes: 0

Views: 349

Answers (1)

Mike
Mike

Reputation: 4963

The application path is set first by the HelloWorldApplication.java class annotation

@ApplicationPath("/")

This means that your application will be served at the root context of your deployment, so you only need to go to http://localhost:8080/hello to get to any resource within your application.

The rest resource you have defined in HelloWorld.java sets its path within the application by another class annotation:

@Path("helloworld")

so JAX-RS will make this resource available with a combined path of the @ApplicationPath and the resource @Path. You can probably see for yourself that you can change the @Path above to be:

@Path("/")

The path to your resource will then be context-path + ApplicationPath + Path or, in other words your scenario would be, your-app-name + nothing + nothing.

When you deploy your application to GlassFish, you could even set your context path to be the root context so that you wouldn't even need to specify hello in the URL.

Upvotes: 1

Related Questions