Wednesday, 11 December 2013

Spring Boot JSON Restful web service example

Spring  Boot API provides easy way of creating stand alone applications with minimum effort and very less configurations. It contains embedded tomcat or jetty to run web applications. Using this API no need to worry about creating war and other configuration. See more about Spring boot
Here is one simple web service which is returning state name and capital as JSON format upon passing state code.

1. Create a Maven Project and add below dependencies 



2. Create a Resource Controller class (

This is a controller class which is handling web service requests, here we are adding @EnableAutoConfiguration to take up auto configuration and in the main method using to create instance of the controller class.
package com.vinod.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

public class StateController {
    @RequestMapping(value = "/{code}", method = RequestMethod.GET)
    public @ResponseBody State getState(@PathVariable String code) {
        if (code.equals("KL")) {
            return new State("Kerala", "Trivandrum");
        } else {
            return new State("Default State", "Default Capital");
    public static void main(String[] args) throws Exception {, args);

class State {
    private String name;
    private String capital;

    public State(String name, String capital) {
        super(); = name; = capital;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getCapital() {
        return capital;

    public void setCapital(String capital) { = capital;


3. Run it

Run the above main program, your web service started via spring boot. Let us test this with below url http://localhost:8080/state/KL