Building a RESTful Web Application with JAX-RS and extjs
Creating a simple RESTful Web Application with JAX-RS and extjs is an easy one with NetBeans.
I‘ll split this tutorial into three posts:
- Creating the Web Service.
- Creating the Web UI with extjs
- Adding a simple form to the UI for adding and modifying data.
The final Application will look like this:
Here are the steps for creating the JAX-RS Web Service which provides access to the data.
Create a new “Java Web“ project (Strg+Shift+N).
NOTE: If you don‘t see a project type “Java Web” you have to install the Java Web Application plugin first (Tools->Plugins->Java Web Applications).
Create a simple Entity Bean named “Person“.
Open the “New File“ dialog and select the file type “Entity Class”.
Netbeans kindly informed us that we haven‘t added a Persistence Unit yet. Press the “Create Persistence Unit …“ button and select a data source. I used the already existing datasource “jdbc/sample“ which is installed by default in glassfish and points to the “build-in” javadb.
Click “finish” to create the new Entity bean.
Add the following private fields to the class.
private String name1; private String name2; private String address1; private String address2; private String postalCode; private String city; private String country;
Press “Alt-Ins” to generate the Getters and Setters for the fields.
Now you can create the Web Service for the Person entity.
Even for this task you can use a NetBeans wizard. Open the “New File“ dialog once again and select “RESTful Web Service from Entity Classes“ from the “Web Service“ category. Add the “Person” entity and go to the next page. Here you can adjust the package names if you like.
After clicking finish you can find the generated files in your source folder.
At this stage you have a functional REST backend. Deploy your application to your glassfish instance and direct the browser to http://localhost:8080/extjs-example/resources/people.
You should see an xml response with the root element people and no children. Alternatively you can open the context menu of “RESTful Web Services“ and select “TEST RESTful Web Services” which generates a nice Web UI for testing the web services.
Eventually load some sample data into the person table. Download the csv file which contains some sample records. Connect against the database where your datasource is pointing at (here: Netbeans->Services->jdbc://derby://localhost:1527/sample) and execute the following command (for derbydb):
call SYSCS_UTIL.SYSCS_IMPORT_TABLE ('APP', 'PERSON', '[PATH_TO]/people.csv', ';', '"', 'ISO-8859-1', 1);
Replace “[PATH_TO]” with the path where your people.csv is located.
Open the following url again in your browser: http://localhost:8080/extjs-example/resources/people
Now you should see the data of the first 10 people from your database.
That‘s it for the Web Service so far.
Marco Rico Gomez is a passionate software developer located in Germany who likes to share his thoughts and experiences about software development and technologies with others.
blog comments powered by Disqus