springboot入门案例

134 阅读1分钟

官网教程 docs.spring.io/spring-boot…

创建springboot项目的方式有很多,这里列举如下

  • start.spring.io/ 生成项目脚手架
  • 使用Idea创建
  • 使用Spring Tool Suite创建项目,支持Eclipse,VSCode等IDE
  • 使用NetBeans创建

这里使用maven,手动创建springboot项目

1.使用idea创建maven项目

2.在 pom.xml 中,声明 spring-boot-starter-paren 父工程,添加 spring-boot-starter-web 依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.2</version>
</parent>

<properties>
    <java.version>1.8</java.version>
</properties>

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

3.创建springboot引导程序类

App.java

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

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

@SpringBootApplication 注解组合了 @SpringBootConfiguration@EnableAutoConfiguration@ComponentScan 3个注解。

4.编写控制器代码

HelloController.java

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController
{
  @GetMapping("/hello")
  public String hello(@RequestParam(value = "name",defaultValue = "world")String name)
  {
    return String.format("hello,%s!",name);
  }
}

5.启动springboot应用

mvn spring-boot:run

访问 http://localhost:8080/hello?name=feet

ok!应用启动成功,springboot就是这么简单!

6.单元测试

pom.xml 中添加 spring-boot-starter-test 依赖

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

然后,编写测试代码 HelloControllerTest.java

package com.lyp;

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.jupiter.api.Test;
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.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

@SpringBootTest
@AutoConfigureMockMvc
public class HelloControllerTest
{
  @Autowired
  private MockMvc mockMvc;

  @Test
  public void helloTest()
      throws Exception
  {
    mockMvc.perform(MockMvcRequestBuilders.get("/hello?name=feet").accept(MediaType.APPLICATION_JSON))
        .andExpect(status().isOk())
        .andExpect(content().string(equalTo("hello,feet!")));
  }
}

运行测试用例

mvn clean test

7.构建可执行的jar文件

设置maven打包方式为jar(默认),并添加maven构建插件

<packaging>jar</packaging>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

运行maven命令

mvn clean package

跳过测试

mvn clean package -DskipTests

target 目录下生成可运行的jar文件,使用java命令进行启动

cd target

java -jar helloworld-1.0-SNAPSHOT.jar

8.部署项目

springboot内嵌了tomcat服务器,只需要将jar文件拷贝到主机上进行相关操作即可。