Category Archives: Java

Error Handling and Logging in Spring Boot REST API – Part III

In previous posts, I wrote about how to create a spring boot REST API Part I and how to add swagger documentation for REST API Part II. In this post, we will add error handling and logging to our REST API. Error handling and Logging are two different ideas, so I will divide this post in two sections.

1. Logging

In most production applications, logging is critical and it is used for multiple purposes. Few of those uses are debugging the production issues or auditing for the application. Over the years, different logging libraries have evolved to use in java based applications. slf4j is the most popular framework as it provides a simple abstraction layer to any kind of logging framework.

In our tutorial for this application, we will be using log4j2 which is the most recent and advance logging library out there. It provides lot of useful features for performance, support for multiple APIs, advance filtering, automatic reloading of configurations etc. We will not cover any of these in this article, if you are interested to read about log4j2 libraries, read here.

Add log4j2 library in application –

To use log4j2, we will add the maven dependency to our project’s pom file. This should look like below

 <dependency>
 <groupId>org.apache.logging.log4j</groupId>
 <artifactId>log4j-api</artifactId>
 </dependency>
 <dependency>
 <groupId>org.apache.logging.log4j</groupId>
 <artifactId>log4j-core</artifactId>
 </dependency>

Add log4j2 configuration file

To enable logging, we will have to add a configuration file in our application. This configuration file can be XML, JSON or YAML file. We will be using a XML file log4j2.xml which will look like below

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
 <Appenders>
 <Console name="Console" target="SYSTEM_OUT">
 <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
 </Console>
 <File name="BenefitsFile" fileName="benefits.log" append="true">
 <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
 </File>
 </Appenders>
 <Loggers>
 <Root level="debug">
 <AppenderRef ref="Console" />
 <AppenderRef ref="BenefitsFile"/>
 </Root>
 </Loggers>
</Configuration>

So we are using Console  and BenefitsFile as two loggers which will log into a console and file respectively. We are setting log level to DEBUG. If you log any messages with a level lower than DEBUG, they will be logged into console or file. We will have to add a file benefits.log in classpath to achieve this logging in file. Log pattern is with date time, log level, class from which log is originating and log message.

Add logging in application code

Once we have required logging libraries and logging configuration adjusted, we can add logging in our code to capture this logging during runtime execution. In one of the managers CompanyManagerImpl, we will add a logger.

public static final Logger LOGGER = LogManager.getLogger(CompanyManagerImpl.class);

@Override
public List<Company> getAllCompanies()
{
  LOGGER.info(" Enter >> getAllCompanies() ");
  List<Company> cList = (List<Company>) companyRepository.findAll();
  LOGGER.info(" Exit << getAllCompanies() ");
  return cList;
}

Now once we execute our spring boot application, we can capture the logs in console or file. The file will be benefits.log.

2. Error Handling

We will not write about exceptions in detail as it has been covered in this post Exceptions. We will create our own custom exception which will be extended from WebApplicationException which jersey library provides.

This will look like below:

package com.betterjavacode.benefits.utilities;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

public class InvalidRequestException extends WebApplicationException 
{

  /**
  *
  */
  private static final long serialVersionUID = 1L;
  private int errorcode = 00; // 00 indicates - no error

  public InvalidRequestException() 
  {

  }

  public InvalidRequestException(int errorcode, String message) 
  {
    super(Response.status(Response.Status.BAD_REQUEST).entity(message).build());
    this.errorcode = errorcode;
  }

  public InvalidRequestException(int errorcode, String message, Throwable cause) 
  {
     super(cause, Response.status(Response.Status.BAD_REQUEST).entity(message).build());
     this.errorcode = errorcode;
  }
}

Now we can use this custom exception in our managers when we want to throw an error message to indicate if there is anything wrong with client request. Similarly we can build another exception to show if there is anything wrong on server side. Following snippet shows from CompanyManagerImpl where we have shown how to throw this exception.

@Override
public Company getCompany(int guid) 
{
  LOGGER.info(" Enter >> getCompany() ");
  Company company = companyRepository.findOne(guid);
  if (company == null) {
    LOGGER.info(" Exit << createCompany() ");
    throw new InvalidRequestException(400, "Company not found");
  }
  LOGGER.info(" Exit << getCompany() ");
  return company;
}

In this post, we showed how to handle logging and errors in a REST API. The code for this is available on github repository.

 

How To – Spring Boot CRUD Rest API Example – Part I

As part of this post, we will learn how to write a CRUD Rest API using Spring Boot. Spring boot provides some cool features to create a production-ready Spring application that can be deployed as a war file on any environment. This will be a series of posts, but we will start with the creation of a simple REST API.

