Spring web application on WildFly


In this post, we are going to build a simple spring web application and deploy it in WildFly 8 application server. WildFly formerly know as JBoss Application Server. This release is Java EE7 certified, supporting both the Web and the Full profiles.


We are going to leverage the default data source defined in the Wildfly 8 for H2 in memory database. If you open standalone.xml file under the following directory wildfly-8.0.0.Final\standalone\configuration, You can see the following entry for the data source.
If you want to define your own data source for Oracle, MySQL etc. You can introduce one datasource element by filling necessary details along with JNDI name

Step 1: We will use the above JNDI to create our datasource bean in Spring configuration as:
Step 2: Container Managed Entity Manager will be used in our application to demonstrate the configuration of container managed entity manager in WildFly 8.
We will create a enity manager factory by looking up the JNDI registered as java:comp/env/persistence/my-emf in WildFly Application Server.

Step 3: To enable application that uses a server deployed persistence unit, It must register persistence context's or the persistence unit's JNDI via web.xml as follows:
JNDI binding
JNDI binding via persistence.xml properties is not supported in JBoss AS 7.0
Migration Guide for spring

Our Spring application context will look like this.
appContext.xml
Complete version of appContext.xml is available in source code


Now here we have one sample application consist of a REST Service "EmployeeRestService", one service class "EmployeeService" and Spring Data JPA Repository "EmployeeRepository" as DAO layer which in tern talk to underlying database.



Now we will start writing some code to understand our application implementation.
We will keep our entity as simple as possible. We will create a Employee POJO for our example as:
Employee.java
package com.sarf.domain;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@Table(name = "employee")
public class Employee implements Serializable {

 private static final long serialVersionUID = 1L;
 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 @Column(name = "employeeId")
 private int nEmployeeId;
 @Column(name = "employeeName")
 private String strEmployeeName;

 //Setter and Getter methods
}

We will define DAO layer as Spring Data Repository class which will perform CRUD operations for us 

EmployeeRepository.java
package com.sarf.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.sarf.domain.Employee;

@Repository()
public interface EmployeeRepository 
 extends JpaRepository<Employee,integer> {
 }

Our service class will perform employee entry and retrieval of employees
EmployeeService.java
package com.sarf.service;

import java.util.List;
import com.sarf.domain.Employee;

public interface EmployeeService {
 public void addEmployee(String emolpyeeName);
 public List<Employee> getEmployees();
}

EmployeeServiceImpl.java
package com.sarf.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.sarf.domain.Employee;
import com.sarf.repository.EmployeeRepository;

@Service("employeeService")
@Transactional
public class EmployeeServiceImpl implements EmployeeService {

 @Autowired
 private EmployeeRepository employeeRepository;

 /** {@inheritDoc} */
 public void addEmployee(final String employeeName) {
  final Employee e = new Employee();
  e.setStrEmployeeName(employeeName);
  this.employeeRepository.saveAndFlush(e);
 }
 public List<Employee> getEmployees() {
  return this.employeeRepository.findAll();
 }
}

REST full service will define two operations : addEmployee and getEmployees
EmployeeRestService.java
package com.sarf.restservice;

import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.sarf.domain.Employee;

@Path("/employee-service/")
@Produces(MediaType.APPLICATION_JSON)
public interface EmployeeRestService {

 @POST
 @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
 @Path("addEmployee")
 public void addEmployee(@FormParam("employeeName") String employeeName);

 @GET
 @Consumes(MediaType.APPLICATION_JSON)
 @Path("getEmployees")
 public List<Employee> getEmployees();
}


EmployeeRestServiceImpl.java
package com.sarf.restservice;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.sarf.domain.Employee;
import com.sarf.service.EmployeeService;

@Service
@Transactional
public class EmployeeRestServiceImpl implements EmployeeRestService {
 @Autowired
 private EmployeeService employeeService;

 public void addEmployee(final String employeeName) {
  this.employeeService.addEmployee(employeeName);
 }

 public List<Employee> getEmployees() {
  final List<Employee> listEmployee = 
  this.employeeService.getEmployees();
  return listEmployee;
 }
}

Our final persistence.xml will look like

persistence.xml


web.xml
data.sql
 Click here to get source code

Comments are closed.