Springboot Application 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.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {
 
    @RequestMapping("/")
    public String sayHello(){
        return "Hello Spring Boot!!";
    }
 
    @RequestMapping("/sayMe")
    public String sayMe(String name){
        return "hello " + name;
    }
 
    @RequestMapping("/add")
    public String add(Integer number1, Integer number2 ){
        return "result: " + String.valueOf(number1 + number2);
    }
 
    @RequestMapping(value="/json", produces=MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> json(String arg1, String arg2){
         Map<String, Object> map = new HashMap<>();
  
         map.put("result: ", "json value");
         map.put("arg1: ", arg1);
         map.put("arg2: ", arg2);
         return map;
    } 

}




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. Root path , http://localhost:8080/




b. Call /sayMe , http://localhost:8080/sayMe?name=jack.


c. call /add



d. call /json