This guide provides a complete setup for deploying a traditional Spring application on WebSphere Liberty using ojdbc8 driver and Java 8, focusing on the backend components without views.
packagecom.example.dao;importcom.example.model.Employee;importorg.springframework.stereotype.Repository;importjavax.persistence.EntityManager;importjavax.persistence.PersistenceContext;importjavax.persistence.TypedQuery;importjava.util.List;@RepositorypublicclassEmployeeDAO{@PersistenceContextprivateEntityManagerentityManager;publicList<Employee>getAllEmployees(){TypedQuery<Employee>query=entityManager.createQuery("SELECT e FROM Employee e ORDER BY e.id",Employee.class);returnquery.getResultList();}publicEmployeefindById(Longid){returnentityManager.find(Employee.class,id);}publicvoidsave(Employeeemployee){if(employee.getId()==null){entityManager.persist(employee);}else{entityManager.merge(employee);}}publicvoiddelete(Longid){Employeeemployee=findById(id);if(employee!=null){entityManager.remove(employee);}}publicList<Employee>findByDepartment(Stringdepartment){TypedQuery<Employee>query=entityManager.createQuery("SELECT e FROM Employee e WHERE e.department = :dept",Employee.class);query.setParameter("dept",department);returnquery.getResultList();}}
Create a file at src/main/java/com/example/config/WebConfig.java:
packagecom.example.config;importorg.springframework.context.annotation.ComponentScan;importorg.springframework.context.annotation.Configuration;importorg.springframework.web.servlet.config.annotation.EnableWebMvc;@Configuration@EnableWebMvc@ComponentScan(basePackages="com.example.controller")publicclassWebConfig{// Additional configuration can go here if needed}
AppConfig.java
Create a file at src/main/java/com/example/config/AppConfig.java:
Create a file at src/main/java/com/example/config/AppInitializer.java:
packagecom.example.config;importorg.springframework.web.WebApplicationInitializer;importorg.springframework.web.context.ContextLoaderListener;importorg.springframework.web.context.support.AnnotationConfigWebApplicationContext;importorg.springframework.web.servlet.DispatcherServlet;importjavax.servlet.ServletContext;importjavax.servlet.ServletException;importjavax.servlet.ServletRegistration;publicclassAppInitializerimplementsWebApplicationInitializer{@OverridepublicvoidonStartup(ServletContextservletContext)throwsServletException{// Root application context (equivalent to applicationContext.xml)AnnotationConfigWebApplicationContextrootContext=newAnnotationConfigWebApplicationContext();rootContext.setConfigLocation("com.example.config");// package with @Configuration classesservletContext.addListener(newContextLoaderListener(rootContext));// Dispatcher servlet context (equivalent to dispatcher-servlet.xml)AnnotationConfigWebApplicationContextdispatcherContext=newAnnotationConfigWebApplicationContext();dispatcherContext.register(WebConfig.class);// your WebConfig classServletRegistration.Dynamicdispatcher=servletContext.addServlet("dispatcher",newDispatcherServlet(dispatcherContext));dispatcher.setLoadOnStartup(1);dispatcher.addMapping("/");}}
Logging Configuration
Create a file at src/main/resources/log4j.properties:
# Root logger option
log4j.rootLogger=INFO, stdout# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.outlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n# Hibernate logging options
log4j.logger.org.hibernate=INFOlog4j.logger.org.hibernate.SQL=DEBUGlog4j.logger.org.hibernate.type.descriptor.sql.BasicBinder=TRACE# Spring logging
log4j.logger.org.springframework=INFO
Building and Deployment Steps
Build the application:
mvn clean package
Create and configure Liberty server:
mvn liberty:create liberty:install-feature
Deploy the application:
mvn liberty:deploy
Start the Liberty server:
mvn liberty:start
Your REST API will be accessible at:
http://localhost:9080/api/employees
API Endpoints
Method
URL
Description
GET
/api/employees
Get all employees
GET
/api/employees/{id}
Get employee by ID
POST
/api/employees
Create a new employee
PUT
/api/employees/{id}
Update an employee
DELETE
/api/employees/{id}
Delete an employee
GET
/api/employees/department?name={dept}
Get employees by department
Testing with curl
Here are some curl commands to test your API:
Get all employees:
curl -X GET http://localhost:9080/api/employees
Get a specific employee:
curl -X GET http://localhost:9080/api/employees/1
Create a new employee:
curl -X POST http://localhost:9080/api/employees \-H"Content-Type: application/json"\-d'{"name":"John Doe","department":"IT","salary":75000,"email":"john.doe@example.com"}'
Update an employee:
curl -X PUT http://localhost:9080/api/employees/1 \-H"Content-Type: application/json"\-d'{"name":"John Doe","department":"Engineering","salary":80000,"email":"john.doe@example.com"}'
curl -X GET http://localhost:9080/api/employees/department?name=IT
Important Notes
Replace the database connection details in server.xml with your actual Oracle database credentials.
The Oracle JDBC driver (ojdbc8.jar) needs to be accessible to your Liberty server. Make sure it's properly located in the server's lib directory.
If you encounter class loading issues, verify that the commonLibraryRef attribute in the application tag in server.xml is correctly pointing to your Oracle library.
This application uses JPA/Hibernate for database operations. The hibernate.hbm2ddl.auto=update property will attempt to create/update tables in your database schema.
Liberty's JPA implementation might differ slightly from standalone Hibernate. If you encounter any JPA-related issues, consult the WebSphere Liberty documentation for specifics.
When deploying in a production environment, remember to adjust the logging levels and security settings appropriately.