Sunday, April 16, 2017
This section provides a quick introduction on how to develop a simple micro service which exposes a REST API using Spring Boot.

The intent is to do a simple “/sayhello” API which returns “Hello” as the output.

  • Open the Spring Tool Suite (STS) and click on Spring Starter Project.
Create a Spring Starter Project

  • Fill in the application details. For this example we would use Java 8 and Maven as the build system.
Spring Starter Project application details


  • Since we are exposing APIs from this application enable Web Services under Web. Refer the snapshot below.
Spring Starter Project Enable Web Services
  • Finish the application wizard and you would see the Spring project created and ready for use in Spring Tool Suite IDE. Under Maven dependencies all the dependent jars are already loaded. Also notice that pom.xml is created.
Spring Starter Project Application

  • Now let us understand the top level class.
package com.sourcetricks;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyFirstAppApplication {

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

@SpringBootApplication is a convenience annotation. The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes if you are familiar with Spring annotations.

Otherwise we have one line of code in main program which launches the application.

  • Now let us introduce a new class HelloController which exposes the /sayhello API. This is achieved using @RestController and @RequestMapping annotations as shown below.

package com.sourcetricks;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping("/sayhello")
    public String hello() {
      return "Hello";
    }
}


  • Now we can run the project as a Java application in the STS IDE and we should start to see the embedded tomcat coming up on port 8080.

2017-04-16 07:27:43.657  INFO 17584 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-04-16 07:27:43.790  INFO 17584 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-04-16 07:27:43.799  INFO 17584 --- [           main] com.sourcetricks.MyFirstAppApplication   : Started MyFirstAppApplication in 7.816 seconds (JVM running for 10.038)


  • Open a browser and invoke the /sayhello API we introduced.
Spring Boot Application - Launch Web Service
  • Next step is to package this application into a standalone application using Maven. If you notice the generated pom.xml file you will see the necessary build plugins already added since we selected Maven as the build system during project creation.
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>com.sourcetricks</groupId>
 <artifactId>MyFirstApp</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>MyFirstApp</name>
 <description>This is my first Spring Boot Web Service</description>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.2.RELEASE</version>
  <relativePath/>
  <!-- lookup parent from repository -->
 </parent>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <java.version>1.8</java.version>
 </properties>

 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web-services</artifactId>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>
</project>


  • Go to the workspace folder where the application is stored in your workspace and run “mvn package” command. This builds and packages the application. You should see “MyFirstApp-0.0.1-SNAPSHOT.jar” bundled with all dependencies.
Maven standalone application package

  • You can test if the application starts up by running the command “java –jar MyFirstApp-0.0.1-SNAPSHOT.jar”. The same browser test can be done on the exposed API.

Now we are ready with a simple micro service based on Spring Boot. 

23 comments :

  1. Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us
    Analytics Training In Chennai

    ReplyDelete
  2. I want more and more articles and blogs please post soon such informative information.
    this guy makes good reviews

    ReplyDelete
  3. Today’s GPS watches have all the sport-specific features to track every stride on the roads, but they’re also jammed with other sensors and technology—pairing with your smartphone—to keep you connected the rest of the day, too. Here are some of the top-of-the-line watches runners will want to check out Top GPS Watches

    ReplyDelete
  4. It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of the ideas can be nice content.The people to give them a good shake to get your point and across the command.

    AWS Training in HRBR Layout
    AWS Training in Kalyan Nagar
    Best AWS Training Institute in Kalyan Nagar Bangalore

    ReplyDelete
  5. Some us know all relating to the compelling medium you present powerful steps on this.Advanced Selenium Training in Chennai

    ReplyDelete
  6. Thanks a lot very much for the high your blog post quality and results-oriented help. I won’t think twice to endorse to anybody who wants and needs support about this area. Best Java Training Institute Chennai

    ReplyDelete
  7. I simply wanted to thank you so much again. I am not sure the things that I might have gone through without the type of hints revealed by you regarding that situation.
    amazon-web-services-training-in-bangalore

    Best Hadoop Training Institute In chennai

    ReplyDelete
  8. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
    amazon-web-services-training-in-bangalore

    Best Hadoop Training Institute In chennai

    ReplyDelete
  9. I have read your blog. Your information is really useful for beginner. informations provided here are unique and easy to understandThanks for this useful infromation.
    QTP Training in Chennai
    Java Training in Chennai
    Python Training in Chennai

    ReplyDelete
  10. I hope you continue to have such quality articles to share with everyone! I believe a lot of people will be surprised to read this article hotmail sign in

    ReplyDelete
  11. Great article, really very helpful content you made. Thank you, keep sharing.

    Internet Of Things Services | Austere Technologies

    ReplyDelete
  12. Great post just what I was looking for.
    article

    ReplyDelete
  13. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
    amazon-web-services-training-in-bangalore

    ReplyDelete
  14. wow...nice blog, very helpful information. Thanks for sharing.

    Best Software Company | Austere Technologies

    ReplyDelete
  15. VERY INFORMATIVE BLOG. KEEP SHARING SUCH A GOOD ARTICLES.

    Mobility Services | Austere Technologies

    ReplyDelete
  16. Thanks a lot very much for the high your blog post quality and results-oriented help. I won’t think twice to endorse to anybody who wants and needs support about this area.
    rpa training in chennai

    ReplyDelete
  17. Appreciation for really being thoughtful and also for deciding on certain marvelous guides most people really want to be aware of.
    Best rpa training Institute in chennai

    ReplyDelete
  18. I strongly believe that there will be great opportunities for those who looked into this area...
    Best Online Software Training Institute | DevOps Training

    ReplyDelete
  19. Thanks a lot very much for the high your blog post quality and results-oriented help. I won’t think twice to endorse to anybody who wants and needs support about this area.
    datascience training in chennai

    ReplyDelete

Contact Form

Name

Email *

Message *

Back to Top