One of the benefits of using DukeScript for a client-server application is code reuse.
You can use the same model classes on the client and the server. Our Maven
Archetype CRUD with Jersey Faces is a good example of this.
In this tutorial we’ll change the project a bit to store the data in a database using JPA.
Creating the project
In NetBeans create a new project (File | New Project…). In the New Project wizard
switch to category DukeScript. Choose the template DukeScript Application.
In step 2 choose “crud” as the artifact id. You could use a different name, but
since this is used as part of the projects name it’s easier to understand what I’m referring to when
we both use the same name here.
When you reach the final wizard step Select a Template to start with
where you should select CRUD with Jersey Faces.
On the command line you could use this command instead:
-DarchetypeVersion=0.10 # or later version like 0.18
NetBeans will now automatically start a build; on Eclipse or with plain Maven you’ll
have to do that manually.
Using Wildfly as the server
The purpose of our Jersey sample is to show how you can interact with a server via JSON messages.
So the only purpose of the server module is to answer the requests from the client.
For a more traditional setup you can simply create a regular web project,
e.g. using the Maven Web Application project template.
Here’s how you can use Wildfly as the server.
In the Parent Project, right-click “Modules” and select “Create New Module” from the context menu.
In the wizard that pops up choose “Maven > Web Application”.
In the second step you can select a server. Choose Wildfly here. If it’s not in the drop down list, click add and
point it to the Wildfly dir. Then confirm the settings and finish the wizard.
Now use “New > Web Services > Restful WebService from pattern” and create a Singleton Web Service.
This is only needed to configure the project for Web Services.
You can delete the class you just created after that. Now add the following class to your project:
Now copy the ContactsResource to the new project and delete the old server project.
Make sure to add the project with „Shared Client Server Data Structures“ as a dependency,
so you can again reuse the data model.
If you’re using NetBeans you’ll now see the ContactsResource in the IDE under the
“Restful Web Services” Node. That’s it, you can now start the server and point the
client to this more traditional Web Application.
We’ll use javadb for the database, since it’s easiest to setup. You just need to add this to your pom.xml:
We’ll now define a persistence unit ( File | New file | Persistence | Persistence unit ).
Choose “EclipseLink (JPA 2.1)” as the Persistence Provider and leave the DataSource empty.
Open the Source view and change the file like this:
In order to use JPA for persisting Objects, you can either use Annotations or
an XML-File with the mapping information. We’ll use an XML-File, as the
Model classes are generated and cannot easily be changed. This way we also do not
need to introduce a JPA dependency on the client. The modified persistence.xml
references the mapping-file “orm.xml” for this.
Create file orm.xml next to the persistence.xml:
Please make sure to replace the fully qualified path of the three Model classes (Phone, Address, Contact)
with the one that applies for your project. Otherwise the classes can’t be found.
Now we need to change the application to use the PersistenceUnit. We’ll first
create class ApplicationResources to Produce the Unit:
Next we’ll change our ContactsResource class to use the PersistenceUnit:
Now run the server application. It should open the browser and display a hello world message.
In your NetBeans project open the “Restful Web Services” node and right-click
“ContactsResource”. Choose “Test Resource Uri” from the context menu. This will open the
resource in the browser. You can now copy the URL from the Browsers address field.
Testing the client
Now we’re ready to run the client. Run project “crud General Client Code”.
In the URL field paste the URL you just copied and connect. That’s it:
The addresses you enter will now be persisted via JPA.