搭建SpringBoot项目 —— 两种搭建方式

(一)通过网址自动搭建

  1. 访问地址:https://start.spring.io/
  2. 选择构建工具:Project:选择 Maven Project ;Language:java;Spring Boot:选择SpringBoot版本信息;Project Metadata:基本数据信息,java版本以及项目结构,名称等;Dependencies:添加依赖;
  3. 点击 :Generate the project - Ctrl + ⏎
  4. 解压导入即可。

(二)使用工具(IDEA)搭建

新建项目(new Porject) —> Spring Initializr —>: java版本信息 —> next

springboot 搭建sql server springboot如何搭建_spring


项目信息自行修改即可 —>next

springboot 搭建sql server springboot如何搭建_spring_02


SpringBoot版本选择和maven选择 —> next:

springboot 搭建sql server springboot如何搭建_spring_03


点击Finish完成项目创建

springboot 搭建sql server springboot如何搭建_java_04


进入到主界面后,maven会下载一些依赖包,可能会需要一段时间,整体目录如下:

springboot 搭建sql server springboot如何搭建_spring_05


SpringBoot项目至此创建完成。

测试SpringBoot
启动SpringBoot项目:SpringBoot内嵌Tomcat 默认端口8080 ,访问 http://localhost:8080/ 得到的反馈 图(2)

springboot 搭建sql server springboot如何搭建_html_06


springboot 搭建sql server springboot如何搭建_html_07

(图2)

报错原因:无内容

解决:启动类添加一个test方法,测试访问 http://localhost:8080/test 如下:

@RestController
@SpringBootApplication
public class DemoApplication{

	@RequestMapping("test")
	public String test(){
		return "Hello World";
	}
	
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

访问结果:

springboot 搭建sql server springboot如何搭建_spring_08


到这里SpringBoot基本就搭建完成了。

基本注解解析

解释一下 @SpringBootApplication 作用

@SpringBootApplication 注解实际上是 SpringBoot 提供的一个复合注解

上源码:

springboot 搭建sql server springboot如何搭建_java_09


可以看出其是一个合成体,但其中最重要的三个注解分别是:

  • @SpringBootConfiguration
  • @EnableAutoConfiguration
  • @ComponentScan
    所以我们也可以把启动类上的@SpringBootApplication用这三个注解替换(如果不嫌麻烦的话)
@SpringBootConfiguration
@EnableAutoConfiguration 
@ComponentScan
public class DemoApplication{
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

@RestController注解和@Controller注解区别
解释:@RestController注解相当于@ResponseBody + @Controller合在一起的作用

  1. 使用@RestController注解时Controller中的方法无法返回jsp页面,或者html,配置的视图解析器 InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。
  2. 如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。
    如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。

举个例子:
  当@Controller 注解在对应的方法上时,视图解析器可以解析return 的jsp,html页面,并且跳转到相应页面,
若需要返回json等内容信息到页面,则需要加 @ResponseBody 注解。

做个小小的测试:
  由于springboot不推荐jsp开发,推荐使用thymeleaf模版引擎进行web页面开发,所以我们需要在pom.xml添加thymeleaf依赖:

<!--thymeleaf模板引擎依赖-->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

application.properties中添加以下配置,用来指定跳转页面的存放路径和页面的格式信息:

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

templates目录下创建index.html文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>Hello SpringBoot</h1>
</body>
</html>

看一下测试代码:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class test {
    @RequestMapping("index")
    public String Index(){
        return "index";
    }
}

启动入口程序,访问 http://localhost:8080/index 查看效果

springboot 搭建sql server springboot如何搭建_spring_10

总结:
  之前使用SpringBoot都是碎片式的学习,感觉没什么进步,因此准备系统性的学习SpringBoot以便于更深层次的理解该框架。SpringBoot在不同版本下所添加的依赖包也会有一些问题,所以在选择版本时需注意。