Preloader image

In this example a web service is created that stores and retrieves data from the database. Jersey will be used to create the web service. For the database, EclipseLink JPA provider will be used.

The web service will store and retrieve information about persons:

@NamedQuery(name = "Person.findAll", query = "select p from Person p")
public class Person {

    private long id;
    private String name;

    public long getId() {
        return id;

    public void setId(long id) { = id;

    public String getName() {
        return name;

    public void setName(String name) { = name;

The two services our application provide are: persisting a person, retrieving all persons from the database:

public class PersonDAO {

    private EntityManager em;

    public Person save(final String name) {
        final Person person = new Person();
        return person;

    public List<Person> findAll() {
        return em.createNamedQuery("Person.findAll", Person.class).getResultList();

Next, we are ready to expose as a service this business logic:

public class PersonService {

    private PersonDAO dao;

    public PersonService() {

    public Person create(@PathParam("name") final String name) {

    public List<Person> list() {
        return dao.findAll();

Now, that we have a service class, defining the resources provided by the application, we extent the class and we add our PersonService:

public class JerseyApplication extends Application {

    public Set<Class<?>> getClasses() {
        final Set<Class<?>> classes = new HashSet<Class<?>>();
        return classes;

There is one more step to do, to configure a servlet provided by Jersey. This can be done in webapp/WEB-INF/web.xml:

<web-app xmlns=""
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-name>Jersey Web Application</servlet-name>

The web service is ready to be tested. For this, we will write an unit test using Arquillian:

public class JerseyApplicationTest {

    @Deployment(testable = false)
    public static WebArchive createDeployment() {
        return ShrinkWrap.create(WebArchive.class, "jersey-application.war")
                .addAsManifestResource(new FileAsset(new File("src/main/webapp/WEB-INF/web.xml")), "web.xml")
                .addAsManifestResource(new ClassLoaderAsset("META-INF/persistence.xml"), "persistence.xml")
                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");

    private URL webapp;

    public void test() {

        String allPersons = get("person/all");


    private String get(String url) {
        final CloseableHttpClient client = HttpClients.custom().build();
        final HttpHost httpHost = new HttpHost(webapp.getHost(), webapp.getPort(), webapp.getProtocol());
        final HttpClientContext context = HttpClientContext.create();

        final HttpGet get = new HttpGet(webapp.toExternalForm() + url);
        CloseableHttpResponse response = null;
        try {
            response = client.execute(httpHost, get, context);
            return EntityUtils.toString(response.getEntity());
        } catch (final IOException e) {
            throw new IllegalStateException(e);
        } finally {
            try {
            } catch (final IOException e) {
                // no-op

We use Arquillian to programmatically start a new test container. In a test a person is persisted, then it’s presence in database is checked by retrieving all person entities.

Full example can be found here. It’s a maven project, and the test can be run with mvn clean install command.