What you’ll need 

  1. Eclipse Mars.2 Release
  2. Java version 1.8
  3. MySQL 5.0 or higher
  4. Maven 3.0 or higher

What we’ll cover 

In this article, we will cover the following items

  1. Create a Maven project
  2. Assemble pom file for all dependencies
  3. Create entity classes
  4. Business logic to handle data
  5. A REST controller
  6. Run the API in tomcat

Create a Maven project

As the first step, let’s create a maven project in eclipse. You can create this by going into File > New > Maven Project.

Select an Archtype as maven-archtype-webapp.

Enter artifactid as benefits and groupid as com.betterjavacode

Assemble pom file for all dependencies

We will be using spring-boot and all the required dependencies including spring-data. Spring data JPA provides a lot of useful enhancements that you can seamlessly use with spring-boot project. Spring-data will cover the data access layer which basically implements persistence. Once we use spring-data, we don’t have to add any external hibernate or eclipselink JPA APIs. Also, some of the data access repositories provided by spring-data make implementing data access layer code less worrisome.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.betterjavacode</groupId>
 <artifactId>Benefits</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <name>Benefits Maven Webapp</name>
 <url>http://maven.apache.org</url>
 <parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>1.4.2.RELEASE</version>
 </parent>
 <dependencies>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 </dependency>

 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-data-jpa</artifactId>
 </dependency>
 <dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <scope>runtime</scope>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-jdbc</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-test</artifactId>
 </dependency>
 <dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-api</artifactId> 
 </dependency>
 <dependency>
 <groupId>org.apache.logging.log4j</groupId>
 <artifactId>log4j-api</artifactId>
 </dependency>
 <dependency>
 <groupId>org.apache.logging.log4j</groupId>
 <artifactId>log4j-core</artifactId>
 </dependency>
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <scope>test</scope>
 </dependency>
 </dependencies>
 <build>
 <plugins>
 <plugin>
 <artifactId>maven-compiler-plugin</artifactId>
 <version>3.3</version>
 <configuration>
 <source>1.8</source>
 <target>1.8</target>
 </configuration>
 </plugin>
 <plugin>
 <artifactId>maven-war-plugin</artifactId>
 <version>2.6</version>
 <configuration>
 <warSourceDirectory>WebContent</warSourceDirectory>
 <failOnMissingWebXml>false</failOnMissingWebXml>
 </configuration>
 </plugin>
 </plugins>
 <finalName>Benefits</finalName>
 </build>
</project>

Create entity classes

We will be creating a rest API for Benefits service which will have companies and users as main objects. We are only covering basic data model classes at the moment, but as part of the series, we will develop a web application. Each company will have a company profile and each user will user profile. So we will have four basic entities Company, CompanyProfile, User, UserProfile.

package com.betterjavacode.benefits.entities;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity(name = "Company")
@Table(name = "company")
public class Company implements Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;

public Company() {

}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column
private String name;

@Column
private int statusid;

