SpringCloud学习之旅01--gradle项目构建

 

spring cloud - 概述

参考链接:https://blog.csdn.net/yangspgao/article/details/76797606

SpringCloud学习之旅01--gradle项目构建_第1张图片

 

使用STS构建gradle项目:

SpringCloud学习之旅01--gradle项目构建_第2张图片

 

项目目录结构:

SpringCloud学习之旅01--gradle项目构建_第3张图片

这里所有的jar不是pom.xml文件了,全在build.gradle文件里面:


 

buildscript {
ext {
springBootVersion = '2.0.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}


apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'


group = 'com.cxb'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8


repositories {
mavenCentral()
maven { url "https://repo.spring.io/milestone" }
}


configurations {
providedRuntime
}


ext {
springCloudVersion = 'Finchley.RC2'
}



dependencies {

        //这里就是所有的依赖了

compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2')
compile('org.springframework.cloud:spring-cloud-starter')
runtime('mysql:mysql-connector-java')
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
testCompile('org.springframework.boot:spring-boot-starter-test')
}


dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}

}

hello控制器:


 

package com.cxb.spring.cloud.weather.controller;


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


@RestController
public class HelloController {


// @RequestMapping("/hello")
@GetMapping("/hello")
public String hello() {
return "Hello World!";
}


}

 

WeatherController控制器:

package com.cxb.spring.cloud.weather.controller;


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


@RestController
public class WeatherController {

/**
* 获取指定ID城市的天气    忠县的ID
* http://wthrcdn.etouch.cn/weather_mini?citykey=101042400
* @return
*/
@GetMapping("/getWeatherByCityId")
public String getWeatherByCityId() {
return "获取指定ID城市的天气";
}

/**
* 获取指定名字城市的天气
* http://wthrcdn.etouch.cn/weather_mini?city=忠县
* @return
*/
@GetMapping("/getWeatherByCityName")
public String getWeatherByCityName() {
return "获取指定名字城市的天气";
}

/**
* 获取城市列表
* http://mobile.weather.com.cn/js/citylist.xml
*/
@GetMapping("/getCityList")
public String getCityList() {
return "获取城市列表";
}



}

 

启动类:(Application 、ServletInitializer  构建项目自动生成的)

package com.cxb.spring.cloud.weather;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;


@SpringBootApplication
//没有连接数据库的时候报错  需要加上这一句
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class Application {


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

}

 

package com.cxb.spring.cloud.weather;


import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;


public class ServletInitializer extends SpringBootServletInitializer {


@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}



}

 

启动访问链接:

http://localhost:8080/hello

SpringCloud学习之旅01--gradle项目构建_第4张图片

 

http://localhost:8080/getWeatherByCityId

SpringCloud学习之旅01--gradle项目构建_第5张图片

你可能感兴趣的:(springcloud)