笔记摘要
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。
Spring Boot特性:
- 创建独立的Spring应用程序
- 嵌入的Tomcat,无需部署WAR文件
- 简化Maven配置
- 自动配置Spring
- 提供生产就绪型功能,如指标,健康检查和外部配置
- 开箱即用,没有代码生成,也无需XML配置
Spring Boot特性理解:
- 为基于Spring的开发提供更快的入门体验
- 开箱即用,没有代码生成,也无需XML配置。同时也可以修改默认值来满足特定的需求。
- 提供了一些大型项目中常见的非功能特性,如嵌入式服务器,安全,指标,健康监测,外部配置等。
- Spring Boot并不是Spring功能上的增强,而是提供了一种快速使用Spring的方式。
Eclipse实现HelloWorld
实验过程:
pom.xml文件配置
pom.xml文件配置后工程会报错,Update Maven Project即可。
<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>my.spring</groupId>
<artifactId>helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>helloworld</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8.0_144</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>
</dependency>
</dependencies>
</project>
MyController类代码
package my.spring.helloworld;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@RequestMapping("/helloworld")
@ResponseBody
public String hello() {
return "helloworld";
}
}
App类代码
package my.spring.helloworld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Hello world!
*
*/
@SpringBootApplication
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
BUG记录
配置pom.xml时节点下参数为1.4.1.RELEASE时,Update Maven Project系统报错,值更换为1.5.4.RELEASE时可正常运行;
MyController类未使用@Controller注解导致HTTP 404错误;
MyController类未使用@ResponseBody注解导致Whitelabel Error Page错误。
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Mar 07 21:19:30 CST 2018
There was an unexpected error (type=Internal Server Error, status=500).
Circular view path [helloworld]: would dispatch back to the current handler URL [/helloworld] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)