Category Archives: Spring-boot

Building a Saas application

This is a brainstorm post where I will jot down the ideas to build a saas application. Before we start, we have to go to basics.

What is a Saas?

Software as a service (Saas) is a software delivery model. In this model, software is served through subscription service. Saas has been popular for more than a decade now. In fact, the sales of such software has sky rocketed that building a simple software has become easier. From project management to ordering a healthy food, we can get any of these services through software with subscription.

Now what do we want to build and how do we start?

Of course, this is not an easy question to answer in a single post. Lot of times, there are lot of trial and errors you have to go through to build a viable product that people will use it. But also what and who are we targeting as an audience. There are lot of broader areas to think to build a product. That would make the entire process to build a software way too complex. So where do we start? The eternal question still remains. Human psychology over the years has progressed and helped technology to build lot of cool products. With AI has been knocking on our doors, what we build today, will be obsolete in next ten years. Based on own experience, what I have found, is that look into your own daily life. When you go for grocery shopping, when you talk to your friends, coworkers. The moment, you feel frustrated anything that is not in your control, that’s where you have something to build on.

I know it sounds ridiculously easy to write here in the post, but not easy when you are living the life. What I am trying to point is, look at problems you or other human face and if that problem can be solved through a software, you have got a viable product idea.  Every pain point, problem is an idea to build a product. Simple example – Elon Musk was driving on LA roads, he was caught in a traffic which didn’t move for long time. How do we improve our traffic? With increasing cars and population, this is almost going to be a night mare in future. He realized the problem and started a company called The Boring Company which will build underground tunnels for handling traffic.

Back to our idea storming session about building a solution for what problem. If you are like me who works in a software company, it is easy to see through this dilemma to build a solution that can help you and other developers equally. But in a larger context, you can always go through different Saas services and hear the feedback from those services’ users. Any negative feedback is your path to build a product. Assuming we got the idea to build a Saas product, so how do we proceed further?

Post-idea discussion

Once we have a solid idea, we can think about building a minimum viable product which gives customers chance to explore the product with minimum fuss. Less complex the product is for customers to use intuitively, better will be their experiences and happier they will be to recommend your product to others. So one of the major aspect you should work on after a solid idea, is to create a minimum viable design. This will be alpha version of the product. Getting alpha version out of the door in minimum time will give you better idea where to focus on scaling the product in future. This will also save time and money.

Technology and Frameworks

Once we have initial design of minimum product, we can think of what technology and framework to use. What kind of infrastructure to use? Considering less expensive option, cloud is very popular to use to build a Saas product. This reduces the management of infrastructure while giving high availability and scalability. Amazon, Google and Microsoft all these companies offer cloud solution to build your application. Also if you want to scale your application in future for data intensive, cloud is the best option to handle all kinds of load.

For backend, there are different frameworks available based on C#, Python or Java. Since I have worked on Java, I vouch for Spring which offers lot of flexibility and ease to add lot of code easily. Of course, there is a learning curve if you have never used spring before. For database, we have two major options, one is SQL based database or NoSQL. If it is data intensive application, NoSQL makes more sense.

On frontend side, angularjs offers lot of ease to build a modern user interface to interact with backend.

Conclusion

There are lot of other factors we have not considered in this discussion especially related to performance and health of the application. Also we didn’t discuss any major approach to build the application. I hope this brain storming post will give readers an idea what possibilities are out there to build something that is helpful.

Advertisements

Spring Boot and Microservices

Over the last few months, I have worked on Spring Boot and tried to collect my knowledge around Microservices. I was discussing lot of this with my friends and one friend did suggest me to write a book. Initially I was little hesitant to write a book about something I was learning. But also the whole point of learning is to teach someone at some point of time.

So I took this as a challenge to write a book about Spring Boot and Microservices. Initially I created series of posts and posted on this blog to see how much it could benefit people. And now to make it easy for every one, I have collected all this information and wrote an ebook. This ebook Spring Boot and Microservices is free to download. I hope this will help people to understand the concepts of Microservices and my example can help them to head start the building their projects.

Spring Boot and Microservices

What’s next?

