1.新建一個Maven Java工程
創(chuàng)建后若出現(xiàn)形如下列的問題:
Failure to transfer org.apache.maven:maven-archiver:pom:2.4.2 from http://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are
forced. Original error: Could not transfer artifact org.apache.maven:maven-archiver:pom:2.4.2 from/to central (http://repo.maven.apache.org/maven2): connection timed out to http://repo.maven.apache.org/maven2/org/apache/maven/maven-
archiver/2.4.2/maven-archiver-2.4.2.pom
解決方法:
在M2文件夾里發(fā)現(xiàn)maven-archiver沒有下載芒填,只有l(wèi)astupdate文件,刪除之治筒;重新project -> Maven - Update Dependencies(快捷鍵ALT+F5) 問題解決。
如果還有其他錯誤,解決方法相同。
2.在pom.xml文件中添加Spring BootMaven依賴
依賴1:
<!-- spring boot父節(jié)點依賴腾降,引入這個之后相關(guān)的引入就不需要添加version配置,spring boot會自動選擇最合適的版本進行添加 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
</parent>
依賴2:設(shè)置jdk版本
<!-- 指定jdk版本碎绎,此處為1.8螃壤,默認為1.6 -->
<java.version>1.8</java.version>
依賴3:
<!--spring-boot-starter-web:為我們打包了:MVC,AOP(面向切面編程)筋帖。奸晴。的依賴包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--
<version></version>
由于我們上面指定了parent(springboot),springboot 會代替我們指定最合適的版本號,所有這里我們就不指定了
-->
</dependency>
</dependencies>
總文件:
<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.zhaolixiang</groupId>
<artifactId>spring-boot1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot1</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 指定jdk版本日麸,此處為1.8蚁滋,默認為1.6 -->
<java.version>1.8</java.version>
</properties>
<!-- spring boot父節(jié)點依賴,引入這個之后相關(guān)的引入就不需要添加version配置,spring boot會自動選擇最合適的版本進行添加 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!--spring-boot-starter-web:為我們打包了:MVC辕录,AOP(面向切面編程)睦霎。。的依賴包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--
<version></version>
由于我們上面指定了parent(springboot),springboot 會代替我們指定最合適的版本號走诞,所有這里我們就不指定了
-->
</dependency>
</dependencies>
<!--如果我們要直接Main啟動spring副女,那么以下plugin必須要添加,否則是無法啟動的蚣旱。
如果使用maven的spring-boot:run的話是不需要此配置的碑幅。
(我在測試的時候,如果不配置下面的plugin也是直接在Main中運行的塞绿。) -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin </artifactId>
</plugin>
</plugins>
</build>
</project>
3.新建一個類文件:HelloController.java
package com.zhaolixiang.spring_boot1;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;;
/**
* 在這里我們使用RestController(等價于@Controller和@RequestBody)
* @author hasee
*
*/
@RestController
public class HelloController {
/**
* 這里我們使用@RequestMapping建立請求地址:
* http://127.0.0.1:8080/hello
* @return
*/
@RequestMapping("/hello")
public String hello(){
System.out.println("hello進程");
return "hello";
}
}
4.設(shè)置啟動入口:
在App.class類中設(shè)置:
public static void main( String[] args )
{
/**
* 在Main方法中啟動我們的應(yīng)用程序
*/
System.out.println( "Hello World!" );
SpringApplication.run(App.class, args);
}
如果報錯:
The type org.springframework.context.ConfigurableApplicationContext cannot be resolved.
It is indirectly referenced from required .class files
原因:你正要使用的類調(diào)用了另一個類沟涨,而這個類又調(diào)用了其他類,這種關(guān)系可能會有好多層异吻。而在這個調(diào)用的過程中裹赴,某個類所在的包的缺失就會造成以上那個錯誤。
解決方法:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
改為:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
</parent>