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

 

spring cloud - 概述

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

 

使用STS构建gradle项目:

 

项目目录结构:

这里所有的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

 

http://localhost:8080/getWeatherByCityId

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值