Where do we go from here? There are lot of questions about the next strategy about Spring Boot and Microservices. What I have covered in this book, is a tiny portion of big picture. There is lot of options like scaling the service, adding health check for the service, deploying the service on cloud with automation. But for right now, I just want to take a break from thinking about this and I will come up with a next plan soon.

Till that time, you can download, read and send me your feedback about the book. It will be great if you can leave a review for the book here.

If you have any questions, please leave your comments on this blog and I will try my best to answer them.

How to deploy Spring Boot Application on docker – Part IX

What is docker

Docker is a container platform delivered by a company named “Docker Inc.” Docker can be used by developers, operators and enterprise users to deliver and use packaged software. Docker has something called a container. Container can be a virtual machine (VM) in lay man’s terms, but still little different from VM. Container contains packaged software delivered in a way that it can be run isolated on a shared operating system. As per official definition – Unlike VMs, container does not bundle a full operating system – only libraries and settings required to make the software work are needed.

In this demo, we will use our spring boot application built throughout from Part I to Part VIII.

I am using Windows Platform Toolbox for docker to build my docker containers. 

We will build a container with mysql database deployed and another container where we will deploy spring boot application. This spring boot application container will connect to mysql database container at runtime. The process is little complicated, but once you get your hands on docker container, it becomes very straight forward to understand. Also for this post, I will not explain anything about spring boot application. You should review all the previous posts I have written explaining how to build and deploy spring boot application on an embedded tomcat.

Building a docker container with mysql

Few things to remember

  1. Make sure your spring boot application is working with mysql database before you build a container.
  2. If your application contains user administration and password, make sure you have a super administrator whose password you can insert in database with password_hash. This is specifically true for the application we will be deploying in docker container.

For most standard applications (like mysql, java 8, spring-boot), there are number of images available in docker hub. When we will run our docker container for database, docker shell will pull the version of that application from the hub to build a container. We will not be creating any new or blank docker image. To run a docker container with mysql version 5.6, we will use below command.

 

  • Name of our docker container is benefitsmysql.
  • We are not using any password. This is not recommended for production code, I am just doing this for demo purposes.
  • Database name is benefitsmysql.
  • Also this database is running at port 3308 to 3306 of local host machine.
  • -d to tell Docker to daemonize the container and keep it running.
  • mysql:5.6 to download MySQL 5.6 Server image from Docker public repo

Once this is started, there are couple of ways you can verify if we are able to connect to this database or not.

Get the ip address of this container host with command docker-machine ip . Now in mysql administrator workbench, access the mysql server with ip address and port 3308 and see if you can access the database.

Another way on docker shell terminal – use this command docker exec -it benefitsmysql -l , this will connect you as a root to the shell where mysql is installed. And then you can use mysql as regular command to access mysql.

To run our Spring boot application successfully, once you access mysql, create following tables:

 

Building a docker image for Spring Boot Application along with mysql

To dockerize my spring boot application, we will use a maven plugin to build a docker image.

I am passing dockerDirectory where Dockerfile will be stored to build our image. Also another change I have made to my original pom file, is that i have added packaging as jar.

I have also changed in my application.properties to point to mysql database container by updating database url with ipaddress of docker container.

spring.datasource.url=jdbc:mysql://192.168.99.100:3308/benefitsmysql

My Dockerfile to build a docker image is as below:

Basically this will build a Benefits.jar using java 8 and will expose port 8443 that I can use to access my application.

Now build a new docker container image by using maven goal as

mvn clean package docker:build

To run the application

docker run -p 8443:8443 --name benefits --link benefitsmysql:mysql -d containerid

This will execute the jar built within that container. Important to note here is --link as it links other container where we have mysql server deployed. So we are linking two containers and we will call the database from our spring boot application container. The same command can be used little differently to see the detail execution log as below

docker run -p 8443:8443 --name benefits --link benefitsmysql:mysql -it containerid

 

Executing the application

Once the application starts successfully, we will access our application with url https://192.168.99.100:8443/home , this will look like below:

DockerContainerSpringAppLogin

Another note – Make sure to update ip addess in all angular js references.

