当前位置 主页 > 网站技术 > 代码类 >

    spring boot使用WebClient调用HTTP服务代码示例

    栏目:代码类 时间:2019-12-03 18:05

    这篇文章主要介绍了spring boot使用WebClient调用HTTP服务代码示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    WebClient的请求模式属于异步非阻塞,能够以少量固定的线程处理高并发的HTTP请求

    WebClient是Spring WebFlux模块提供的一个非阻塞的基于响应式编程的进行Http请求的客户端工具,从Spring5.0开始提供

    在Spring Boot应用中

    1.添加Spring WebFlux依赖

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>

    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;
      }
    }