<一> idea+gradle+springboot創建項目


轉載自https://windcoder.com/springbootchutan-chuangjianxiangmu

 

前言

一邊學習公司用到的技術,一邊重構小程序后端,從而更好的理解公司搭建的框架。此處記錄一個用idea+gradle+springboot的基礎實現。

所用環境

IntelliJ IDEA 2017.1.1

JDK1.8

Gradle4.3

Spring Boot1.5.8.RELEASE

創建Gradle項目

1、new Project

如圖在gradle中選擇java和web

springBoot初探-創建項目

2、填寫基礎信息

填寫GroupId、ArtifactId、Version三項,和maven中一樣。

3、選擇gradle基礎

此處是如圖選擇,具體含義見下面名詞解釋:

springBoot初探-創建項目

相關名詞

 

  • Use auto-import | 是否開啟自動導入,若開啟修改gradle腳本文件后會自動檢測變化並對項目進行刷新。
  • Create directories for empty content roots automatically | 導入或者創建gradle項目時,是否自動創建標准目錄結構。
  • Create separate module per source set | 讓每個模塊單獨采用Gradle約定的source set結構去執行構建。
  • Use default gradle wrapper (recommended) | 使用Gradle Wrapper(如果一定要翻譯的話,可以稱之為Gradle 包裝器),這可以使得項目組成員不必預先安裝好gradle即可執行gradle腳本,同時也便於統一項目所使用的gradle版本,當然雖說是不必預裝其實是它會自己去官網幫你下載一個,然而gradle安裝包體積不小同時又有牆的過濾,所以開啟此項最好事先備好梯子。
  • Use gradle wrapper task configuration | 自定義Gradle Wrapper配置。
  • Use local gradle distribution | 采用本地安裝的Gradle執行腳本
  • Gradle home | 選擇你的Gradle安裝目錄即可,無需選擇到bin
  • Gradle JVM | 選擇構建Gradle項目使用的JVM,默認是項目采用的JDK

 

4、標准目錄結構

執行完成后需等待一會gradle的自動創建過程,然后可看到如圖所示的目錄結構

springBoot初探-創建項目

添加SpringBoot依賴

在build.gradle文件中的dependencies 中添加springboot依賴:

 compile("org.springframework.boot:spring-boot-starter-web:1.5.8.RELEASE")

創建含main函數的AppLicationController類:

package com.windcoder.nightbook.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Description:
 * User: WindCoder
 * Date: 2017-11-12
 * Time: 22:30 下午
 */
@Controller
@EnableAutoConfiguration
public class AppLicationController {

    @ResponseBody
    @RequestMapping("/helloMain")
    public String home(String name){
        return "Hello "+name+"! This is Spring-boot test One";
    }

    public static void main(String[] args){
        SpringApplication.run(AppLicationController.class,args);
    }
}

運行

在AppLicationController類處右鍵-執行run即可。此時不需要管存不存在web.xml,也不需要創建application.properties文件。

新建自定義Controller

到現在為止依舊是spingBoot官方的實例,但此時若自己建一個自定義的controller類,如HelloController 類:

package com.windcoder.nightbook.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Description:
 * User: WindCoder
 * Date: 2017-11-12
 * Time: 22:32 下午
 */
@Controller
@RequestMapping("/api")
public class HelloController {

    @ResponseBody
    @RequestMapping(value ="/hello",method = RequestMethod.GET)
    public String home(String name){
        return "Hello "+name+"! This is Spring-boot test";
    }
}

重啟執行發現會報如下錯誤:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

此時需要略作修改才能讓程序找到——將@EnableAutoConfiguration替換為@SpringBootApplication:

package com.windcoder.nightbook.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Description:
 * User: WindCoder
 * Date: 2017-11-12
 * Time: 22:30 下午
 */
@Controller
@SpringBootApplication
public class AppLicationController {

    @ResponseBody
    @RequestMapping("/helloMain")
    public String home(String name){
        return "Hello "+name+"! This is Spring-boot test One";
    }

    public static void main(String[] args){
        SpringApplication.run(AppLicationController.class,args);
    }
}

若是該類中沒有訪問路徑,可以將@Controller注解去掉。

還有一種實現方式是對含main的類增加注解,ComponentScan方式:

@Configuration
@EnableAutoConfiguration
@ComponentScan

此處暫且不再重貼代碼。

之后重新運行便可訪問到自定義Controller中的路徑。

弦外音:

很多Spring Boot開發者經常使用@Configuration@EnableAutoConfiguration@ComponentScan注解他們的main類,由於這些注解如此頻繁地一塊使用,Spring Boot就提供了一個方便的@SpringBootApplication注解作為代替。

@SpringBootApplication注解等價於以默認屬性使用@Configuration@EnableAutoConfiguration@ComponentScan

@SpringBootApplication注解也提供了用於自定義@EnableAutoConfiguration@ComponentScan屬性的別名(aliases)。

 

 

參考資料

Spring Boot 官方

[Gradle中文教程系列]-跟我學Gradle-14.1:IDEA中Gradle插件的使用

使用Intellij Idea+Gradle 搭建Java 本地開發環境

SpringBoot配置非含main類的Controller的注解

spring boot 自定義Controller,不能被掃描

 延伸

Spring boot(9) 的異常,以及異常頁面的處理

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM