CRUD Example with Wildfly

This blog post is an answer to a question recently asked in the DukeScript Forum. The question is about our CRUD archetype, which contains a very simple Jersey based REST Server: “How do you suggest I deploy my web application to the Wildfly Application server?” Since this is not the first time I’ve been asked this question, I thought it’s worth a little tutorial.

I’m using NetBeans 8.0.2 with the latest version of the DukeScript Plugin and WildFly 8.2.0 for this. I’m assuming you have already created a project using the CRUD Archetype.

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 more traditional web project, e.g. using the Maven Web Application project template.

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:

@javax.ws.rs.ApplicationPath("webresources")
public class ApplicationConfig extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new java.util.HashSet<>();
        addRestResourceClasses(resources);
        return resources;
    }

    /**
     * Do not modify addRestResourceClasses() method.
     * It is automatically populated with
     * all resources defined in the project.
     * If required, comment out calling this method in getClasses().
     */
    private void addRestResourceClasses(Set<Class<?>> resources) {
        resources.add(fully.qualified.path.to.ContactsResource.class);
    }
    
}

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.