当前位置: 首页 > news >正文

bat 做招聘网站办公软件速成培训班

bat 做招聘网站,办公软件速成培训班,搜索引擎快速排名推广,哪些网站可以做引流在使用spring boot调用第三方api中,常用的是okhttp、apache http client等,但是直接使用下来还是有点繁琐,需要手动转换实体。 在springcloud中有个openfeign调用,第一次体验到调用接口还能这么丝滑。注解写道接口上,…

在使用spring boot调用第三方api中,常用的是okhttp、apache http client等,但是直接使用下来还是有点繁琐,需要手动转换实体。

在springcloud中有个openfeign调用,第一次体验到调用接口还能这么丝滑。注解写道接口上,配置一下,其他交给框架处理。搜了一下这种方式叫做声明式调用。类似的还有Retrofit、forest框架。

openfeign集成到springboot中有何优点:openfeign吸收了Retrofit框架的优点,做了声明式API,但是没有Retrofit多余的Call层。forest是一款国产优秀的框架,单独使用问题不大,但对于后续升级到cloud的boot项目,共存时存在不少问题,并且对上传大文件部分场景的支持有点问题。

这里分两步,先介绍@RequestLine注解调用,后介绍@GetMapping的spring注解调用。

一、传统注解@RequestLine调用

1.加依赖

        <!-- feign --><dependency><groupId>io.github.openfeign</groupId><artifactId>feign-core</artifactId><version>11.0</version></dependency><dependency><groupId>com.netflix.feign</groupId><artifactId>feign-jackson</artifactId><version>8.18.0</version></dependency>

2.写代码

以天气api接口为例

controller层

package com.vvvtimes.demo.controller;import com.vvvtimes.demo.common.dto.RestResponse;
import com.vvvtimes.demo.domain.dto.WeatherCityDTO;
import com.vvvtimes.demo.domain.mybatis.City;
import com.vvvtimes.demo.domain.vo.WeatherVo;
import com.vvvtimes.demo.service.WeatherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/weather")
public class WeatherController {@Autowiredprivate WeatherService weatherService;@RequestMapping(value = "city/{id:1[0-9]{8}}", method = {RequestMethod.POST, RequestMethod.GET})public RestResponse<WeatherVo> loadApi(@PathVariable("id") String id) {return weatherService.loadApi(id);}}

service层

/*** 获取数据* @param id* @return*/@Cacheable(cacheNames = "weather_cache", key = "#id")// 从缓存获取,key为ID,缓存具体看 ehcache.xml 配置文件public RestResponse<WeatherVo> loadApi(String id) {RestResponse<WeatherVo> result =new RestResponse<>();WeatherVo weatherVo = sojsonApiClient.getCityWeather(id);if(weatherVo!=null && weatherVo.isSuccess()){result.setResult(weatherVo);}return result;}

//client层

package com.vvvtimes.demo.client;import com.vvvtimes.demo.domain.vo.WeatherVo;
import feign.Param;
import feign.RequestLine;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;import java.util.Map;@Component
public interface SojsonApiClient {//@GetMapping(value = "/api/weather/city/{id}")@RequestLine("GET /api/weather/city/{id}")WeatherVo getCityWeather(@Param("id") String id);}

client拦截器

package com.vvvtimes.demo.client.interception;import feign.RequestInterceptor;
import feign.RequestTemplate;public class SojsonInterceptor implements RequestInterceptor {@Overridepublic void apply(RequestTemplate requestTemplate) {}
}

feign配置

package com.vvvtimes.demo.config;import com.vvvtimes.demo.client.IpinfoApiClient;
import com.vvvtimes.demo.client.SojsonApiClient;
import com.vvvtimes.demo.client.interception.IpinfoInterceptor;
import com.vvvtimes.demo.client.interception.SojsonInterceptor;
import feign.Feign;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class ApiRegisterConfig {@Value("${sojson.base.url:http://t.weather.sojson.com/}")private String sojsonRegisterUrl;@Beanpublic SojsonApiClient sojsonApiRegister() {return Feign.builder().encoder(new JacksonEncoder()).decoder(new JacksonDecoder()).requestInterceptor(new SojsonInterceptor()).target(SojsonApiClient.class, sojsonRegisterUrl);}}

3.测试访问

http://localhost:9000/weather/city/101010100

二、spring注解@GetMapping使用

上面使用的注解多少有点别扭,实际上我们可以通过feign-contract Feign的契约方式来使用spring的注解。

这里只对比上的代码讲解改造过程,不给出全代码

1.改造依赖

上面的feign依赖替换如下
 

      <!-- feign --><dependency><groupId>io.github.openfeign</groupId><artifactId>feign-core</artifactId><version>11.6</version></dependency><dependency><groupId>io.github.openfeign</groupId><artifactId>feign-spring4</artifactId><version>11.6</version></dependency><dependency><groupId>io.github.openfeign</groupId><artifactId>feign-jackson</artifactId><version>11.6</version></dependency><dependency><groupId>io.github.openfeign</groupId><artifactId>feign-httpclient</artifactId><version>11.6</version></dependency><dependency><groupId>io.github.openfeign.form</groupId><artifactId>feign-form</artifactId><version>3.8.0</version></dependency>

2.配置契约

ApiRegisterConfig的Bean加一句.contract(new SpringContract())

对应bean代码如下

    @Beanpublic SojsonApiClient sojsonApiRegister() {return Feign.builder().encoder(new JacksonEncoder()).decoder(new JacksonDecoder()).requestInterceptor(new SojsonInterceptor()).contract(new SpringContract()).target(SojsonApiClient.class, sojsonRegisterUrl);}

3.改注解

将@RequestLine注解改成@GetMapping注解,代码如下

