Over a million developers have joined DZone.

Microservices: The Spring Boot Way

Spring Boot makes writing a web application as easy as writing a standalone application with almost zero boilerplate code; find out how to start using it for your microservices project here.

· Integration Zone

Build APIs from SQL and NoSQL or Salesforce data sources in seconds. Read the Creating REST APIs white paper, brought to you in partnership with CA Technologies.

Some Food for Thought

When a Java developer thinks of developing a web application/services in Java and what it involves, the following list likely comes to mind:

  • Servlet APIs
  • Web Frameworks (Struts/SpringMVC/REST API frameworks etc)
  • Application Logic
  • Web Container (Tomcat/JBoss)
  • Build Scripts (ANT/MAVEN)
  • Deployment    

Now compare this with what comes to our mind when we think of developing a standalone Java program/application:

  • Application Logic

That's it!

This exactly what Spring Boot tries to achieve. It makes writing a web application as easy as writing a standalone application with almost zero boilerplate code! All you need to worry about is your Application logic and leave rest to Spring Boot

Here is how to do it. 

Prerequisite: JDK 1.7 or above, Maven 3.x, and IDE or your choice. 

Step 1: Download Spring Boot. The easiest way is to do it using Maven. Create a basic JAR project in Maven. From the command prompt execute the following:

mvn archetype:create -DgroupId=com.springboot -DartifactId=spring-boot-example

This will create a standalone JAR project. Open its  pom.xml file. 

Step 2: Add the following dependencies:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
</parent>
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
</dependencies>

Step 3: Now create the following class:

package com.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;


@RestController
@EnableAutoConfiguration
public class SampleController {

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleController.class, args);
    }
}

This is a simple RESTful service which when requested via GET, returns Hello World. And this is all you need as far as writing code is concerned, the surprise continues.  

Step 4: Compile the project:

mvn compile

Step 5: Now it's time to run. There are many ways this can be run, but the easiest way when using Maven is using this plugin. So add the exec-maven-plugin plugin to pom.xml :

    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0.2</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
         </plugin>
         <plugin>
             <groupId>org.codehaus.mojo</groupId>
             <artifactId>exec-maven-plugin</artifactId>
             <configuration>
                 <mainClass>com.springboot.SampleController</mainClass>
             </configuration>
        </plugin>
    </plugins>
</build>

Step 6: After this, running is as easy as:

mvn exec:java

It starts and prints a lot of messages; in the end you will see something like this:

2016-04-09 21:11:19.574  INFO 10384 --- [ntroller.main()] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-04-09 21:11:19.581  INFO 10384 --- [ntroller.main()] com.springboot.SampleController          : Started SampleController in 9.939 seconds (JVM running for 19.084)

This indicates that the service is up and running. 

Step 7: Now that the service is up and running, it's time to test the service. Point your browser to http://localhost:8080, and you should see "Hello world!"

So, your service is ready to be used! No creation of WARs and no deployment in Tomcat. 

Well, all this is done, and as a developer I am happy that it saved me a lot of effort, but as a good developer, I should know how it's happening behind the scene. 

What Spring Boot does for you is:

  • Looks at your classpath and at beans you have configured.
  • Makes reasonable assumptions about what you’re missing, and adds it. For example, when it sees@RestController it knows you are going to need Tomcat and it adds the embedded Tomcat.
  • It does everything around starting the code and deploying the application.
  • It reads all the annotations and creates the mappings of request paths and method to be executed.

Spring Boot offers a fast way to build microservces-based applications. With Spring Boot you can focus more on business features and less on infrastructure (framework, deployment, web server, etc.).

The Integration Zone is brought to you in partnership with CA Technologies.  Use CA Live API Creator to quickly create complete application backends, with secure APIs and robust application logic, in an easy to use interface.

Topics:
microservices ,java

Published at DZone with permission of Santosh Singh. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}