Feign的HelloWorld

Scroll Down

GitHub地址

https://github.com/OpenFeign/feign

Feign是干啥的?

GitHub官网上Feign的描述是“Feign makes writing java http clients easier” Feign让编写Java Http客户端更方便。

Feign HelloWorld

interface Weather{

    @RequestLine("GET /")
    String index();
}

@Test
public void test(){
    Weather weather = Feign.builder()
            .client(new Client.Default(null,null))
            .decoder(new Decoder.Default())
            .encoder(new Encoder.Default())
            .target(Weather.class, "http://t.weather.sojson.com/api/weather/city/101030100");
    String result = weather.index();
    log.info("weather result:{}", result);
}

执行结果

09:20:26.588 [main] INFO com.wangshouyu.feign.FeignTest - weather result:{"message":"success感谢又拍云(upyun.com)提供CDN赞助","status":200,"date":"20200525","time":"2020-05-25 09:20:26","cityInfo":{"city":"天津市","citykey":"101030100","parent":"天津","updateTime":"08:31"},"data":{"shidu":"61%","pm25":24.0,"pm10":68.0,"quality":"良","wendu":"24","ganmao":"极少数敏感人群应减少户外活动","forecast":[{"date":"25","high":"高温 31℃","low":"低温 18℃","ymd":"2020-05-25","week":"星期一","sunrise":"04:52","sunset":"19:25","aqi":36,"fx":"南风","fl":"3-4级","type":"晴","notice":"愿你拥有比阳光明媚的心情"},{"date":"26","high":"高温 27℃","low":"低温 16℃","ymd":"2020-05-26","week":"星期二","sunrise":"04:51","sunset":"19:26","aqi":60,"fx":"东南风","fl":"3-4级","type":"多云","notice":"阴晴之间,谨防紫外线侵扰"},{"date":"27","high":"高温 33℃","low":"低温 18℃","ymd":"2020-05-27","week":"星期三","sunrise":"04:50","sunset":"19:27","aqi":60,"fx":"东南风","fl":"<3级","type":"阴","notice":"不要被阴云遮挡住好心情"},{"date":"28","high":"高温 33℃","low":"低温 20℃","ymd":"2020-05-28","week":"星期四","sunrise":"04:50","sunset":"19:28","aqi":24,"fx":"南风","fl":"<3级","type":"晴","notice":"愿你拥有比阳光明媚的心情"},{"date":"29","high":"高温 32℃","low":"低温 18℃","ymd":"2020-05-29","week":"星期五","sunrise":"04:49","sunset":"19:28","aqi":26,"fx":"东南风","fl":"<3级","type":"晴","notice":"愿你拥有比阳光明媚的心情"},{"date":"30","high":"高温 34℃","low":"低温 20℃","ymd":"2020-05-30","week":"星期六","sunrise":"04:49","sunset":"19:29","aqi":46,"fx":"东南风","fl":"3-4级","type":"晴","notice":"愿你拥有比阳光明媚的心情"},{"date":"31","high":"高温 33℃","low":"低温 18℃","ymd":"2020-05-31","week":"星期日","sunrise":"04:48","sunset":"19:30","aqi":43,"fx":"东风","fl":"4-5级","type":"多云","notice":"阴晴之间,谨防紫外线侵扰"},{"date":"01","high":"高温 33℃","low":"低温 18℃","ymd":"2020-06-01","week":"星期一","sunrise":"04:48","sunset":"19:31","aqi":76,"fx":"西北风","fl":"3-4级","type":"阴","notice":"不要被阴云遮挡住好心情"},{"date":"02","high":"高温 30℃","low":"低温 19℃","ymd":"2020-06-02","week":"星期二","sunrise":"04:46","sunset":"19:31","aqi":26,"fx":"东风","fl":"3级","type":"多云","notice":"阴晴之间,谨防紫外线侵扰"},{"date":"03","high":"高温 34℃","low":"低温 18℃","ymd":"2020-06-03","week":"星期三","sunrise":"04:46","sunset":"19:32","aqi":29,"fx":"西南风","fl":"2级","type":"晴","notice":"愿你拥有比阳光明媚的心情"},{"date":"04","high":"高温 35℃","low":"低温 18℃","ymd":"2020-06-04","week":"星期四","sunrise":"04:45","sunset":"19:33","aqi":89,"fx":"西北风","fl":"3级","type":"多云","notice":"阴晴之间,谨防紫外线侵扰"},{"date":"05","high":"高温 37℃","low":"低温 25℃","ymd":"2020-06-05","week":"星期五","sunrise":"04:45","sunset":"19:33","aqi":79,"fx":"西北风","fl":"3级","type":"晴","notice":"愿你拥有比阳光明媚的心情"},{"date":"06","high":"高温 22℃","low":"低温 16℃","ymd":"2020-06-06","week":"星期六","sunrise":"04:51","sunset":"19:24","aqi":44,"fx":"北风","fl":"3级","type":"小雨","notice":"雨虽小,注意保暖别感冒"},{"date":"07","high":"高温 33℃","low":"低温 18℃","ymd":"2020-06-07","week":"星期日","sunrise":"04:45","sunset":"19:34","aqi":10,"fx":"东南风","fl":"3级","type":"小雨","notice":"雨虽小,注意保暖别感冒"},{"date":"08","high":"高温 37℃","low":"低温 25℃","ymd":"2020-06-08","week":"星期一","sunrise":"04:45","sunset":"19:35","aqi":14,"fx":"西南风","fl":"2级","type":"多云","notice":"阴晴之间,谨防紫外线侵扰"}],"yesterday":{"date":"24","high":"高温 30℃","low":"低温 17℃","ymd":"2020-05-24","week":"星期日","sunrise":"04:52","sunset":"19:25","aqi":41,"fx":"东南风","fl":"<3级","type":"多云","notice":"阴晴之间,谨防紫外线侵扰"}}}

可以看到上面的信息返回的是一段json的数据。

Decoder

编写天气信息,因为我只想要今日的天气的一些信息,所以我简单的封装了下对象的信息。

@Data
@ToString
class WeatherInfo{

    private String shidu;

    private Double pm25;

    private Double pm10;

    private String quality;

    private String wendu;

    private String ganmao;
}

class WeatherDecoder implements Decoder{

    @Override
    public Object decode(Response response, Type type) throws IOException, DecodeException, FeignException {
        // 判断body是否为空
        if(null == response.body()){
            return null;
        }
        Reader reader = response.body().asReader(UTF_8);
        Gson gson = new Gson();
        Object o = gson.fromJson(reader, Object.class);
        String jsonResult = gson.toJson(o);
        JsonParser parser = new JsonParser();
        JsonObject jsonObject = parser.parse(jsonResult).getAsJsonObject();
        JsonElement data = jsonObject.get("data");
        return gson.fromJson(data, type);
    }
}

从json的结构上来看,我们想要的也就是data的部分数据。

interface Weather{
    @RequestLine("GET /")
    WeatherInfo index();
}
@Test
public void test(){
    Weather weather = Feign.builder()
            .client(new Client.Default(null,null))
            .decoder(new WeatherDecoder())
            .encoder(new Encoder.Default())
            .target(Weather.class, "http://t.weather.sojson.com/api/weather/city/101030100");
    WeatherInfo result = weather.index();
    log.info("weather result:{}", result);
}

执行结果

09:39:32.169 [main] INFO com.wangshouyu.feign.FeignTest - weather result:FeignTest.WeatherInfo(shidu=61%, pm25=24.0, pm10=68.0, quality=良, wendu=24, ganmao=极少数敏感人群应减少户外活动)