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