Share this blog!

A Hello world application in JAX-RS

This post explains steps to create, deploy and run a simple Hello world application in JAX-RS (in Netbeans).

Update: Check this article to add Jersey manually.


1. Create a web application

In Netbeans IDE create a new web project by File→New Project. Select Java Web in categories and choose Web Application in Projects.

Define a project name and set GlassFishServer as the server. If it is not installed, you may have to install it - simply choose Add and install it on the go. Once the project is created, the index file will be opened in the Source pane.

2. Add the Root Resource Class

You can refer this article from Oracle Docs for more information on Root Resource Class and the annotations. 

The easiest way to add the Root Resource Class is to use the pre-designed template from Netbeans. For this, right click on the project clicke New→RESTful Web Services from Patterns. 

Select Simple Root Resource and define the resource package name, path, class name and MIME.




Once finished, the HelloWorld.java will be added and appeared in the Source pane.

In the newly created class, find the getHTML method and return a valid HTML message.

public String getHtml() {
        return "<html lang=\"en\"><body><h1>Hello, World!!</body></h1></html>";
}

What did we just do?


@Path("helloworld")
public class HelloWorld {

    @GET
    @Produces(MediaType.TEXT_HTML)
    public String getHtml() {
        return "<html lang=\"en\"><body><h1>Hello, World!!</h1></body></html>";
    }

}

Just before the class definition, there is an annotation @Path("helloworld") which binds the relative URI webresources/helloworld into the HelloWorld class. The webresources/ path was actually set by the template, which is defined by @javax.ws.rs.ApplicationPath in the ApplicationConfig class in the package. 

The @GET is similar to the HTTP GET method and the method following the GET annotation represents the method to be executed.

The @Produces defines the output format, which in our case was set to TEXT_HTML, which is why we are returning an html output.

Refer this article for more info on annotations.


3. Test the web service

Right click on the project and click Test RESTful Web Services. If it prompts for configuration, go for the defaults set by the dialog box and click OK.

The test will deploy the application and initialize a test client in your browser. Click on helloworld resource on the left and click Test on the appearing pane to test the resource.


4. Run the service

When running the application, it will show only the contents of the index file regardless of the service. The service can be accessed by the path defined in the Test client (in the above test client it is http://localhost:8080/HelloWorldREST/webresources/helloworld).

You can add the path to the index file as in:

<a href="webresources/helloworld">Go to hello world</a>

Or it could be set as the default in run configuration on the project properties. Right click on project and click Properties→Run and define the Relative URL (for the above, it would be webresources/helloworld).


Next PostNewer Post Previous PostOlder Post Home

0 comments:

Post a Comment