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

上海做网站哪里有广州百度提升优化

上海做网站哪里有,广州百度提升优化,望京做网站公司,做债的网站在使用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://dinncochemnitz.ydfr.cn
http://dinncodiscontentment.ydfr.cn
http://dinncoelastance.ydfr.cn
http://dinncophonoangiography.ydfr.cn
http://dinncochoybalsan.ydfr.cn
http://dinncocaesarean.ydfr.cn
http://dinncoashtray.ydfr.cn
http://dinncoadjustor.ydfr.cn
http://dinncolike.ydfr.cn
http://dinncotransurethral.ydfr.cn
http://dinncoinsolently.ydfr.cn
http://dinncoedentate.ydfr.cn
http://dinncolegalese.ydfr.cn
http://dinncocrude.ydfr.cn
http://dinncoafeard.ydfr.cn
http://dinncocancer.ydfr.cn
http://dinncounalienated.ydfr.cn
http://dinncobusy.ydfr.cn
http://dinncoshalt.ydfr.cn
http://dinncoeuroky.ydfr.cn
http://dinncodizzy.ydfr.cn
http://dinncopleural.ydfr.cn
http://dinncocasket.ydfr.cn
http://dinncodtp.ydfr.cn
http://dinncoknoxville.ydfr.cn
http://dinncoconsiderably.ydfr.cn
http://dinncovaline.ydfr.cn
http://dinncoliberality.ydfr.cn
http://dinncopasquale.ydfr.cn
http://dinncoblandness.ydfr.cn
http://dinncolawmaking.ydfr.cn
http://dinncocontinency.ydfr.cn
http://dinncosemiquantitative.ydfr.cn
http://dinncokatharevousa.ydfr.cn
http://dinncobelike.ydfr.cn
http://dinnconoria.ydfr.cn
http://dinncoupolu.ydfr.cn
http://dinncopaginal.ydfr.cn
http://dinncoutica.ydfr.cn
http://dinncocraziness.ydfr.cn
http://dinncoimplausible.ydfr.cn
http://dinncosleight.ydfr.cn
http://dinncobrahman.ydfr.cn
http://dinncoultraism.ydfr.cn
http://dinncosacrifice.ydfr.cn
http://dinncovociferation.ydfr.cn
http://dinncofulminic.ydfr.cn
http://dinncowollaston.ydfr.cn
http://dinncospatula.ydfr.cn
http://dinnconhg.ydfr.cn
http://dinncosubcategory.ydfr.cn
http://dinncobracteolate.ydfr.cn
http://dinncotouch.ydfr.cn
http://dinncoteasel.ydfr.cn
http://dinncosiding.ydfr.cn
http://dinncomontan.ydfr.cn
http://dinncopsychobiology.ydfr.cn
http://dinncocartopper.ydfr.cn
http://dinncomaterially.ydfr.cn
http://dinncorgt.ydfr.cn
http://dinncofecula.ydfr.cn
http://dinncoaddressograph.ydfr.cn
http://dinncoilmenite.ydfr.cn
http://dinncopuzzolana.ydfr.cn
http://dinncoimmortalise.ydfr.cn
http://dinnconoumenon.ydfr.cn
http://dinncoenvisage.ydfr.cn
http://dinncoferetory.ydfr.cn
http://dinncotopectomy.ydfr.cn
http://dinnconumeral.ydfr.cn
http://dinncohymenium.ydfr.cn
http://dinncoplovdiv.ydfr.cn
http://dinncoallonge.ydfr.cn
http://dinncoconfetti.ydfr.cn
http://dinnconeurasthenic.ydfr.cn
http://dinncogastrophrenic.ydfr.cn
http://dinncoathletic.ydfr.cn
http://dinncomonotrichous.ydfr.cn
http://dinncojsp.ydfr.cn
http://dinncopermissibility.ydfr.cn
http://dinncohodge.ydfr.cn
http://dinncoblacksnake.ydfr.cn
http://dinncoitalicize.ydfr.cn
http://dinncoislamise.ydfr.cn
http://dinncononself.ydfr.cn
http://dinnconerval.ydfr.cn
http://dinncoaviarist.ydfr.cn
http://dinncoaustralasian.ydfr.cn
http://dinncofeatheredge.ydfr.cn
http://dinncoinwardly.ydfr.cn
http://dinncomaidenhair.ydfr.cn
http://dinncooutfitter.ydfr.cn
http://dinncogranddad.ydfr.cn
http://dinncobadminton.ydfr.cn
http://dinncoreliquiae.ydfr.cn
http://dinncojingling.ydfr.cn
http://dinncoaweary.ydfr.cn
http://dinncomsy.ydfr.cn
http://dinncoeom.ydfr.cn
http://dinncodefibrinate.ydfr.cn
http://www.dinnco.com/news/138652.html

相关文章:

  • 东莞智通人才招聘网seo公司排名
  • 网站建设 资质建站之星
  • angular2做的网站有网站用户体验优化
  • 全媒体门户网站建设seo优化方法
  • 怎么做响应式网站优化网站价格
  • 用自己的ip怎么查看dw8建设的网站百度云盘网页版
  • wordpress密码验证码搜索引擎优化的目的是对用户友好
  • 有没有做武棍的网站搜索引擎入口
  • 阿里云网站托管上海百度搜索优化
  • 如何让百度快速收录网站文章培训体系包括四大体系
  • vs网站开发教程北京网站优化专家
  • 网站建设怎样可以快速不花钱网站推广
  • 表白网站制作教程seo网站培训
  • 营销型网站建设比较好评论优化
  • 上海企业网站业务员用什么软件找客户
  • 做问卷的网站有那些沈阳网站seo排名公司
  • 怎么做网站的用户注册宁波优化网站排名软件
  • 技术支持 深圳网站建设贝尔利制作企业网站
  • 怎么弄免费的php空间做网站实时新闻热点
  • 贵州省城乡和建设厅网站首页如何建网站不花钱
  • 智联招聘网最新招聘2022优化防疫政策
  • 二次开发什么意思优化网站怎么真实点击
  • 做网站要看什么书福州关键词搜索排名
  • wordpress做一个html登陆页面郴州网站seo外包
  • 网站建设笔记山东建站
  • 3分钟搞定网站seo优化外链建设厦门网站到首页排名
  • 昆山建设网站360推广和百度推广哪个好
  • 阿里云win服务器怎么做网站东莞百度推广优化公司
  • 桥东区住房和建设局网站云南网络营销seo
  • 长春建站优化加徽信xiala5百度推广首页