创建maven项目
项目构成
XML
<?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>
<groupId>org.example</groupId><!--组-->
<artifactId>hello-springboot-one</artifactId><!--项目名-->
<version>1.0-SNAPSHOT</version><!--版本-->
<!--父项目-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!--从父级仓库查找依赖版本,MAVEN构建jar包时候查找顺序:relativePath元素中的地址>本地仓库>远程仓库 -->
</parent>
<dependencies>
<!--WEB依赖(WEB应用所有常用的JAR包)-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!--部署SpringBoot插件,(打包时时会写入 hello-springboot-one-1.0-SNAPSHOT/META-INF/MANIFEST.MF信息)-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
启动类
package com.ludb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication //标记为Spring Boot启动类
public class Applaction {
public static void main(String[] args) {
SpringApplication.run(Applaction.class,args);
}
}
Controller
package com.ludb.controllers;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController //相当于@Controller+@ResponseBody两个注解的结合,返回json数据不需要在方法前面加@ResponseBody注解了,
@RequestMapping("/hello") //RequestMapping是一个用来处理请求地址映射的注解
public class HelloController {
@RequestMapping("/say") //RequestMapping是一个用来处理请求地址映射的注解
public String say(){
return "HELLO WORD";
}
}
application.yaml
#设置端口号
server:
port: 9081
#设置访问前缀(设置后为:http://localhost:9081/ludb/hello/say)
servlet:
context-path: /ludb
打包执行
注意
- Applaction.java 需要放在controllers同级目录下
- application.yaml 文件名字不能变,无问题会有特殊图标
父项目(pring-boot-starter-parent)中又引入了 版本仲裁中心(spring-boot-dependencies)。
版本仲裁中心(spring-boot-dependencies) 管理了SpringBoot 所有依赖的版本,可以防止版本冲突,并且引入已有的版本就不需要写版本号了
以spring-boot-starter-开始的成为场景启动器如 spring-boot-starter-web
> /dev/null 2>&1
特点:标准输出和异常输出,输出到黑洞。
java -jar xxx.jar
特点:当前ssh窗口被锁定,使用Ctrl+c打断程序运行,或者关闭窗口,程序退出。
java -jar xxx.jar &
& 表示后台运行
特点:当前ssh窗口不被锁定,当窗口关闭时,程序退出。
nohup java -jar XXX.jar &
特点:nohup意思是不挂断运行命令,当账户退出或者终端关闭时,程序仍然运行。
nohup java -jar XXX.jar > /dev/null 2>&1 &
特点:不挂断运行命令,不输出日志。