DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Frameworks
  4. Microservices: The Spring Boot Way

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.

Santosh Singh user avatar by
Santosh Singh
·
May. 30, 16 · Tutorial
Like (13)
Save
Tweet
Share
14.45K Views

Join the DZone community and get the full member experience.

Join For Free

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.).

Spring Framework Spring Boot

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Best CI/CD Tools for DevOps: A Review of the Top 10
  • Introduction to Spring Cloud Kubernetes
  • Getting a Private SSL Certificate Free of Cost
  • Specification by Example Is Not a Test Framework

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: