Advertise with us

Our eBooks

JAX-RS HelloWorld application

In this tutorial we will learn how to create a simple Hello JAX-RS application using as provider RESTEasy which is the REST Server of WildFly. It can run as well in any Java EE container.

Create the Maven project

In order to get started we will need a Maven archetype to bootstrap a Web application project. You can use one simple like the following one:

mvn -DarchetypeGroupId=org.codehaus.mojo.archetypes \
-DarchetypeArtifactId=webapp-javaee7 \
-DgroupId=com.mastertheboss -DartifactId=jpa-basic \
-Dversion=1.1 -Dpackage=com.mastertheboss \
-Darchetype.interactive=false --batch-mode --update-snapshots \

We will create a simple application that stores some Entity objects using the REST API. For this purpose, let's write our simple Entity class that we will persist in the Database:

package com.mastertheboss.model;

import javax.enterprise.event.Event;
import javax.inject.Inject;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.PostRemove;

public class SimpleProperty {

	private String key;

	private String value;

	public String getKey() {
		return key;

	public void setKey(String key) {
		this.key = key;

	public String toString() {
		return "SimpleProperty [key=" + key + ", value=" + value + "]";

	public String getValue() {
		return value;

	public void setValue(String value) {
		this.value = value;


Now, in order to store our Entity in a transactional way, we need an EJB:

package com.mastertheboss.ejb;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.mastertheboss.model.SimpleProperty;


public class  ServiceBean   {

	private EntityManager em;

	public void put(SimpleProperty p){


	public void delete(SimpleProperty p){

		Query query = em.createQuery("delete FROM SimpleProperty p where p.key='"+p.getKey()+"'");



	public List<SimpleProperty> findAll(){

		Query query = em.createQuery("FROM SimpleProperty");

		List <SimpleProperty> list = query.getResultList();
		return list;


	public SimpleProperty findById(String id){

		SimpleProperty p = em.find(SimpleProperty.class, id);
		return p;



Finally, we need to code our front-end REST Service for our application. Here is our basic REST Endpoint that will be added:


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;

import com.mastertheboss.model.SimpleProperty;
import com.mastertheboss.ejb.ServiceBean;

 * JAX-RS Example
 * <p/>
 * This class produces a RESTful service to read/write the contents of the members table.
public class RestService {

	private ServiceBean ejb;

	public List<SimpleProperty> listAllMembers() {
		return ejb.findAll();

	public SimpleProperty findById(@PathParam("id") String id) {

		SimpleProperty p = ejb.findById(id);
		if (p == null) {
			throw new WebApplicationException(Response.Status.NOT_FOUND);
		return p;

	public Response createMember(SimpleProperty member) {

		Response.ResponseBuilder builder = null;

		try {

			// Create an "ok" response
			builder = Response.ok("Saved!");

		} catch (Exception e) {
			// Handle generic exceptions
			Map<String, String> responseObj = new HashMap<>();
			responseObj.put("error", e.getMessage());
			builder = Response.status(Response.Status.BAD_REQUEST).entity(responseObj);



As you can see, the above REST endpoint has some basic methods for creating and querying our Entity objects, returning a JSON view of it.

In order to get it working, we need a REST Activator class:



public class RestActivator extends Application {

Done with the code, we will add a persistence.xml file to declare the mapping for our PersistenceContext objects:

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="2.1"
             xmlns="" xmlns:xsi=""
    <persistence-unit name="primary">
            <!-- Properties for Hibernate -->
            <property name="" value="create-drop" />
            <property name="hibernate.show_sql" value="false" />

As you can see, we rely on the default Datasource which ships with WildFly. Here is the final project tree structure:

├── pom.xml
└── src
    └── main
        ├── java
        │   └── com
        │       └── mastertheboss
        │           ├── ejb
        │           │   └──
        │           ├── model
        │           │   └──
        │           └── rest
        │               ├──
        │               └──
        ├── resources
        │   └── META-INF
        │       └── persistence.xml
        └── webapp
            ├── index.html

Now compile the project:

$ mvn clean install

The deploy it on WildFly by copying it, for example, in the deployments folder.

Accessing the REST Service

In order to access our REST Service we will need to understand how the REST String will be composed. As the REST Activator has been bound on the "/rest" URL and our application Web context name is "jpa-basic", the REST URL will be:


Then we need to add the specific URL specified in the REST Service. For example, in order to execute a POST with cURL:

$ curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"key":"xyz","value":"xyz"}' \

We can check that data was inserted with:

$ curl http://localhost:8080/jpa-basic/rest/action


Much the same, we can query by id. It will return the same id:

$ curl http://localhost:8080/jpa-basic/rest/action/id/xyz


That's all! Enjoy your first REST application example on WildFly!

You can find the source code for this REST/JPA application on:

© 2021 All Rights Reserved.

Please publish modules in offcanvas position.