group 'www.muzi.com' version '1.0-SNAPSHOT' buildscript{ ext{ nexusUrlPrefix = "http://ip:8081" springBootVersion = '1.5.8.RELEASE' } repositories { mavenLocal() //添加Maven本地資源庫 maven{ url "${nexusUrlPrefix}/nexus/content/groups/public/" } } dependencies{ classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' //添加Java插件 apply plugin: 'maven'//添加Maven插件 apply plugin: 'maven-publish'//添加Maven發佈插件 apply plugin: 'idea' apply plugin: 'spring-boot' jar{ baseName = "test-boot" version = "${version}" } //指定編碼環境 sourceCompatibility = 1.8 //指定編譯環境 targetCompatibility = 1.8 //設置編碼格式 tasks.withType(JavaCompile){ options.encoding = "UTF-8" } //添加資源庫 repositories { mavenLocal() //添加Maven本地資源庫 maven{ url "${nexusUrlPrefix}/nexus/content/groups/public/" } } //默認發佈到Maven Nexus私服的發行庫 def nexusUrl="${nexusUrlPrefix}/nexus/content/repositories/releases/" //若是爲快照版本發佈到Maven Nexus私服的快照庫 if (version.endsWith("-SNAPSHOT")){ nexusUrl="${nexusUrlPrefix}/nexus/content/repositories/snapshots/" } //上傳nexus資源配置 uploadArchives { repositories{ mavenDeployer{ //上傳資源到Maven私服 repository(url:nexusUrl){ authentication(userName:"admin",password:"admin123") } pom.version ="$project.version" pom.artifactId ="tools-mz" pom.groupId ="$project.group" } } } dependencies { compile( ["org.springframework.boot:spring-boot-starter-web"] ) testCompile("org.springframework.boot:spring-boot-starter-test") }
package www.muzi.com; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Date:2017/3/23 14:25 */ /** * 在這裏咱們使用@RestController(等價於@Controller和@RequestBody) */ @RestController public class HelloController { /** * 在這裏使用@RequestMapping創建映射請求:http://127.0.0.1:8080/hello */ @RequestMapping("/haha") public String hello(){ return "哈哈哈"; } }
package www.muzi.com; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Hello world! * */ /** * 在這裏使用@SpringBootApplication指定這是一個SpringBoot的應用程序 */ @SpringBootApplication public class App { public static void main( String[] args ) { /** * 在main方法中進行啓動咱們的應用程序 */ SpringApplication.run(App.class, args); } }
@SpringBootApplication開啓了Spring 的組件掃描和Spring Boot的自動配置功能。實際
上,@SpringBootApplication將三個有用的註解組合在了一塊兒。 java
第一種運行方式:右鍵Run'App.main()',而後在瀏覽器輸入測試訪問地址:http://127.0.0.1:8080/haha (項目默認端口8080)web
第二種運行方式:點擊Gradle Projects->Tasks->application->bootRunspring