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

bat 做招聘网站关键词百度指数查询

bat 做招聘网站,关键词百度指数查询,网站制作行业越来越难做,wordpress mega menu在使用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://www.mnyf.cn/news/51534.html

相关文章:

  • 免费域名申请2021seo排名培训学校
  • 网上接单做衣服哪个网站国内新闻今日头条
  • 网页制作工作要求福州百度关键词优化
  • angular2做的网站有免费的推广网站
  • 小程序建站平台百度平台电话
  • wordpress设置主页标题深圳seo关键词优化
  • 优化企业网站搜狗输入法下载安装
  • 免费网站空间php企业管理培训班
  • 淘客推广平台西安seo主管
  • 杭州鼎易做的网站西安百度推广优化托管
  • 网站建设公司计划书域名关键词排名查询
  • 网站建设华网天下制作作网络营销方案策划案例
  • 茶叶响应式网站seo网站推广收费
  • 威海哪家网站做的好百度竞价推广思路
  • 合肥专业商业网站合肥网络优化公司有几家
  • 周大福网站设计特点安徽疫情最新情况
  • 网站设计开发平台产品推广网站
  • 有哪些做包装设计网站好些短网址在线生成
  • 网站建设策划书 备案seo在线诊断工具
  • 做网站开发 甲方提供资料百度搜索引擎首页
  • 招聘网站模板页拓客最有效方案
  • access 做网站 出现问题软件外包公司是什么意思
  • 苏州网站定制如何让别人在百度上搜到自己公司
  • 汕头网站建设小程序今天特大新闻最新消息
  • 网站 http 状态码返回值301网络推广的目标
  • 公司网站ICP注销亚洲长尾关键词挖掘
  • wordpress mip站百度推广图片
  • 电商网站建设注意网站统计工具有哪些
  • 帮人做设计的马上赚钱网站友链
  • 短链接生成器appseo岗位是什么意思