Saturday 24 May 2014

Creating Rest Web Services the easy way with Java in Netbeans

Download Netbeans 8.0 from https://netbeans.org/downloads/ I got the Java EE version as it comes with Tomcat for deploying.

Create a new project in Netbeans, go to Maven > Project from archtype. You can search through all of the available archtypes for Jersey and choose Jersey quickstart webapp.

Find the web.xml file and add the following to enable POJO > JSON mapping.

<init-param>
     <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
     <param-value>true</param-value>
</init-param>

Now when you return any regular Java objects they will be automatically converted to JSON for browser consumption.

There is an example resource called MyResource.java, pay attention to the annotations as these are what powers your web services. Jersey uses all of the standard JAX-RS annotations for creating web services.

Select you project and hit the green play button, your default browser will open and you can view the HTML pages inside the Web Pages folder and also access any web services you have set up.

For more information on Jersey:
https://jersey.java.net/documentation/latest/user-guide.html

No comments:

Post a Comment