Enterprise Java

Building a RESTFul Service using Spring Boot

Everyone is talking about Microservices such as WSO2 Microservice FrameworkSpring Boot, etc. Since I haven’t worked on any Spring related project since a very long time, I thought to implement a simple RESTFul service using Spring Boot.

So I started with Spring documentation. It is straightforward.  You can create the structure of your project using “Spring Initializr“. This is an online tool where you can add all the desired dependencies to your project POM file. Since I am a big fan of Maven, I am generating a maven project.

In the Spring Initializr UI, you can choose the Language, Spring Boot Version, Project Group ID, artifact name, etc. Please refer below screenshot for information I have provided while generating the project.

When clicking on “Generate Project”, it will download zipped maven project into your computer. Unzip it and import into an IDE. The initial project structure is like below.

In my HelloWorld REST service implementation, it accepts user’s name as a path parameter(or URL parameter) and returns a greeting JSON payload(response). So I am expecting to invoke my REST service by calling below URL: APP_NAME/api/hello/chandana.

The @RestController is a way to implement RESTFul service using Spring. So, this new controller class is going to name as HelloWorldController. So my HelloWorldController class looks like below.

package com.chandana.helloworld;

import com.chandana.helloworld.bean.Greeting;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class HelloWorldController {

    @RequestMapping("/")
    public String welcome() {//Welcome page, non-rest
        return "Welcome to RestTemplate Example.";
    }

    @RequestMapping("/hello/{name}")
    public Greeting message(@PathVariable String name) {

        Greeting  msg = new Greeting(name, "Hello " + name);
        return msg;
    }

}

Note: If you notice Spring Boot 1.5.6 not importing classes correctly and displaying an error message as “Cannot resolve symbol RestController” in your IDE, you need to downgrade the spring version that is used in the project. Spring Boot 1.5.6 by default uses Spring 4.3.10.RELEASE dependency and it need to be downgraded to 4.3.9.RELEASE. So please add <spring.version>4.3.9.RELEASE</spring.version> on the properties section of your POM file.

So everything is in place. I can build and run Spring Boot project using below maven command. It will compile the project and run it.

mvn spring-boot:run

While starting the server you can notice registered REST service URL in the console like below

INFO 9556 — [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped “{[/api/hello/{name}]}” onto public com.chandana.helloworld.bean.Greeting com.chandana.helloworld.HelloWorldController.message(java.lang.String)

INFO 9556 — [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped “{[/api/]}” onto public java.lang.String com.chandana.helloworld.HelloWorldController.welcome()2017-0

Finally, Can invoke REST Service by accessing this URL: http://localhost:8080/api/hello/NAME

Final Project Structure:

Greeting POJO class:

package com.chandana.helloworld.bean;

public class Greeting {

    private String player;
    private String message;

    public Greeting(String player, String message) {
        this.player = player;
        this.message = message;
    }

    public String getPlayer() {
        return player;
    }

    public void setPlayer(String player) {
        this.player = player;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

POM XML:

<?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.chandana</groupId>
 <artifactId>helloworld</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>helloworld</name>
 <description>Demo project for Spring Boot</description>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.6.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>
  <spring.version>4.3.9.RELEASE</spring.version>
 </properties>

 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</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>

HelloWorldController class:

package com.chandana.helloworld;

import com.chandana.helloworld.bean.Greeting;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class HelloWorldController {

    @RequestMapping("/")
    public String welcome() {//Welcome page, non-rest
        return "Welcome to RestTemplate Example.";
    }

    @RequestMapping("/hello/{name}")
    public Greeting message(@PathVariable String name) {

        Greeting  msg = new Greeting(name, "Hello " + name);
        return msg;
    }

}

Conclusion

As it seems, it is very straightforward to implement RESTFul services using Spring Boot. So I got an idea to implement backend of my “Yield Price Sri Lanka” android app using Spring Boot. Besides, hoping to implement an Admin UI to manage price and commodity information and also a public web UI to display price details for users who don’t have an Android app. Keep in touch.

Reference: Building a RESTFul Service using Spring Boot from our JCG partner Chandana Napagoda at the Chandana Napagoda blog blog.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Priya
7 years ago

Very useful article. I m searching for this only. Thanks for sharing. keep blogging.

Raja
Raja
7 years ago

Thanks For The Knowledge.

Back to top button