创建第一个 Spring Boot 项目

Create Spring Boot project
使用到的相关版本:
jdk 1.8
IDE(VSCode or IDEA)
Spring Boot 2.1.1

使用 IDEA

新建 Spring Initializr 项目

packing 要选 jar

勾选 Web MySQL MyBatis 依赖

pic1

pic2

使用 VSCode

Install Extension: Java Development pack.

Press Ctrl+Shift+P to call Spring Initializer. It will automatically guide you to set ArtifactId, GroupId, package name, etc. Here I use Maven and set groupId “com.wsd”, artifactId “springweb” and the default name is “demo”.

A simple Spring Boot Web application

You will see src/java/com/wsd/springweb/DemoApplication.java, Edit it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.wsd.springweb;

import java.util.Arrays;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
System.out.println("Let's inspect the beans provided by Spring Boot");

String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
};
}

}

JavaBean 函数 commandLineRunner 的作用是通过运行一个匿名函数,输入Application中所有的 Bean(包括由当前App生成的和自动添加到Spring Boot中去的)

src/java/com/wsd/springweb/DemoController.java

1
2
3
4
5
6
7
8
9
10
11
12
package com.wsd.springweb;

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

@RestController
public class DemoController {
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}

src/java/com/wsd/springweb/application.properties

1
server.port=8501

Maven compile and package

Run the .jar

result

And you can retrive the string by visiting the webpage

web

Add Unit Test

src/java/com/wsd/springweb/DemoControllerTest.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.wsd.springweb;

import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
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.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class DemoControllerTest {
@Autowired
private MockMvc mvc;

@Test
public void getHello() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("Greetings from Spring Boot!")));
}
}

a MockMvc object will mimic a mvc controller accessing the specified URL, then check whether the result is right.

注解 @RunWith(SpringRunner.class) 开启了 Run Test 功能。
注解 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 搭建出了网络环境

随机端口测试

src/java/com/wsd/springweb/DemoControllerIT.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.wsd.springweb;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.*;
import java.net.URL;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class DemoControllerIT {

@LocalServerPort
private int port;

private URL base;

@Autowired
private TestRestTemplate template;

@Before
public void setUp() throws Exception {
this.base = new URL("http://localhost:" + port + "/");
}

@Test
public void getHello() throws Exception {
ResponseEntity<String> response = template.getForEntity(base.toString(), String.class);
assertThat(response.getBody(), equalTo("Greetings from Spring Boot!"));
}
}

Add production-grade services

Before adding

before-actuator

After adding

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

into pom.xml file

after-actuator

多了一行

2019-11-08 21:10:31.858  INFO 104665 --- [main] o.s.b.a.e.web.EndpointLinksResolver :
Exposing 2 endpoint(s) beneath base path '/actuator'

接下来便可以访问 http://localhost:8501/actuator

actuator-web

数字图像处理项目调研 外刊精读学习计划

评论

You forgot to set the shortname for Disqus. Please set it in _config.yml.
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×