Springboot Rest Service Example

                                                            Index

1. Springboot Java classes
2. Pom.xml dependencies
3. Browser Examples


1. Springboot Java classes
 

package com.example.springboot.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class RestApiController {

    private Map<Integer, String> users = new HashMap<Integer, String>();
 
    @RequestMapping(value="/insert")
    public ResponseEntity<String> insertUser(Integer id, String name){
  
        users.put(id, name);
        return new ResponseEntity<>("Inserted", HttpStatus.OK);
    }
 
    @RequestMapping(value="/list")
    public ResponseEntity<Map<Integer, String>> listUsers(){
        return new ResponseEntity<>(users, HttpStatus.OK);
    }
 
    @RequestMapping(value="/update")
    public ResponseEntity<String> update(Integer id, String name){
  
       users.put(id, name);
        return new ResponseEntity<>("Updated", HttpStatus.OK);
    }
}




package com.example.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages ="com.example.springboot.controller")
public class App{
 
    public static void main( String[] args ){
         SpringApplication.run(App.class, args);
    }
}

2. Pom.xml dependencies
 
<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>

            ....

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
  <!-- <relativePath />lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </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>
            ...
</project>
 
 
3. Browser Examples
 
a. call /insert , http://localhost:8080/api/insert?id=5&name=jack




b. call /list , http://localhost:8080/api/list





c. call /update, http://localhost:8080/api/update?id=5&name=steve