RESTful web services using spring boot

Datetime:2017-04-19 05:16:34         Topic: Web Service  REST          Share        Original >>
Here to See The Original Article!!!

We will see how to create RESTful web service using spring boot in this article. I had to include this in the middle of angular articles because we are looking for a bigger picture. We are going to create an end to end enterprise app using client side as well as server side technologies.

RESTful web services using spring boot:

Without going into too much details, let us start building it as quickly as possible. Below tools are being used.

Create a gradle project in Eclipse:

Project Structure

Navigate to File > New > Other > Gradle > Gradle Project

and name it SpringBoot.

You will have a project created with its structure as depicted in this screenshot.

  • build.gradle is our build file.
  • src/main.java : Here we will store java files

buid.gradle:

buildscript {
 ext {
 springBootVersion = '1.5.2.RELEASE'
 }
 repositories {
 mavenCentral()
 }
 dependencies {
 classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
 }
}
 
applyplugin: 'java'
applyplugin: 'eclipse'
applyplugin: 'org.springframework.boot'
 
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
 
repositories {
 mavenCentral()
}
 
 
dependencies {
 compile("org.springframework.boot:spring-boot-starter-web")
 compile('org.springframework.boot:spring-boot-starter-jersey')
 testCompile('org.springframework.boot:spring-boot-starter-test')
}

HelloWorld.java:

package com.thejavageek;
 
public class HelloWorld {
 
 private String message;
 
 public HelloWorld(String message){
 this.message = message;
 } 
 
 public String getMessage() {
 return message;
 }
 
 public void setMessage(String message) {
 this.message = message;
 } 
 
}

This is our model class which simply holds the message.

HelloWorldController:

package com.thejavageek;
 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloWorldController {
 
 @RequestMapping("/sayHello")
 public HelloWorldsayHello() {
 return new HelloWorld("Hello World");
 }
 
}
  • @RestController  specifies that this class will be treated as a spring controller.
  • @RequestMapping(“/sayHello”) :  This means method is mapped to path http://localhost:8080/sayHello .

We are done creating RESTful web services using spring boot. Let us create an actual class that starts the application and boots the services.

Application.java:

package com.thejavageek;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class Application {
 
 public static void main(String args[]){
 SpringApplication.run(Application.class, args);
 }
 
}

All magic happens here, run this class as normal java application and spring will do all the process and start its embedded tomcat server. Once tomcat is started, simply navigate to your browser and hit http://localhost:8080/sayHello

as soon as hit the url, it should be able to display hello world message as below.

{"message":"Hello World"}

I hope this small article helped create RESTful web services using spring boot.








New