@OneToOne(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
@JoinColumn(name = "companyprofileid")
private CompanyProfile cp;

@Column
private String type;

@Column
private String ein;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getStatusid() {
return statusid;
}

public void setStatusid(int statusid) {
this.statusid = statusid;
}

public CompanyProfile getCp() {
return cp;
}

public void setCp(CompanyProfile cp) {
this.cp = cp;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getEin() {
return ein;
}

public void setEin(String ein) {
this.ein = ein;
}

}

package com.betterjavacode.benefits.entities;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity(name = "User")
@Table(name = "user")
public class User implements Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;

public User() {

}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column
private Date createdate;

@Column
private String email;

@Column
private String firstname;

@Column
private String middlename;

@Column
private String lastname;

@Column
private String username;

@Column
private String jobtitle;

@OneToOne(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
@JoinColumn(name = "userprofileid")
private UserProfile userprofile;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public Date getCreatedate() {
return createdate;
}

public void setCreatedate(Date createdate) {
this.createdate = createdate;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getFirstname() {
return firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public String getMiddlename() {
return middlename;
}

public void setMiddlename(String middlename) {
this.middlename = middlename;
}

public String getLastname() {
return lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getJobtitle() {
return jobtitle;
}

public void setJobtitle(String jobtitle) {
this.jobtitle = jobtitle;
}

public UserProfile getUserprofile() {
return userprofile;
}

public void setUp(UserProfile up) {
this.userprofile = up;
}

}

Business logic to handle the data

Part of our architecture for REST API, we will have the following three layers

  1. Rest layer
  2. Business object layer
  3. Data access layer

So in the Business object layer, we will implement all the managers which will handle the processing of rest requests to create, update, read, or delete the data. In subsequent posts, we will enhance this layer to handle logging, error handling, and more.

package com.betterjavacode.benefits.managers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.betterjavacode.benefits.entities.User;
import com.betterjavacode.benefits.interfaces.UserManager;
import com.betterjavacode.benefits.repositories.UserRepository;

public class UserManagerImpl implements UserManager {

private UserRepository userRepository;

@Autowired
public void setUserRepository(UserRepository userRepository) {
this.userRepository = userRepository;
}

@Override
public User createUser(User u) {
if (u != null) {
User user = userRepository.save(u);
return user;
} else {
return null;
}
}

@Override
public User updateUser(User u) {
// TODO Auto-generated method stub
return null;
}

@Override
public User getUser(int id) {
User user = userRepository.findOne(id);
if (user == null) {
return null;
}
return user;
}

@Override
public List getAllUsers() {
List userList = (List) userRepository.findAll();
return userList;
}

@Override
public void deleteUser(int guid) {
// TODO Auto-generated method stub
User user = userRepository.findOne(guid);
if (user == null) {
return;
}
userRepository.delete(user);
}

}

A REST controller

One of the best uses of Spring boot is to create rest API and the feature it offers for the same is to use the REST controller. Spring-boot offers an annotation for the same as @RestController.

package com.betterjavacode.benefits.controller;

import java.util.List;

import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.betterjavacode.benefits.entities.User;
import com.betterjavacode.benefits.interfaces.UserManager;

@RestController
@RequestMapping("benefits/v1")
public class UserService {

@Autowired
UserManager userMgr;

@RequestMapping(value = "/users/", method = RequestMethod.POST)
public User createUser(User user) {
User u = userMgr.createUser(user);
return u;
}

@RequestMapping(value = "/users/{id}", method = RequestMethod.GET)
public User getUser(@PathVariable("id") int id) {
User u = userMgr.getUser(id);
return u;
}

@RequestMapping(value = "/users/", method = RequestMethod.GET)
public List getAllUsers() {
List cList = userMgr.getAllUsers();
return cList;
}

@RequestMapping(value = "/users/", method = RequestMethod.PUT)
public User updateUser(User user) {
User u = userMgr.updateUser(user);
return u;
}

@RequestMapping(value = "/users/{id}", method = RequestMethod.DELETE)
public Response deleteUser(@PathVariable("id") int id) {
userMgr.deleteUser(id);
return Response.status(Response.Status.OK)
.build();
}
}

package com.betterjavacode.benefits.controller;

import java.util.List;

import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.betterjavacode.benefits.entities.Company;
import com.betterjavacode.benefits.interfaces.CompanyManager;

@RestController
@RequestMapping("benefits/v1")
public class CompanyService {

@Autowired
CompanyManager compMgr;

@RequestMapping(value = "/companies/", method = RequestMethod.POST)
public Company createCompany(Company company) {
Company c = compMgr.createCompany(company);
return c;
}

@RequestMapping(value = "/companies/{id}", method = RequestMethod.GET)
public Company getCompany(@PathVariable("id") int id) {
Company c = compMgr.getCompany(id);
return c;
}

@RequestMapping(value = "/companies/", method = RequestMethod.GET)
public List getAllCompanies() {
List cList = compMgr.getAllCompanies();
return cList;
}

@RequestMapping(value = "/companies/", method = RequestMethod.PUT)
public Company updateCompany(Company company) {
Company c = compMgr.updateCompany(company);
return c;
}

@RequestMapping(value = "/companies/{id}", method = RequestMethod.DELETE)
public Response deleteCompany(@PathVariable("id") int id) {
compMgr.deleteCompany(id);
return Response.status(Response.Status.OK)
.build();
}
}

Run the API in tomcat

We are using embedded tomcat in this Spring-boot project. So once we are done building and installing the code through maven, we can run the project through eclipse or standalone war file in tomcat. For our demo purposes, we will run this application through the eclipse, which will start embedded tomcat.

If we execute the URL http://localhost:8080/benefits/v1/users/1 – it will display JSON for user data as below

Result of Spring Boot REST CRUD API

You can find the source code for this project Github Repository.

How To – Concepts of Websphere

In the enterprise Java application world, Websphere is the most used application server. IBM has created WebSphere as its product for a long time now. Other alternatives have been JBoss and Tomcat. (Though tomcat is not a full-fledged application server and there is a debate about it.)

In this post, we will discuss the basic concepts of IBM Websphere Application Server. If you have any questions, please post them in the comment and I will try to answer them to the best of my abilities.

Application Server

The primary component of IBM WebSphere is an application server. The server runs the actual code of your application. Each server runs its own Java Virtual Machine (JVM). All configurations can have one or more application servers. In other words, an application server can run on only one node, but one node can support many application servers.

Node

It is a logical group of application server processes that share common configuration repositories. A single node is related to a single profile. Likewise, one machine can have more than one node. A node can contain zero or more application servers.  An XML file stores the configuration information that Node is useful for.

Cell

A cell is a grouping of nodes into a single administrative domain. A cell can consist of multiple nodes, all administered from a deployment manager server.

Node Agent

A node agent is created on Node when a node is federated. The node agent works with the deployment manager for administrative activities.

Deployment Manager

Above all, with the deployment manager, you can administer multiple nodes from one centralized manager. This deployment manager works with node agent on each node. Therefore, application server nodes must be federated with the deployment manager before they can be managed by the deployment manager.

In conclusion, we discussed the basic concepts of the IBM WebSphere application server. Hence, subscribe to my blog here.

 

java.lang.NoSuchMethodError javax.servlet.ServletContext.getVirtualServerName()

Recently while working on my Spring Boot project, I came across an error method not found getVirtualServerName. This error was very frequent and bothersome. During the execution of Spring Boot project, I ran into NoSuchMethodError javax.servlet.ServletContext.getVirtualServerName() exception.  Despite going through maven dependency, it was harder to find out where the servlet-api jar was coming into my project build path. In my handling exceptions post, I showed how to handle exceptions better.

Issue –

java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[]]
at java.util.concurrent.FutureTask.report(FutureTask.java:122) [na:1.8.0_91]
at java.util.concurrent.FutureTask.get(FutureTask.java:192) [na:1.8.0_91]
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:911) ~[tomcat-embed-core-8.5.6.jar:8.5.6]
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:890) [tomcat-embed-core-8.5.6.jar:8.5.6]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.5.6.jar:8.5.6]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1403) [tomcat-embed-core-8.5.6.jar:8.5.6]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1393) [tomcat-embed-core-8.5.6.jar:8.5.6]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_91]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_91]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_91]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_91]
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167) [tomcat-embed-core-8.5.6.jar:8.5.6]
... 6 common frames omitted
Caused by: org.apache.catalina.LifecycleException: Failed to start component [Pipeline[StandardEngine[Tomcat].StandardHost[localhost].StandardContext[]]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167) [tomcat-embed-core-8.5.6.jar:8.5.6]
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5099) ~[tomcat-embed-core-8.5.6.jar:8.5.6]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.5.6.jar:8.5.6]
... 6 common frames omitted
Caused by: org.apache.catalina.LifecycleException: Failed to start component [org.apache.catalina.authenticator.NonLoginAuthenticator[]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167) [tomcat-embed-core-8.5.6.jar:8.5.6]
at org.apache.catalina.core.StandardPipeline.startInternal(StandardPipeline.java:170) ~[tomcat-embed-core-8.5.6.jar:8.5.6]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.5.6.jar:8.5.6]
... 8 common frames omitted
Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;
at org.apache.catalina.authenticator.AuthenticatorBase.startInternal(AuthenticatorBase.java:1125) ~[tomcat-embed-core-8.5.6.jar:8.5.6]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.5.6.jar:8.5.6]
... 10 common frames omitted

Reason

Depending on the build path, servlet-api jar that exists is not the right version. If it is older than 3.1, it doesn’t contain method getVirtualServerName().

How did I resolve this issue?

I describe the solution separately, but how I analyzed the issue here. I went through servlet-api jar and tried to find the class ServletContext that contains this method. But the Jar file that I had in my project, didn’t contain this method. That’s why the issue. Then it was simple to figure out based on maven dependencies. Once I had a dependency tree, I was able to nail down the dependency that was bringing the old version of servlet-api jar.

Solution –

  1. Change the version of servlet-api jar
  2. Considering this is a spring-boot project, provide a version of tomcat instead of using default tomcat 8.x which spring-boot provides.
  3. In my case, the fix was to remove javaee.jar from the build path which was bringing servlet-api of an older version. When I changed the Java runtime library from 1.8 to 1.7, the project ran like a charm.

Conclusion

In this post, I showed how to resolve the error getVirtualServerName in ServletContext. If you enjoyed this post, subscribe to my blog.