In this post, we showed how we can deploy Spring boot application connected to mysql on a docker container. Code for this post will be available on github repository here

References

To write my post, I used following references

  1. Docker
  2. Connection refused error
  3. Spring Boot docker

Microservices – A Primer

What is Microservices?

Wikipedia definition says

Microservices is a variant of the service-oriented architecture (SOA) architectural style that structures an application as a collection of loosely coupled services.

But there is no official definition of Microservices by industry standards. It’s recent phenomenon in software industry to architect the new softwares which should be light weight, easier to deploy and scale, easier to refactor individually and could work independently.

To understand in details, you can definitely read Martin Fowler’s Microservices or Chris Richardson’s Microservices.

We will not be covering this post in detail as compared to link I have posted here. Microservices are small services that can run independently, but can also easily communicate with other services.

Microservice Architecture vs Monolithic Architecture

In traditional monolithic architecture style, there is a single application with single code base. An application contains number of modules which are interrelated and can have external dependencies. It’s a multi-tier enterprise application and has been used to build software for long.

Microservice architecture style was born out of need to build an application that could easily be supported for mobile applications. Older style was not easy to support for mobile and new generation way to handling of data. Any large enterprise application can be easily built using microservices architecture style.

How to identify Microservice Architecture Pattern?

A simple ground rule of microservice architecture pattern is to build a standalone service that can be run without depending on any other service. That means for a large application can have more than one services talking to each other, communicating with their own databases, but still performing the business logic. Databases are used to ensure loose coupling of services.

A large enterprise e-commerce application can consist of following services

  1. Backend service REST API to manage data
    1. Account Service
    2. Shipment Service
    3. Inventory Service
  2. Runtime service to handle runtime and backend data to process business logic
  3. Logging service
  4. Error Handling service
  5. Session service

UI for the e-commerce application can be built independently to use backend services to show/edit data.

By standards, there are few rules to identify microservices patterns

  1. Decomposition by business capability
  2. Database per service pattern
  3. API gateway pattern
  4. Client-side discovery and Server-side discovery

Pros and Cons of Microservices

Pros

  1. Deployability – They can independently be deployed.
  2. Reliability – A fault in the service can only bring down that service, depending on handling in application, rest of the application can still be accessed.
  3. Scalability – Each microservice can be scaled depending on requirements using clusters and grids.
  4. Availability – Dispatching the patch or newer version of service requires less downtime compared to regular monolithic application.
  5. Management – Easier to manage
  6. Design and Development – Each service can be developed independently and helps developer to manage the service easily without worrying about other services.

Cons

  1. Performance – All services involved in application have to communicate with each other over network and that could hamper the performance.
  2. Testability – Automated tests are harder to manage and run.
  3. Memory usage – Possible duplicate data across services and lot of duplication in cache.

References

You can read more about Microservices at following links:

  1. Microservices by Chris Richardson
  2. Microservices by Martin Fowler
  3. Stackoverflow post about microservices

How To: AngularJS User Interface to CRUD Spring Boot REST API

In this post, we will add an user interface using AngularJS to the REST api we created here.

Controller for home page

First we will create a controller in Spring Boot rest api to call our home page. All the requests that will come to web server, will go through this controller and controller will return a home page for the request based on path.

MainController.java will look like below:


package com.betterjavacode.benefits.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class MainController {

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

@RequestMapping(value = "/home", method = RequestMethod.GET)
public String homepage() {
LOGGER.info(" Enter >> homepage() ");
return "index";
}
}

Any request coming to https://localhost:8443/home will return a page from index.html.

Create a Home Page

Now, we will create an index.html page. We will also be using angular JS framework as part of this home page so that we can build a single page application. If you are not aware of Angular JS or new to this framework, you can read about it AngularJS. One thing to remember while creating this page is a directory structure. Lot of issues that arise to create html pages are because of directory structure. Directory structure will look like below:

directorystructure

The home page index.html page is under main/resources/templates/ directory and it looks like below


<html ng-app="benefitApp">

<head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /><title>Benefit Application</title>

<script>document.write('<base href="' + document.location + '" />');</script>

	<link rel="stylesheet" href="/css/bootstrap.css" />

