• 接口调用三种方式


    创建服务端

    一个controller,一个启动类,配置端口

    • controller
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class ServerController {
    
        @RequestMapping("/server/test1")
        public String getStr(String a){
            System.out.println("请求到server端");
            return a;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    原始httpclient请求

    参考链接:httpClient

    只需要一个controller测试发送请求

    • controller
    import org.apache.http.HttpEntity;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.io.IOException;
    import java.nio.charset.StandardCharsets;
    
    @RestController
    public class HttpController {
    
        @RequestMapping("/http/test1")
        public String test1(String a) throws IOException {
        	//服务端的地址
            String urlPath="http://192.168.3.32:7071/server/test1?a="+a;
            //CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
            /*
                HttpClients是一个工具类,等同上面的方法
                可关闭的httpclient客户端,相当于打开的一个浏览器
             */
            CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
    
            //httpGet
            HttpGet httpGet=new HttpGet(urlPath);
    
            //可关闭的响应:DecopressingEntity
            CloseableHttpResponse response=null;
            try {
                //发送请求
                response=closeableHttpClient.execute(httpGet);
                //获取响应结果:HttpEntity不仅可以作为结果,也可以作为请求的参数实体,有很多的实现
                HttpEntity entity = response.getEntity();
                //对HttpEntity操作的工具类,转成string看下结果
                String toStringResult = EntityUtils.toString(entity, StandardCharsets.UTF_8);
                System.out.println(toStringResult);
                return toStringResult;
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                if (closeableHttpClient!=null){
                    closeableHttpClient.close();
                }
                if(response!=null){
                    response.close();
                }
            }
            return "null";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    在这里插入图片描述

    RestTemplate调用接口

    import org.springframework.http.HttpEntity;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.MediaType;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    @RestController
    public class RestTemplateController {
    
        @RequestMapping("/rest/test1")
        public String test1(String a){
    
            String urlPath="http://192.168.3.32:7071/server/test1?a="+a;
    
            // 创建 RestTemplate 实例
            RestTemplate restTemplate = new RestTemplate();
    
    
            // 设置请求头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
    
            HttpEntity<String> httpEntity = new HttpEntity<String>(null, headers);
            ResponseEntity<String> forEntity = restTemplate.postForEntity(urlPath,httpEntity,String.class);
            return forEntity.getBody();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    在这里插入图片描述

    feign调用接口

    需要一个service接口使用注解配置服务端的相关信息,还需要在启动类使用注解@EnableFeignClients

    引入pom

    • pom
      要用openfeign,而不是feign,feign早就废弃了
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
        </dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • service
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.stereotype.Service;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    //服务端请求接口的地址为192.168.3.32:7071/server/test1
    @Service
    //url为要请求接口的ip和端口,name随便
    @FeignClient(url = "192.168.3.32:7071",name = "client")
    public interface ClientService {
    
    	//请求的接口名称,对应服务端的controller
        @RequestMapping("/server/test1")
        @ResponseBody
        //需要绑定参数,不然可能收到的值为null
        public String getStr(@RequestParam("a")String a);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • controller
    import com.wzw.service.ClientService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class ClientController {
    
        @Autowired
        private ClientService service;
    
        @RequestMapping("/client/getStr")
        public String getStr(String a){
            System.out.println(a);
            return service.getStr(a);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述

  • 相关阅读:
    【Java】【PAT】Basic Level 1022 D进制的A+B
    GitHub爬虫项目详解
    数据分析 | Pandas 200道练习题 进阶篇(2)
    微软确认:测试版用户可在Win11上运行Android应用
    使用 OpenCV 和 Tesseract OCR 进行车牌识别
    关于Http和Https
    Linux笔记--硬链接与软链接
    个人设计web前端大作业 基于html5制作美食菜谱网页设计作业代码
    如何用H5实现好玩的2048小游戏
    中项2022
  • 原文地址:https://blog.csdn.net/a3562323/article/details/132897879