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

专业网站推荐上海百度推广平台

专业网站推荐,上海百度推广平台,做爰视频免费安全的网站,wow做宏的网站SpringBoot 3.2引入了新的 HTTP interface 用于http接口调用,采用了类似 openfeign 的风格。 具体的代码参照 示例项目 https://github.com/qihaiyan/springcamp/tree/master/spring-http-interface 一、概述 HTTP Interface 是一个类似于 openfeign 的同步接口调…

SpringBoot 3.2引入了新的 HTTP interface 用于http接口调用,采用了类似 openfeign 的风格。

具体的代码参照 示例项目 https://github.com/qihaiyan/springcamp/tree/master/spring-http-interface

一、概述

HTTP Interface 是一个类似于 openfeign 的同步接口调用方法,采用 Java interfaces 声明远程接口调用的方法,理念上类似于SpringDataRepository,可以很大程度精简代码。

要使远程调用的接口可以执行,还需要通过 HttpServiceProxyFactory 指定底层的http接口调用库,支持 RestTemplate、WebClient、RestClient三种。

二、引入 HTTP interface

首先引入 spring-boot-starter-web 依赖。

在 build.gradle 中增加一行代码:

implementation 'org.springframework.boot:spring-boot-starter-web'

三、声明接口调用 Interface

通过声明 Interface 的方式实现远程接口调用方法:

public interface MyService {@GetExchange("/anything")String getData(@RequestHeader("MY-HEADER") String headerName);@GetExchange("/anything/{id}")String getData(@PathVariable long id);@PostExchange("/anything")String saveData(@RequestBody MyData data);@DeleteExchange("/anything/{id}")ResponseEntity<Void> deleteData(@PathVariable long id);
}

在上述代码中,我们分别声明了包括 GET/POST/DELETE 操作的四个方法,其中第一个方法演示了如何在远程接口调用时指定header参数,只需要简单的使用 RequestHeader 注解即可。

四、使用声明的方法

类似于SpringDataRepository,使用 HTTP interface 也非常简单,只需要注入对应的 Bean 即可:

public class MyController {@Autowiredprivate MyService myService;@GetMapping("/foo")public String getData() {return myService.getData("myHeader");}@GetMapping("/foo/{id}")public String getDataById(@PathVariable Long id) {return myService.getData(id);}@PostMapping("/foo")public String saveData() {return myService.saveData(new MyData(1L, "demo"));}@DeleteMapping("/foo")public ResponseEntity<Void> deleteData() {ResponseEntity<Void> resp = myService.deleteData(1L);log.info("delete {}", resp);return resp;}
}

便于演示方便,我们编写了自己的Controller。

在Controller中,我们注入声明好的 HTTP interface:

    @Autowiredprivate MyService myService;

当我们自己的接口被调用时,接口内部会通过注入的 MyService 声明的方法调用其它系统的接口。

RestClient restClient = RestClient.builder(restTemplate).baseUrl("https://httpbin.org").build();
RestClientAdapter adapter = RestClientAdapter.create(restClient);
HttpServiceProxyFactory factory = HttpServiceProxyFactory.builderFor(adapter).build();

五、实现 HTTP interface

Spring framework 通过 HttpServiceProxyFactory 来实现 HTTP interface 方法:

@Configuration
public class MyClientConfig {@Beanpublic RestTemplate restTemplate(RestTemplateBuilder builder) {return builder.build();}@Beanpublic MyService myService(RestTemplate restTemplate) {restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory("https://httpbin.org"));RestTemplateAdapter adapter = RestTemplateAdapter.create(restTemplate);HttpServiceProxyFactory factory = HttpServiceProxyFactory.builderFor(adapter).build();return factory.createClient(MyService.class);}
}

在上述配置中,我们可以看到 MyService 这个 HTTP interface 对应的 Bean 的初始化方法。

如果想使用 Spring Boot 3.2 新出的 RestClient,那初始化代码可以改为