<script src="https://code.angularjs.org/1.6.1/angular.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-route.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-resource.js"></script>

<script type="text/javascript" src="./js/app.js"></script>

</head>

<body ng-controller="MainCtrl">

Hello {{name}}!
<div>
<ul class="menu">
	<li><a href="listUser">user-list</a></li>
	<li><a href="listCompany">company-list</a></li>
</ul>
<div ng-view="ng-view"></div>
</div>
</body></html>

Home page shows that this is an angular app with name “benefitApp”. This also declares a controller “MainCtrl” with an angular view. Important to see we are importing angular.js, angular-route.js and angular-resource.js modules. Click on user-list or company-list, will show list of users and list of companies respectively.

Create a controller

Now to handle the controller (MainCtrl), we added in index.html, we will add app.js which will declare the controller. This javascript file also contains config data to handle all the routing of pages. That’s why we will be importing “ngRoute” and “ngResource” angular modules.


var app = angular.module('benefitApp', ['ngRoute','ngResource']);

var app = angular.module('benefitApp', ['ngRoute','ngResource']);
app.controller('MainCtrl', function($scope, $routeParams) {

$scope.name = 'World';

$scope.$routeParams = $routeParams;

})

Through out the interaction on web pages, we will be using different controllers for editing user or company information and creating user or company. The same file app.js will also handle routing of these pages which is shown below


app.config(function($routeProvider,$locationProvider) {

$locationProvider.html5Mode(true);

$routeProvider.when('/listUser',

{templateUrl: 'views/listUser.html', controller: 'userController'});

$routeProvider.when('/listCompany',

{templateUrl: 'views/listCompany.html', controller: 'companyController'});

$routeProvider .when('/editUser/:userId',

{ templateUrl : 'views/editUser.html' }) ;

$routeProvider .when('/editCompany/:companyId',

{ templateUrl : 'views/editCompany.html' }) ;

$routeProvider.when('/createUser',

{templateUrl:'views/createUser.html'});

$routeProvider.when('/createCompany',

{templateUrl:'views/createCompany.html'});

});

Rest of the code showing all controllers’ logic has been skipped for post purposes. It is available on github repository.

UserController or CompanyController are calling rest apis which we have built using Spring boot.

Demo

Now build the code and run our embedded tomcat webserver. Fire up the url https://localhost:8443/home – it will look like below:

homepage

Click on user-list and it will show list of users inside the same page as below:

listofuserspage

Click on edit button and we will see a form to update user information:

edituserpage

Download –

In this post, we showed how to create a simple CRUD user interface using angular JS for Spring Boot REST api. The code for this is available at repository

Handle uncaught exceptions in a Spring-Boot REST API – Part VI

Many times, we have seen exception thrown on your web page and all the stack trace of the exception. A non-technical user will not be able to understand most of the time. Also stack trace is not the best option when we can show the same exception error in nice json format with the root cause. In this post, we will show how to handle unhandled exceptions from our previous Spring REST API we built here. This particular feature will show how to handle most HTTP 500 errors which happen because of server side issues. Any errors with request or client side, those are HTTP 400 errors and they have been handled in previous post Error Handling in Spring Boot Rest API.

Problem

What happens when there is a database connection issue OR columns in your REST API are different from specified in database tables? Your API will throw a 500 error and if you don’t have any mechanism, this will display error in an html format which will not give much information to user or developer to resolve the issue.

Solution

JAX-RS provides a way to handle uncaught exceptions. This can be done by my implementing an interface for ExtendedExceptionMapper. Basically this is a contract for a provider that takes java exceptions and maps them to a response object which can be transformed into a JSON. This can be implemented as below:


package com.betterjavacode.benefits.utilities;

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

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.glassfish.jersey.spi.ExtendedExceptionMapper;

