1.springboot:hello world

1.新建一個(gè)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) 問題解決。

如果還有其他錯(cuò)誤,解決方法相同。

2.在pom.xml文件中添加Spring BootMaven依賴

依賴1:

<!-- spring boot父節(jié)點(diǎn)依賴,引入這個(gè)之后相關(guān)的引入就不需要添加version配置,spring boot會自動(dòng)選擇最合適的版本進(jìn)行添加 -->
  <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,默認(rèn)為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,默認(rèn)為1.6 -->
    <java.version>1.8</java.version>
  </properties>
  
  <!-- spring boot父節(jié)點(diǎn)依賴,引入這個(gè)之后相關(guān)的引入就不需要添加version配置,spring boot會自動(dòng)選擇最合適的版本進(jìn)行添加 -->
  <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啟動(dòng)spring,那么以下plugin必須要添加,否則是無法啟動(dòng)的。 
  如果使用maven的spring-boot:run的話是不需要此配置的。
  (我在測試的時(shí)候,如果不配置下面的plugin也是直接在Main中運(yùn)行的。) --> 

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

3.新建一個(gè)類文件:HelloController.java

package com.zhaolixiang.spring_boot1;

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

/**
 * 在這里我們使用RestController(等價(jià)于@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進(jìn)程");
        return "hello";
    }

}

4.設(shè)置啟動(dòng)入口:

在App.class類中設(shè)置:

public static void main( String[] args )
    {
        /**
         * 在Main方法中啟動(dòng)我們的應(yīng)用程序
         */
        System.out.println( "Hello World!" );
        SpringApplication.run(App.class, args);
       
    }

如果報(bào)錯(cuò):

The type org.springframework.context.ConfigurableApplicationContext cannot be resolved.
It is indirectly referenced from required .class files

原因:你正要使用的類調(diào)用了另一個(gè)類,而這個(gè)類又調(diào)用了其他類,這種關(guān)系可能會有好多層。而在這個(gè)調(diào)用的過程中,某個(gè)類所在的包的缺失就會造成以上那個(gè)錯(cuò)誤。

解決方法:

  <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>  

5.啟動(dòng):右鍵Run As -> Java Application

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容