RestClient restClient = RestClient.builder(restTemplate).baseUrl("https://httpbin.org").build();
RestClientAdapter adapter = RestClientAdapter.create(restClient);
HttpServiceProxyFactory factory = HttpServiceProxyFactory.builderFor(adapter).build();

六、单元测试

常用的单元测试方法对于 HTTP interface 仍然可用,对应的文章可以参照:springboot单元测试技术

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class DemoApplicationTest {@Autowiredprivate TestRestTemplate testRestTemplate;@Autowiredprivate RestTemplate restTemplate;private MockRestServiceServer mockRestServiceServer;@Beforepublic void before() {mockRestServiceServer = MockRestServiceServer.bindTo(restTemplate).ignoreExpectOrder(true).build();this.mockRestServiceServer.expect(ExpectedCount.manyTimes(), MockRestRequestMatchers.requestTo(Matchers.startsWithIgnoringCase("https://httpbin.org"))).andExpect(method(HttpMethod.GET)).andRespond(MockRestResponseCreators.withSuccess("{\"get\": 200}", MediaType.APPLICATION_JSON));this.mockRestServiceServer.expect(ExpectedCount.manyTimes(), MockRestRequestMatchers.requestTo(Matchers.startsWithIgnoringCase("https://httpbin.org"))).andExpect(method(HttpMethod.POST)).andRespond(MockRestResponseCreators.withSuccess("{\"post\": 200}", MediaType.APPLICATION_JSON));this.mockRestServiceServer.expect(ExpectedCount.manyTimes(), MockRestRequestMatchers.requestTo(Matchers.startsWithIgnoringCase("https://httpbin.org"))).andExpect(method(HttpMethod.DELETE)).andRespond(MockRestResponseCreators.withSuccess("{\"delete\": 200}", MediaType.APPLICATION_JSON));}@Testpublic void testRemoteCallRest() {log.info("testRemoteCallRest get {}", testRestTemplate.getForObject("/foo", String.class));log.info("testRemoteCallRest getById {}", testRestTemplate.getForObject("/foo/1", String.class));log.info("testRemoteCallRest post {}", testRestTemplate.postForObject("/foo", new MyData(1L, "demo"), String.class));testRestTemplate.exchange("/foo", HttpMethod.DELETE, HttpEntity.EMPTY, String.class);}
}
http://www.qdjiajiao.com/news/6230.html

相关文章:

  • 深圳产品网站建设百度推广区域代理
  • 外贸网站建设制作百度网站提交收录入口
  • 网站开发bug制作网页的工具软件
  • 花卉网站建设推广今日新闻最新头条
  • dedecms网站制作教程自建站平台
  • 邢台企业网站制作建设如何在百度提交自己的网站
  • 个人备案域名可以做哪些网站线上广告平台
  • 网站设计风格介绍免费b站推广网站
  • 做网站的哪个好广州seo排名收费
  • 赌博网站做代理微信群卖房卡宣传软文模板
  • 二级医院做网站百度seo服务
  • 郑州网站建设公司深圳纯手工seo
  • 网站开发站点的文件夹网站建设的好公司
  • 沈阳和平三好街做网站高级seo课程
  • cms框架 wordpress深圳搜索优化排名
  • 网站开发的一般过程网站如何做优化推广
  • 秦皇岛黄页大全秦皇岛本地信息网上海搜索排名优化公司
  • 手机做无水印短视频网站网络营销组合策略
  • 百度seo公司整站优化如何在百度发布信息
  • 动态网站开发实训总结html家乡网站设计
  • 网站建设公司怎样选游戏推广引流软件
  • 昆明网站推广公司关键词排名
  • 做网站哪个语言快2023第二波疫情已经到来
  • 庐江网站制作公司百度惠生活怎么做推广
  • 温州市企业网站制作网络营销策划书ppt
  • 网站关联词搜索怎么做原创代写文章平台
  • 做外国的网站卖东西seo专业培训费用
  • 建设工程官方网站电商广告网络推广
  • 雍熙网站建设谷歌广告上海有限公司官网
  • 天眼查网站建设公司aso排名服务公司