@Provider
public class UncaughtExceptionMapper implements ExtendedExceptionMapper<Throwable> {

private static final Logger LOGGER = LogManager.getLogger(UncaughtExceptionMapper.class);

@Override
public Response toResponse(Throwable exception) {
LOGGER.info("Enter >> toResponse ");
LOGGER.debug("Exception Caught: " + exception.getMessage());
LOGGER.info("Exit << toResponse");
return Response.status(Status.BAD_REQUEST)
.entity(exception.getMessage())
.build();
}

@Override
public boolean isMappable(Throwable arg0) {
return !(arg0 instanceof WebApplicationException);
}

}

Basically this implementation shows a class UncaughtExceptionMapper implementing an interface ExtendedExceptionMapper which provides a way to map all exceptions which are not of type WebApplicationException (Most HTTP 400 errors are WebApplicationExceptions). toResponse method will help to log all the exceptions and convert exceptions into a Response object.

Conclusion

In this post, we showed how to map all uncaught exceptions into json format response. The code for this post is available at github repository.

How to consume OAuth secured SOAP web service

I faced the issue where I had to consume a SOAP service which was secured by OAuth1.0a. And Spring doesn’t provide any direct solution for consumer OAuth secured SOAP service.

In Producing and Consuming SOAP web service and Consuming SOAP web service over HTTPS, we saw how to consume a SOAP web service. In this post, we will go little beyond this and implement a solution to consume OAuth secured SOAP web service. Securing a web service is general trend and you must secure a web service if you are letting others consume it. This is a secure way to transfer data between producer and consumer without compromising customer data.

Pre-requisites

  1. Spring web services
  2. OAuth library and knowledge

How to implement?

Below is a code that shows how to send a SOAP request call to a web service if it is not OAuth secured.

public class UserClient extends WebServiceGatewaySupport
{
    public GetUserResponse getUserById (int userid)
    {
       GetUserRequest userrequest = new GetUserRequest();
       userrequest.setId(userid);
       GetUserResponse response = (GetUserResponse)getWebServiceTemplate().marshalSendAndReceive(userrequest, new SoapActionCallback("https://localhost:8443/benefits/endpoints/getUserResponse"));
       return response;
     }
}

Basically, we are using a WebServiceTemplate to marshal a request and send it to SOAP endpoint. SoapActionCallback  is callback which allows to change the marshalled message before it can be sent to endpoint and a response can be retrieved.

As part of this solution, we will implement a class SignedMessageSender that will sign the request with OAuth consumer key and secret.


public class SignedMessageSender extends HttpComponentsMessageSender
{
    private final CommonsHttpOAuthConsumer consumer;

    public SignedMessageSender(CommonsHttpOAuthConsumer consumer)
    {
      this.consumer = consumer;
    }

    public WebServiceConnection createConnection(URI uri)
    {
       HttpComponentsConnection conn = null;
       try
       {
          conn = (HttpComponentsConnection)super.createConnection(uri);
consumer.sign(connection.getHttpPost());
       }
       catch (IOException e | OAuthException e)
       {
          e.printStackTrace();
       }
       return conn;
}

Now we build our bean for client to use this message sender, we will assign consumer key and consumer secret. This also uses JAXB marshaller. The code for this will look like below


@Bean

public UserClient getUserClient(Jaxb2Marshaller marshaller)

{

UserClient us = new UserClient();

us.setDefaultUri("https://localhost:8443/benefits/endpoints/users.wsdl");

us.setMarshaller(marshaller);

us.setUnmarshaller(marshaller);

String consumerkey = "";

String secretkey = "";

CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(consumerkey,secretkey);

SignedMessageSender signedMessageSender = new SignedMessageSender(consumer);

signedMessageSender.createConnection(new URL("https://localhost:8443/benefits/endpoints/users.wsdl").toURI());

us.setMessageSender(signedMessageSender);

return us;

}

 

This shows how we can implement a solution to consume a SOAP web service secured by OAuth 1.0a. I am sure we can add similar solution if the service is secured by OAuth 2.0, but that will be another post.

Conclusion

In this post, we showed how to add OAuth signed SOAP message to SOAP service which would return a response to consumer.

References

  1. Add Header to SOAP message
  2. SOAP WS-addressing
  3. https://www.avisi.nl/blog/2012/11/22/consuming-oauth-secured-soap-webservices-using-spring-ws-axiom-signpost/