Building a RESTful Web Service - Spring Boot
Hello everyone, Today we will learn how to develop Restful service using Spring Boot
Following technologies stack being used:
- Spring Boot 2.1.1.RELEASE
- Spring 5.1.3.RELEASE
- Maven 3
- JDK 1.8
- Eclipse Oxygen
1. Project Structure
2. Maven/Dependency Management [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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<!-- lookup parent from repository -->
</parent>
<groupId>com.knowledgefactory</groupId>
<artifactId>springboot-rest</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-rest</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- spring mvc, rest -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3. Rest Controller:
package com.knowledgefactory.knowledgefactorydemo;
import java.util.ArrayList;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public ResponseEntity<List<UserVo>> listAllUsers() {
List<UserVo> list = new ArrayList<>();
UserVo obj = new UserVo();
obj.setName("knowledgefactory");
obj.setEmail("knowledgefactory4upeoples@gmail.com");
list.add(obj);
return new ResponseEntity<List<UserVo>>(list, HttpStatus.OK);
}
}
4. UserVO:
package com.knowledgefactory.knowledgefactorydemo;
public class UserVo {
private String name;
private String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
5. Spring Boot:
package com.knowledgefactory.knowledgefactorydemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class KnowledgefactorydemoApplication {
public static void main(String[] args) {
SpringApplication.run(KnowledgefactorydemoApplication.class, args);
}
}
6. Run:
$ mvn spring-boot:run
7. Test:
Open POSTMAN tool, select request type, specify the URI