怎么在springboot中利用WebClient对HTTP服务进行调用
怎么在spring boot中利用WebClient对HTTP服务进行调用?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
创新互联建站专业为企业提供资阳网站建设、资阳做网站、资阳网站设计、资阳网站制作等企业网站建设、网页设计与制作、资阳企业网站模板建站服务,10多年资阳做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
WebClient的请求模式属于异步非阻塞,能够以少量固定的线程处理高并发的HTTP请求
WebClient是Spring WebFlux模块提供的一个非阻塞的基于响应式编程的进行Http请求的客户端工具,从Spring5.0开始提供
在Spring Boot应用中
1.添加Spring WebFlux依赖
org.springframework.boot spring-boot-starter-webflux
2.使用
(1)Restful接口demoController.java
package com.example.demo.controller; import com.example.demo.domain.MyData; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.*; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; @RestController @RequestMapping("/api") public class demoController { @GetMapping(value = "/getHeader", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}) public MyData getHeader(HttpServletRequest request) { int id = 0; if (request.getParameter("id") != null) { id = Integer.valueOf(request.getParameter("id")); } String name = request.getParameter("name"); //header String userAgent = "USER_AGENT——" + request.getHeader(HttpHeaders.USER_AGENT); userAgent += " | ACCEPT_CHARSET——" + request.getHeader(HttpHeaders.ACCEPT_CHARSET); userAgent += " | ACCEPT_ENCODING——" + request.getHeader(HttpHeaders.ACCEPT_ENCODING); userAgent += " | ContextPath——" + request.getContextPath(); userAgent += " | AuthType——" + request.getAuthType(); userAgent += " | PathInfo——" + request.getPathInfo(); userAgent += " | Method——" + request.getMethod(); userAgent += " | QueryString——" + request.getQueryString(); Cookie[] cookies = request.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { System.out.println(cookie.getName() + ":" + cookie.getValue()); } } MyData data = new MyData(); data.setId(id); data.setName(name); data.setOther(userAgent); return data; } @PostMapping(value = "/getPost", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}) public MyData getPost(HttpServletRequest request) { int id = 0; if (request.getParameter("id") != null) { id = Integer.valueOf(request.getParameter("id")); } String name = request.getParameter("name"); System.out.println(name + "," + id); MyData data = new MyData(); data.setId(id); data.setName(name); return data; } /** * POST传JSON请求 */ @PostMapping(value = "/getPostJson", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}) public MyData getPostJson(@RequestBody(required = true) MyData data) { System.out.println(data.getId()); System.out.println(data.getName()); return data; } }
MyData.java
package com.example.demo.domain; public class MyData { private int id; private String name; private String other; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getOther() { return other; } public void setOther(String other) { this.other = other; } }
(2)WebClient使用
DemoApplicationTests.java
package com.example.demo; import com.example.demo.domain.MyData; import org.junit.Test; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Mono; import java.time.Duration; import java.time.temporal.ChronoUnit; public class DemoApplicationTests { private WebClient webClient = WebClient.builder() .baseUrl("http://127.0.0.1:8080") .defaultHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)") .defaultCookie("ACCESS_TOKEN", "test_token").build(); @Test public void WebGetDemo() { try { Monoresp = webClient.method(HttpMethod.GET).uri("api/getHeader?id={id}&name={name}", 123, "abc") .retrieve() .bodyToMono(MyData.class); MyData data = resp.block(); System.out.println("WebGetDemo result----- " + data.getString()); } catch (Exception e) { e.printStackTrace(); } } @Test public void WebPostDemo() { MultiValueMap formData = new LinkedMultiValueMap<>(2); formData.add("id", "456"); formData.add("name", "xyz"); Mono response = webClient.method(HttpMethod.POST).uri("/api/getPost") .contentType(MediaType.APPLICATION_FORM_URLENCODED) .body(BodyInserters.fromFormData(formData)) .retrieve() .bodyToMono(MyData.class).timeout(Duration.of(10, ChronoUnit.SECONDS)); System.out.println(response); MyData data = response.block(); System.out.println("WebPostDemo result----- " + data.getString()); } @Test public void WebPostJson() { MyData requestData = new MyData(); requestData.setId(789); requestData.setName("lmn"); Mono response = webClient.post().uri("/api/getPostJson") .contentType(MediaType.APPLICATION_JSON_UTF8) .syncBody(requestData) .retrieve() .bodyToMono(MyData.class).timeout(Duration.of(10, ChronoUnit.SECONDS)); MyData data = response.block(); System.out.println("WebPostJson result----- " + data.getString()); } }
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。
文章名称:怎么在springboot中利用WebClient对HTTP服务进行调用
文章路径:http://azwzsj.com/article/joejih.html