SpringCloud学习之旅02--weather项目构建

直接copy上篇构建的gradle项目进行修改整合:(自己新建一个gradle项目也行)

目录结构:

 

build.gradle文件:

// buildscript 代码块中脚本优先执行
buildscript {


// ext 用于定义动态属性
ext {
springBootVersion = '2.0.0.M4'
}


// 使用了Maven的中央仓库及Spring自己的仓库(也可以指定其他仓库)
repositories {
// mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
}


// 依赖关系
dependencies {


// classpath 声明了在执行其余的脚本时,ClassLoader 可以使用这些依赖项
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}


// 使用插件
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'


// 指定了生成的编译文件的版本,默认是打成了 jar 包
group = 'com.waylau.spring.cloud'
version = '1.0.0'


// 指定编译 .java 文件的 JDK 版本
sourceCompatibility = 1.8


// 使用了Maven的中央仓库及Spring自己的仓库(也可以指定其他仓库)
repositories {
//mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
}


// 依赖关系
dependencies {


// 该依赖用于编译阶段
compile('org.springframework.boot:spring-boot-starter-web')


// HttpClient
compile('org.apache.httpcomponents:httpclient:4.5.3')


// 该依赖用于测试阶段
testCompile('org.springframework.boot:spring-boot-starter-test')

}

 

编码转换的工具类:


 

package com.waylau.spring.cloud.weather.util;


import java.io.UnsupportedEncodingException;


public class EncodeUtil {
private final static String ENCODE = "UTF-8"; 
    /**
     * URL 解码
     *
     * @return String
     * @author lifq
     * @date 2015-3-17 下午04:09:51
     */
    public static String getURLDecoderString(String str) {
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = java.net.URLDecoder.decode(str, ENCODE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * URL 转码
     *
     * @return String
     * @author lifq
     * @date 2015-3-17 下午04:10:28
     */
    public static String getURLEncoderString(String str) {
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = java.net.URLEncoder.encode(str, ENCODE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 
     * @return void
     * @author lifq
     * @date 2015-3-17 下午04:09:16
     */
    public static void main(String[] args) {
        String str = "北京";
        System.out.println(getURLEncoderString(str));
        System.out.println(getURLDecoderString(str));
        
    }

}

 

实体对象:


 

package com.waylau.spring.cloud.weather.vo;


import java.io.Serializable;


/**
 * 未来天气.
 * 
 * @since 1.0.0 2017年11月21日
 * @author <a href="https://waylau.com">Way Lau</a> 
 */
public class Forecast implements Serializable {


private static final long serialVersionUID = 1L;
private String date;
private String high;
private String fengli;
private String low;
private String fengxiang;
private String type;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getHigh() {
return high;
}
public void setHigh(String high) {
this.high = high;
}
public String getFengli() {
return fengli;
}
public void setFengli(String fengli) {
this.fengli = fengli;
}
public String getLow() {
return low;
}
public void setLow(String low) {
this.low = low;
}
public String getFengxiang() {
return fengxiang;
}
public void setFengxiang(String fengxiang) {
this.fengxiang = fengxiang;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}



}

package com.waylau.spring.cloud.weather.vo;


import java.io.Serializable;
import java.util.List;


/**
 * 天气信息.
 * 
 * @since 1.0.0 2017年11月21日
 * @author <a href="https://waylau.com">Way Lau</a> 
 */
public class Weather implements Serializable {


private static final long serialVersionUID = 1L;

private String city;
private String aqi;
private String ganmao;
private String wendu;
private Yeaterday yesterday;
private List<Forecast> forecast;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getAqi() {
return aqi;
}
public void setAqi(String aqi) {
this.aqi = aqi;
}
public String getGanmao() {
return ganmao;
}
public void setGanmao(String ganmao) {
this.ganmao = ganmao;
}
public String getWendu() {
return wendu;
}
public void setWendu(String wendu) {
this.wendu = wendu;
}
public Yeaterday getYesterday() {
return yesterday;
}
public void setYesterday(Yeaterday yesterday) {
this.yesterday = yesterday;
}
public List<Forecast> getForecast() {
return forecast;
}
public void setForecast(List<Forecast> forecast) {
this.forecast = forecast;
}



}







 

package com.waylau.spring.cloud.weather.vo;


import java.io.Serializable;


/**
 * Weather Response.
 * 
 * @since 1.0.0 2017年11月21日
 * @author <a href="https://waylau.com">Way Lau</a> 
 */
public class WeatherResponse implements Serializable {


private static final long serialVersionUID = 1L;
private Weather data;
private Integer status;
private String desc;
public Weather getData() {
return data;
}
public void setData(Weather data) {
this.data = data;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}


}





package com.waylau.spring.cloud.weather.vo;


import java.io.Serializable;


/**
 * 昨日天气.
 * 
 * @since 1.0.0 2017年11月21日
 * @author <a href="https://waylau.com">Way Lau</a> 
 */
public class Yeaterday implements Serializable {
private static final long serialVersionUID = 1L;
private String date;
private String high;
private String fx;
private String low;
private String fl;
private String type;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getHigh() {
return high;
}
public void setHigh(String high) {
this.high = high;
}
public String getFx() {
return fx;
}
public void setFx(String fx) {
this.fx = fx;
}
public String getLow() {
return low;
}
public void setLow(String low) {
this.low = low;
}
public String getFl() {
return fl;
}
public void setFl(String fl) {
this.fl = fl;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}

}

 

service层:


 

package com.waylau.spring.cloud.weather.service;


import com.waylau.spring.cloud.weather.vo.WeatherResponse;


/**
 * Weather Data Service.
 * 
 * @since 1.0.0 2017年11月22日
 * @author <a href="https://waylau.com">Way Lau</a> 
 */
public interface WeatherDataService {
/**
* 根据城市ID查询天气数据
* 
* @param cityId
* @return
*/
WeatherResponse getDataByCityId(String cityId);


/**
* 根据城市名称查询天气数据
* 
* @param cityId
* @return
*/
WeatherResponse getDataByCityName(String cityName);

Object getCityIdList();
}


 

package com.waylau.spring.cloud.weather.service;


import java.io.IOException;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.waylau.spring.cloud.weather.vo.WeatherResponse;


/**
 * WeatherDataService 实现.
 * 
 * @since 1.0.0 2017年11月22日
 * @author <a href="https://waylau.com">Way Lau</a> 
 */
@Service
public class WeatherDataServiceImpl implements WeatherDataService {
private static final String WEATHER_URI = "http://wthrcdn.etouch.cn/weather_mini?";

@Autowired
private RestTemplate restTemplate;

@Override
public WeatherResponse getDataByCityId(String cityId) {
String uri = WEATHER_URI + "citykey=" + cityId;
return this.doGetWeahter(uri);
}


@Override
public WeatherResponse getDataByCityName(String cityName) {
String uri = WEATHER_URI + "city=" + cityName;
return this.doGetWeahter(uri);
}

private WeatherResponse doGetWeahter(String uri) {
  ResponseEntity<String> respString = restTemplate.getForEntity(uri, String.class);

ObjectMapper mapper = new ObjectMapper();
WeatherResponse resp = null;
String strBody = null;

if (respString.getStatusCodeValue() == 200) {
strBody = respString.getBody();
}


try {
resp = mapper.readValue(strBody, WeatherResponse.class);
} catch (IOException e) {
e.printStackTrace();
}

return resp;
}


@Override
public Object getCityIdList() {
ResponseEntity<String> respString = restTemplate.getForEntity("http://mobile.weather.com.cn/js/citylist.xml", String.class);
return respString;
}



}






 

package com.waylau.spring.cloud.weather.config;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;


/**
 * Rest Configuration.
 * 
 * @since 1.0.0 2017年11月22日
 * @author <a href="https://waylau.com">Way Lau</a> 
 */
@Configuration
public class RestConfiguration {

@Autowired
private RestTemplateBuilder builder;


@Bean
public RestTemplate restTemplate() {
return builder.build();
}


}

 

WeatherController控制器:

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


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import com.waylau.spring.cloud.weather.service.WeatherDataService;
import com.waylau.spring.cloud.weather.vo.WeatherResponse;
/**
 * Weather Controller.
 * 
 * @since 1.0.0 2017年11月22日
 * @author <a href="https://waylau.com">Way Lau</a> 
 */
@RestController
@RequestMapping("/weather")
public class WeatherController {
@Autowired
private WeatherDataService weatherDataService;

/**
* localhost:8080/weather/cityId/101042400
* @param cityId
* @return
*/
@GetMapping("/cityId/{cityId}")
public WeatherResponse getWeatherByCityId(@PathVariable("cityId") String cityId) {
return weatherDataService.getDataByCityId(cityId);
}

/**
* localhost:8080/weather/cityName/%E5%8C%97%E4%BA%AC
* @param cityName   北京   %E5%8C%97%E4%BA%AC
* @return
*/
// @RequestMapping(value="/cityName/{cityName}", method=RequestMethod.GET,produces = "application/json; charset=utf-8")
@GetMapping("/cityName/{cityName}")
public WeatherResponse getWeatherByCityName(@PathVariable("cityName") String cityName) {
return weatherDataService.getDataByCityName(cityName);
}

/**
* 
* @param cityName
* @return
*/
@GetMapping("/cityIdList")
public Object cityIdList() {
return weatherDataService.getCityIdList();
}

}

 

访问链接:

http://localhost:8080/weather/cityId/101042400

http://localhost:8080/weather/cityName/北京

http://localhost:8080/weather/cityIdList

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值