首先放个参考链接,这个教程有图文讲解的比较详细。 然后自己开始写。 搭建方法很简单,普通的maven项目就好了,不需要选择模板,也就是那个maven-webapp. springboot的简单就是导入依赖,创建目录就好了。 话不多说,maven的配置文件,把下面的粘贴复制在里面中间就好了,原来的保留,毕竟每个项目有项目的名字,额外我加进去的的包括fastjson,打包成jar包的插件,注意想要打包成jar包,需要把自动生成的里面的war改成jar,这个是生成的时候就生成的。
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<!-- 基础配置json -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!--JDK版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
然后创建controller和启动class,注意的是controller必须和启动class放在同一个包下,才能扫到,
主要点:controller里面开始两个注解@Controller``@EnableAutoConfiguration
hello方法上两个注解
@RequestMapping("/hello")
@ResponseBody
或者直接粘贴方法
package com.hello.controller;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@EnableAutoConfiguration
@ResponseBody
public class SystemController {
@RequestMapping("/")
public String System(){
return "welcome!";
}
@RequestMapping("/hello")
public String hello(){
return "hello world!";
}
}
然后启动class,一个注解
@SpringBootApplication
一个main方法,
package com.hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SystemStart {
public static void main(String[] args) {
SpringApplication.run(SystemStart.class, args);
}
}
所以综上所述,最小springboot只需要一个start启动class就可以了,最基础的添加controller,能够返回请求,springboot确实强大。
当然能用不行,还得好用。
在resources下面:
1、创建application.yml文件,指定默认端口
server:
port: 80
2、创建static文件夹,这里面放的文件都是公开的,可下载的,比如a.png,直接访问端口/a.png
3、创建templates文件夹,这里面放的是模板文件,这可以提供快速的构建web
里面可以创建页面,比如index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thumeleaf.org">
<head>
<meta charset="UTF-8">
<title>测试页面</title>
</head>
<body>
<h1 th:text="${newWorld}">hello world</h1>
</body>
</html>
使用方法很简单,controller返回值改一下就好了,改为返回index