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. 

0 comments :

Post a Comment

Contact Form

Name

Email *

Message *

Back to Top