VOOZH about

URL: https://www.javacodegeeks.com/2017/09/building-restful-service-using-spring-boot.html

โ‡ฑ Building a RESTFul Service using Spring Boot - Java Code Geeks


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.

๐Ÿ‘ Image

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.

๐Ÿ‘ Image

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:

๐Ÿ‘ Image

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.
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

๐Ÿ‘ Photo of Chandana Napagoda
Chandana Napagoda
September 8th, 2017Last Updated: September 8th, 2017
2 353 3 minutes read
Subscribe

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

2 Comments
Oldest
Newest Most Voted
8 years ago

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

0
Reply
Raja
8 years ago

Thanks For The Knowledge.

0
Reply
Back to top button
Close
wpDiscuz