Spring Boot quick start (latest)

In this chapter, by completing the construction of the Spring Boot basic project and implementing a simple Http request processing, let everyone have a preliminary understanding of Spring Boot and experience its simple structure and rapid development characteristics. It is expected that the reading and practice process will take about 5 minutes.

Use Maven to build the project

  1. SPRING INITIALIZRGenerate basic projects through tools
    1. access:http://start.spring.io/
    2. Fill in the group (such as com.v5ent), select the build tool Maven Project, Spring Boot version and some basic project information, please refer to the following figure

       

    3. Click to Generate Projectdownload the project compressed package
  2. Unzip the project package and Mavenimport it into the IDE as a project

Project structure analysis

Through the above steps, the creation of the basic project structure is completed. The basic structure of Spring Boot has three files:

  • src/main/javaThe following program entry:DemoApplication
  • src/main/resourcesConfiguration file under:application.properties
  • src/test/The following test entry:DemoApplicationTests

The generated DemoApplicationand DemoApplicationTestsclasses can be run directly to start the currently created project. Since the project does not currently cooperate with any data access or web modules, the program will end after loading Spring.

Introduce web module

pom文件已经Two modules are currently introduced:

  • spring-boot-starter: Core modules, including automatic configuration support, logging and YAML
  • spring-boot-starter-test: Test modules, including JUnit, Hamcrest, Mockito

We need to add spring-boot-starter-webmodules:

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

Write HelloWorld service

  • Created packagenamedcom.v5ent.demo.web
  • Create a HelloControllerclass, the content is as follows
复制代码
@RestController
 public  class HelloController {
    @RequestMapping("/hello")
    public String index() {
        return "Hello World";
    }
}
复制代码
  • Start the main program, open the browser to visit http://localhost:8080/hello, you can see the page outputHello World

Write unit test cases

打开src/test/下的测试入口DemoApplicationTests类。下面编写一个简单的单元测试来模拟http请求,具体如下:

复制代码
@RunWith(SpringRunner.class) 
@SpringBootTest
public class DemoApplicationTests {
    private MockMvc mvc;
    @Before
    public void setUp() throws Exception {
        mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
    }
    @Test
    public void getHello() throws Exception {
        mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(content().string(equalTo("Hello World")));
    }
}
复制代码

使用MockServletContext来构建一个空的WebApplicationContext,这样我们创建的HelloController就可以在@Before函数中创建并传递到MockMvcBuilders.standaloneSetup()函数中。

注意引入下面内容:

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;

Ok,跑起来,完全和预想的一样。至此已完成本章目标,通过Maven构建了一个空白Spring Boot项目,再通过引入web模块实现了一个简单的请求处理。

如此快速、开箱即用,爽到飞起。

Guess you like

Origin blog.csdn.net/litianquan/article/details/80870063