环境:

  • jdk1.8
  • maven 3.6.1
  • springboot 2.2.0
  • IDEA

首先介绍一种快速生成springboot的方式,仅供了解

官方提供了一个快速生成的网站,IDEA集成了这个网站。

打开这个网站

springboot 设置cookie的domain_spring


选择projects下的spring boot

springboot 设置cookie的domain_springboot_02


点击最下放方的quick start

springboot 设置cookie的domain_xml_03


这里配置一些项目的基本信息

springboot 设置cookie的domain_spring_04


添加依赖,这里选择spring web

springboot 设置cookie的domain_maven_05


然后点击开始下载

springboot 设置cookie的domain_springboot_06


下载完成后解压

springboot 设置cookie的domain_spring_07


然后用idea导入项目

springboot 设置cookie的domain_xml_08


选定项目位置

springboot 设置cookie的domain_xml_09


导入maven项目

springboot 设置cookie的domain_springboot_10


之后一路全部选择默认选项,下面路径为项目存放路径


springboot 设置cookie的domain_spring_11


然后一个springboot就生成了

springboot 设置cookie的domain_spring_12


springboot 设置cookie的domain_maven_13


直接运行主程序

springboot 设置cookie的domain_xml_14


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 https://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.2.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.anye</groupId>
	<artifactId>helloworld</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>helloworld</name>
	<description>Demo project for Spring Boot</description>

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

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

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

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

</project>

如上所示,主要有四个部分:

  • 项目元数据:创建时候输入的Project
    Metadata部分,也就是Maven项目的基本元素,包括:groupId、artifactId、version、name、description等
  • parent:继承spring-boot-starter-parent的依赖管理,控制版本与打包等内容
  • dependencies:项目具体依赖,这里包含了spring-boot-starter-web用于实现HTTP接口(该依赖中包含了Spring
    MVC),官网对它的描述是:使用Spring
    MVC构建Web(包括RESTful)应用程序的入门者,使用Tomcat作为默认嵌入式容器。;spring-boot-starter-test用于编写单元测试的依赖包。更多功能模块的使用我们将在后面逐步展开。
  • build:构建配置部分。默认使用了spring-boot-maven-plugin,配合spring-boot-starter-parent就可以把Spring
    Boot应用打包成JAR来直接运行。
编写controller

在springboot主程序的同级目录下新建controller包,并在该包下新建Controller类(注意:所有的controller都必须在springboot主程序的同级目录下,或者同级目录下的子包下。要不然就找不到controller,这里就体现了springboot的约定大于配置)

package com.anye.helloworld.controller;

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

//@RestController=@Controller+@ResponseBody
//表示将该类托管给spring,spring可以通过这个注解找到该controller
@RestController
public class HelloController {
    @RequestMapping("/h1")//请求映射路径
    public String hello(){
        return "hello";
    }
}

启动主程序测试

springboot 设置cookie的domain_xml_15

修改启动时显示的字符

我们可以在resources目录下新建banner.txt文件,并将想要展示的字符串拷贝进去

生成字符串图形

springboot 设置cookie的domain_xml_16


springboot 设置cookie的domain_spring_17