Spring Boot Web:通过 Hello World 入门


#Spring Boot#


该代码拷贝自官方教程 https://github.com/spring-guides/gs-rest-service/ , 做了大幅度的简化,只留了主要该关心的内容 。

项目的结构如下:

.
├── build.gradle
└── src
    ├── main
    │   └── java
    │       └── hello
    │           ├── Application.java
    │           ├── Greeting.java
    │           └── GreetingController.java
    └── test
        └── java
            └── hello
                └── GreetingControllerTests.java

代码解读

build.gradle

内容如下:

buildscript {
    repositories {
        maven { url 'http://mirrors.cloud.tencent.com/nexus/repository/maven-public/' }
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.1.3.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management' // 这个可以让 spring boot 相关的依赖不用写版本号


repositories {
    maven { url 'http://mirrors.cloud.tencent.com/nexus/repository/maven-public/' }
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web") // 引入这个依赖就够了

    // 下面的是测试用的依赖
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('com.jayway.jsonpath:json-path')
}

Application 类:程序主入口

为什么是主入口?因为里面有 main 函数。

package hello;

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);
    }
}

Greeting 类:一个简单的 Java bean

注意,不是 spring bean。 可以叫做 Java bean 的类,就是类中定义了一些字段,并且编写了这些字段的 get、set 方法。

package hello;

public class Greeting {

    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}

GreetingController 类:指定 URL 对应的处理函数

package hello;

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;

@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="World") String name) {
        return new Greeting(counter.incrementAndGet(),
                            String.format(template, name));
    }
}

类本身必须要用 @RestController 注解。@RequestMapping 用来指定要处理的 URL。

就这些了?

是的,就这些了。Spring Boot 会自动处理大部分事情,例如扫描各个类,启动web服务,自动将指定url交给特定的处理函数等等。可以把这个看做是 Spring Boot 和使用者的约定,使用者不用关心配置/或者是不用太过关心配置。

执行效果

运行 Application 类。

浏览器访问 http://127.0.0.1:8080/greeting ,可以看到:

{"id":1,"content":"Hello, World!"}

如果是用 postman 访问,HTTP 请求头 Accept 为空,或者application/json,都会有返回。但是若设置为text/xmlapplication/xml,则无返回。

测试代码

这个项目中还有一个类是 GreetingControllerTests ,这个是测试类。其内容如下:

package hello;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class GreetingControllerTests {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void noParamGreetingShouldReturnDefaultMessage() throws Exception {

        this.mockMvc.perform(get("/greeting")).andDo(print()).andExpect(status().isOk())
                .andExpect(jsonPath("$.content").value("Hello, World!"));
    }

    @Test
    public void paramGreetingShouldReturnTailoredMessage() throws Exception {

        this.mockMvc.perform(get("/greeting").param("name", "Spring Community"))
                .andDo(print()).andExpect(status().isOk())
                .andExpect(jsonPath("$.content").value("Hello, Spring Community!"));
    }

}

按照 junit 的方式运行即可。这一套测试,在运行时不会真的启动一个服务,只是去模拟了服务。


( 本文完 )