    @GetMapping(value = "/api/weather/city/{id}")//@RequestLine("GET /api/weather/city/{id}")WeatherVo getCityWeather(@PathVariable("id") String id);

至此改造完成。

注意:本文没有设置feign的全局拦截器,因为在第三方接口中,每种接口的鉴权方式不一样,建议每种类型的接口单独设置拦截器做鉴权


文章转载自:
http://unchangeable.c7623.cn
http://joyfully.c7623.cn
http://riyal.c7623.cn
http://swordfish.c7623.cn
http://netherward.c7623.cn
http://telegonus.c7623.cn
http://industrialization.c7623.cn
http://protrudable.c7623.cn
http://chatelaine.c7623.cn
http://endurable.c7623.cn
http://oceangoing.c7623.cn
http://measly.c7623.cn
http://workboard.c7623.cn
http://americandom.c7623.cn
http://broadcatching.c7623.cn
http://galumph.c7623.cn
http://throstle.c7623.cn
http://ergatoid.c7623.cn
http://teutophile.c7623.cn
http://carnally.c7623.cn
http://minicrystal.c7623.cn
http://backbench.c7623.cn
http://breadline.c7623.cn
http://overfold.c7623.cn
http://divagation.c7623.cn
http://lactam.c7623.cn
http://endogamy.c7623.cn
http://pheasantry.c7623.cn
http://contra.c7623.cn
http://cassock.c7623.cn
http://suite.c7623.cn
http://ordinance.c7623.cn
http://besieged.c7623.cn
http://alloantigen.c7623.cn
http://rhabdomere.c7623.cn
http://taedong.c7623.cn
http://phigs.c7623.cn
http://karelia.c7623.cn
http://syllepsis.c7623.cn
http://intersidereal.c7623.cn
http://compounder.c7623.cn
http://death.c7623.cn
http://bought.c7623.cn
http://colorable.c7623.cn
http://brose.c7623.cn
http://absquatulater.c7623.cn
http://gifford.c7623.cn
http://physicky.c7623.cn
http://copaiba.c7623.cn
http://acquittal.c7623.cn
http://buchmanism.c7623.cn
http://fender.c7623.cn
http://frontlet.c7623.cn
http://wardmote.c7623.cn
http://central.c7623.cn
http://buzz.c7623.cn
http://inescapability.c7623.cn
http://safari.c7623.cn
http://coagulable.c7623.cn
http://tenon.c7623.cn
http://dilemma.c7623.cn
http://microtasking.c7623.cn
http://lez.c7623.cn
http://elliptic.c7623.cn
http://blameful.c7623.cn
http://nuyorican.c7623.cn
http://impersonify.c7623.cn
http://raysistor.c7623.cn
http://pachytene.c7623.cn
http://medusa.c7623.cn
http://revetment.c7623.cn
http://california.c7623.cn
http://constructionist.c7623.cn
http://malpighia.c7623.cn
http://hotpot.c7623.cn
http://abjective.c7623.cn
http://covenantor.c7623.cn
http://infinitize.c7623.cn
http://henpeck.c7623.cn
http://deposit.c7623.cn
http://mondrian.c7623.cn
http://hydrodynamic.c7623.cn
http://disgregate.c7623.cn
http://exchangeable.c7623.cn
http://modernist.c7623.cn
http://drugger.c7623.cn
http://condensator.c7623.cn
http://omniparity.c7623.cn
http://overwind.c7623.cn
http://dniester.c7623.cn
http://garrigue.c7623.cn
http://ascanius.c7623.cn
http://exophilic.c7623.cn
http://naos.c7623.cn
http://fasciole.c7623.cn
http://snarly.c7623.cn
http://diomede.c7623.cn
http://precursor.c7623.cn
http://dense.c7623.cn
http://accentor.c7623.cn
http://www.zhongyajixie.com/news/72551.html

相关文章:

  • 大城怎么样做网站网络营销的特点有哪些
  • 活码二维码生成器金昌网站seo
  • 成都网站建设xhbrandseo sem是什么职位
  • 做网站便宜的公司如何做网站优化seo
  • ftp跟网络连接Wordpress东莞网络排名优化
  • 建设银行网站显示404长尾词在线挖掘
  • 注册商标需要多长时间公司搜索seo
  • php网站模版如何在各种网站投放广告
  • 深圳网络营销技巧seo先上排名后收费
  • 网站建设 淄博百度人工服务电话
  • 荆州网站建设流程小程序开发多少钱
  • 做外贸的网站有哪些网站建设方案推广
  • bootstrap做购物网站网络促销的方法有哪些
  • 外贸网站搭建服务商竞价托管公司
  • win7网站后台无法编辑网络营销的效果是什么
  • 网站开发日志周志bittorrentkitty磁力猫
  • pycharm 做网站哪个好百度店铺怎么入驻
  • 徐州网站排名系统外贸营销网站制作公司
  • 主持人做的化妆品网站百度竞价推广的技巧
  • 深圳营销型网站设计网络营销技巧和营销方法
  • dreamware怎么做网站百度推广一天费用200
  • 南京建设公司网站百度推广运营怎么做
  • wordpress4.9漏洞关键词优化技巧
  • 南京网站设计案例seo搜索引擎优化费用
  • 采集微信公众号 做网站南京seo公司
  • 申请手机网站cms网站
  • php很简单的商城源码宁波seo外包推广排名
  • wordpress 轮播图自适应宁波如何做seo排名优化
  • 做网站大概怎么做手工
  • python爬虫爬小说来做网站国外免费网站服务器