SpringBoot

创建日期:2024-06-21
更新日期:2024-12-18

SprintBoot入门:https://spring.io/quickstart

初始化创建:https://start.spring.io/,注意依赖添加Spring Web。

POM文件

<?xml version="1.0" encoding="UTF-8"?>
<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>2.7.18</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.liteng</groupId>
    <artifactId>RemoteDebugger</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.7.18</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.7.18</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.7.18</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.32</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.7.18</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <classifier>spring-boot</classifier>
                            <mainClass>org.liteng.springtest.SpringApplication</mainClass>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

注意:想打出的jar包能运行,需要加上excution里面的配置。

启动类

SpringApplication.java

package org.liteng.springtest;

import org.springframework.boot.autoconfigure.SpringBootApplication;

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

控制器

TestController.java

package org.liteng.springtest.controller;

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

@RestController
@RequestMapping("/test")
public class TestController {
    @GetMapping("/hello")
    public String hello(String name) {
        return "hello," + name + "!";
    }
}

常用注解

@SpringBootApplication:相当于@Configuration、@EnableAutoConfiguration、@ComponentScan

@RestController:控制器

@GetMapping("/hello"):get请求映射

@RequestParam(value

"name", defaultValue

"World"):参数映射

返回静态资源

放在resources/static中的文件可以直接访问。例如resources/static/index.html,可以在浏览器中使用http://localhost:8080/index.html访问。

使用thymeleaf引擎

参考:https://blog.csdn.net/caychen/article/details/79625927

参考:https://www.cnblogs.com/tweixi/p/13902918.html

依赖项:org.springframework.boot:spring-boot-starter-thymeleaf

配置文件:

spring:

thymeleaf:

cache: false #开发过程中建议关闭cache

#encoding: UTF-8 #thymeleaf编码

#suffix: .html # thymeleaf后缀

#content-type: text/html #格式

创建模板resources/templates/thymeleaf.html

Title

成功!

这是显示欢迎信息

创建控制器

@Controller

@RequestMapping("/thymeleaf")

public class ThymeleafController {

@RequestMapping("/hello1")

public String hello1() {

return "thymeleaf";

}

@RequestMapping("/hello2")

public ModelAndView hello2() {

ModelAndView mv = new ModelAndView();

mv.addObject("hello", "世界,你好!");

mv.setViewName("thymeleaf");

return mv;

}

}