此文做記錄交流,如有不當(dāng),還望指正。
開(kāi)發(fā)使用springboot也有段時(shí)間了,最近發(fā)現(xiàn)很多之前用過(guò)的東西都已經(jīng)遺忘,遂記錄下來(lái)
創(chuàng)建空的maven項(xiàng)目

修改pom文件
<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>com.demo</groupId>
<artifactId>springboot-helloword</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-helloword</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
編寫(xiě)啟動(dòng)類(lèi)
package com.demo.springboot_helloword;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloWordApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWordApplication.class, args);
}
}
在啟動(dòng)類(lèi)中添加REST方法
package com.demo.springboot_helloword;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class HelloWordApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWordApplication.class, args);
}
@RequestMapping("/index")
public String index(){
return "HelloWord";
}
}
選中HelloWordApplication,右鍵選擇run as,選擇java application 或者 spring boot app 都可以,啟動(dòng)完成后在瀏覽器訪問(wèn) http://localhost:8080/index 查看效果
@SpringBootApplication 注解
@SpringBootApplication是一個(gè)組合注解,包括@SpringBootConfiguration,@EnableAutoConfiguration,@ComponentScan及其他多個(gè)注解,是項(xiàng)目的啟動(dòng)注解
@SpringBootConfiguration:當(dāng)前類(lèi)是一個(gè)配置類(lèi),就像xml配置文件,而現(xiàn)在是用java配置文件,效果是一樣的
@EnableAutoConfiguration:完成自動(dòng)配置功能
@ComponentScan:用注解配置實(shí)現(xiàn)自動(dòng)掃描,默認(rèn)會(huì)掃描當(dāng)前包和所有子包,和xml配置自動(dòng)掃描效果一樣,@Filter是排除了兩個(gè)系統(tǒng)類(lèi)
@RestController 注解
@RestController 本身也是一個(gè)組合注解,是由@Controller和@ResponseBody組成的
@Controller:標(biāo)注此類(lèi)是一個(gè)controller
@ResponseBody:標(biāo)注一個(gè)contrller中的方法直接把數(shù)據(jù)寫(xiě)到ResponseBosy而不是返回一個(gè)頁(yè)面。
@RequestMapping 注解
@RequestMapping是用來(lái)指定請(qǐng)求路徑的,既可以作用在類(lèi)上也可以作用在方法上,如果作用在類(lèi)上則表示此路徑為請(qǐng)求父路徑
spring-boot-starter-parent
SpringBoot父依賴,配置后可使用SpringBoot依賴管理
spring-boot-starter-web
SpringBoot web 集成了spring mvc,RESTFUL,所需容器等