Thursday, August 20, 2015

RESTful API using Spring Boot

In this post, we are going to see how to implement RESTful Web Services using Spring Boot.
Project Structure:
Tools & Technologies:

  • Java 8
  • Maven 3.0.3
  • Spring Boot 1.2.5.RELEASE

Maven Configuration:
pom.xml
<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.ranga</groupId>
<artifactId>SpringBootRestAPIExample</artifactId>
<version>1.0.0-SNAPSHOT</version>

<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
</parent>

<dependencies>
<!-- Get the dependencies of a web application -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

</project>
RestController class
GreetingContoller.java
package com.ranga.greeting.controller;

import java.util.concurrent.atomic.AtomicLong;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ranga.greeting.Greeting;
/**
* Used for handling the greeting RESTFul request.
* @author Ranga Reddy
* @version 1.0
* @since Aug 20, 2015
*/

@RestController
public class GreetingController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();

@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="Ranga") String name) {
return new Greeting(counter.incrementAndGet(),
String.format(template, name));
}
}

Greeting Response class:
Greeting.java
package com.ranga.greeting;

/**
* Used to display the Response.
*
* @author Ranga Reddy
* @version 1.0
* @since Aug 20, 2015
*/

public class Greeting {

private long id;
private String name;

/** Default Greeting Constructor */
public Greeting() {
super();
}

/** Parameterized Greeting Constructor */
public Greeting(long id, String name) {
this.id = id;
this.name = name;
}

/**
* @return the id
*/

public long getId() {
return id;
}

/**
* @param id the id to set
*/

public void setId(long id) {
this.id = id;
}

/**
* @param name the name to set
*/

public void setName(String name) {
this.name = name;
}

/**
* @return the name
*/

public String getName() {
return name;
}

@Override
public String toString() {
return "Greeting [id=" + id + ", name=" + name + "]";
}
}
Client Application
Application.java
package com.ranga.main;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

/**
* Main application to run boot application.
*
* @author Ranga Reddy
* @version 1.0
*/

@SpringBootApplication
@ComponentScan(basePackages="com.ranga")
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
Run the Client Application and open the browser enter the following url:
http://localhost:8080/greeting

http://localhost:8080/greeting?name=Ranga Reddy


0 comments: