6 Möglichkeiten, HTTP-Anfragen mit Java basierend auf Spring Boot aufzurufen

Szenario: 6 Möglichkeiten zum Aufrufen von HTTP-Anfragen mit Java basierend auf Spring Boot. Der Server stellt eine POST-Anfrage und zwei GET-Anfragen aus. Die auf die 6 Arten implementierten Clients rufen alle die 3 vom Server freigegebenen Methoden auf. Kunden, die sechs Arten von HTTP-Anfragen intuitiv fühlen und vergleichen können.

Ausführung:

Spring Boot 2.6.3
Spring Framework 5.3.15
Spring Cloud 2021.0.1

1. Fallszenario

Dieses Beispiel implementiert sechs Möglichkeiten für den Client, drei Methoden desselben Servers aufzurufen.

1. Server

Geben Sie serverseitig eine POST-Anfrage und zwei GET-Anfragen aus.

1.1 Schnittstelleninformationen

(1) POST-Anfrage

[访问URL]: http://127.0.0.1:19091/server/comm/f1
[请求方式]: POST
[请求参数]: JSON
{
    
    "userName":"HangZhou20220719","tradeName":"Vue进阶教程"}
[返回值]: JSON
{
    
    code=200, message=成功}

(2) GET-Anfrage (1)

[访问URL]: http://127.0.0.1:19091/server/comm/f2
[请求方式]: GET
[请求参数]: String
obj=HangZhou20220719
[返回值]: JSON
{
    
    code=200, message=成功}

(3) GET-Anfrage (2)

[访问URL]: http://127.0.0.1:19091/server/comm/f3/{
    
    obj}
[请求方式]: GET
[请求参数]: String
obj=HangZhou20220719
[返回值]: JSON
{
    
    code=200, message=成功}
1.2 Servercode

3 Schnittstellencodes auf der Serverseite.

@Slf4j
@RestController
@RequestMapping("/comm")
public class CommonController {
    
    
    /**
     * 1.发布POST请求
     * 入参注解: @RequestBody
     * 返回注解: @ResponseBody(@RestController包含此注解)
     */
    @PostMapping("/f1")
    public Object f1(@RequestBody Object obj) {
    
    
        log.info("CommonController->f1,接收参数,obj = " + obj.toString());
        log.info("CommonController->f1,处理业务.");
        log.info("CommonController->f1,返回.");
        return ResultObj.builder().code("200").message("成功").build();
    }

    /**
     * 2.发布GET请求
     * 入参注解: @RequestParam
     * 返回注解: @ResponseBody(@RestController包含此注解)
     */
    @GetMapping("/f2")
    public Object f2(@RequestParam("obj") String obj) {
    
    
        log.info("CommonController->f2,接收参数,obj = " + obj.toString());
        log.info("CommonController->f2,处理业务.");
        log.info("CommonController->f2,返回.");
        return ResultObj.builder().code("200").message("成功").build();
    }

    /**
     * 3.发布GET请求
     * 入参注解: @PathVariable
     * 返回注解: @ResponseBody(@RestController包含此注解)
     */
    @GetMapping("/f3/{obj}")
    public Object f3(@PathVariable("obj") String obj) {
    
    
        log.info("CommonController->f3,接收参数,obj = " + obj.toString());
        log.info("CommonController->f3,处理业务.");
        log.info("CommonController->f3,返回.");
        return ResultObj.builder().code("200").message("成功").build();
    }
}
1.3 Serverhilfsobjekte

Serverhilfsobjekt.

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ResultObj {
    
    
    private String code;
    private String message;
}

Zwei, sechs Möglichkeiten für Java, http-Anfragen aufzurufen

1. Verwenden Sie HttpURLConnection, um die HTTP-Anfrage aufzurufen

(1) Speicherort des Jar-Pakets

HttpURLConnection, vollständiger Name: java.net.HttpURLConnection.

Klassen im java.net-Paket im rt.jar-Paket, das mit JDK 1.8 geliefert wird.

(2) Kundencode

public class Utils01JdkClient {
    
    
    public static void main(String[] args) throws Exception {
    
    
        f1();
        f2();
        f3();
    }

    /**
     * 1.使用HttpURLConnection调用服务端的POST请求
     * 服务端入参注解: @RequestBody
     */
    public static void f1() throws Exception {
    
    
        // 1.请求URL
        String postUrl = "http://127.0.0.1:19091/server/comm/f1";
        // 2.请求参数JSON格式
        Map<String, String> map = new HashMap<>();
        map.put("userName", "HangZhou20220718");
        map.put("tradeName", "Vue进阶教程");
        String json = JSON.toJSONString(map);
        // 3.创建连接与设置连接参数
        URL urlObj = new URL(postUrl);
        HttpURLConnection httpConn = (HttpURLConnection) urlObj.openConnection();
        httpConn.setRequestMethod("POST");
        httpConn.setRequestProperty("Charset", "UTF-8");
        // POST请求且JSON数据,必须设置
        httpConn.setRequestProperty("Content-Type", "application/json");
        // 打开输出流,默认是false
        httpConn.setDoOutput(true);
        // 打开输入流,默认是true,可省略
        httpConn.setDoInput(true);
        // 4.从HttpURLConnection获取输出流和写数据
        OutputStream oStream = httpConn.getOutputStream();
        oStream.write(json.getBytes());
        oStream.flush();
        // 5.发起http调用(getInputStream触发http请求)
        if (httpConn.getResponseCode() != 200) {
    
    
            throw new Exception("调用服务端异常.");
        }
        // 6.从HttpURLConnection获取输入流和读数据
        BufferedReader br = new BufferedReader(
                new InputStreamReader(httpConn.getInputStream()));
        String resultData = br.readLine();
        System.out.println("从服务端返回结果: " + resultData);
        // 7.关闭HttpURLConnection连接
        httpConn.disconnect();
    }

    /**
     * 2.使用HttpURLConnection调用服务端的GET请求
     * 服务端入参注解: @RequestParam
     */
    public static void f2() throws Exception {
    
    
        // 1.请求URL与组装请求参数
        String getUrl = "http://127.0.0.1:19091/server/comm/f2";
        String obj = "Vue进阶教程";
        String para = "?obj=" + URLEncoder.encode(obj, "UTF-8");
        getUrl = getUrl + para;
        // 2.创建连接与设置连接参数
        URL urlObj = new URL(getUrl);
        HttpURLConnection httpConn = (HttpURLConnection) urlObj.openConnection();
        httpConn.setRequestMethod("GET");
        httpConn.setRequestProperty("Charset", "UTF-8");
        // 3.发起http调用(getInputStream触发http请求)
        if (httpConn.getResponseCode() != 200) {
    
    
            throw new Exception("调用服务端异常.");
        }
        // 4.从HttpURLConnection获取输入流和读数据
        BufferedReader br = new BufferedReader(
                new InputStreamReader(httpConn.getInputStream()));
        String resultData = br.readLine();
        System.out.println("从服务端返回结果: " + resultData);
        // 5.关闭HttpURLConnection连接
        httpConn.disconnect();
    }

    /**
     * 3.使用HttpURLConnection调用服务端的GET请求
     * 服务端入参注解: @PathVariable
     */
    public static void f3() throws Exception {
    
    
        // 1.请求URL与组装请求参数
        String getUrl = "http://127.0.0.1:19091/server/comm/f3/";
        String obj = "Vue进阶教程";
        obj = URLEncoder.encode(obj, "UTF-8");
        getUrl = getUrl + obj;
        URL urlObj = new URL(getUrl);
        // 2.创建连接与设置连接参数
        HttpURLConnection httpConn = (HttpURLConnection) urlObj.openConnection();
        httpConn.setRequestMethod("GET");
        httpConn.setRequestProperty("charset", "UTF-8");
        // 3.发起http调用(getInputStream触发http请求)
        if (httpConn.getResponseCode() != 200) {
    
    
            throw new Exception("调用服务端异常.");
        }
        // 4.从HttpURLConnection获取输入流和读数据
        BufferedReader br = new BufferedReader(
                new InputStreamReader(httpConn.getInputStream()));
        String resultData = br.readLine();
        System.out.println("从服务端返回结果: " + resultData);
        // 5.关闭HttpURLConnection连接
        httpConn.disconnect();
    }
}

2. Verwenden Sie commons-httpclient, um die HTTP-Anfrage aufzurufen

(1) Speicherort des Jar-Pakets

commons-httpclient, ein früheres Jar-Paket, im MVNRepository-Warehouse, der letzte Wartungszeitpunkt ist: August 2007.

<dependency>
  <groupId>commons-httpclient</groupId>
  <artifactId>commons-httpclient</artifactId>
  <version>3.1</version>
</dependency>

(2) Kundencode

public class Utils02CommonsHttpClient {
    
    

    public static void main(String[] args) throws Exception {
    
    
        f1();
        f2();
        f3();
    }

    /**
     * 1.使用commons-httpclient调用服务端的POST请求
     * 服务端入参注解: @RequestBody
     */
    public static void f1() throws Exception {
    
    
        // 1.请求URL
        String postUrl = "http://127.0.0.1:19091/server/comm/f1";
        // 2.请求参数
        Map<String, String> map = new HashMap<>();
        map.put("userName", "HangZhou20220718");
        map.put("tradeName", "Vue进阶教程");
        String json = JSON.toJSONString(map);
        // 3.创建连接与设置连接参数
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod(postUrl);
        postMethod.addRequestHeader("Content-Type", "application/json");
        RequestEntity entity = new StringRequestEntity(json, "application/json", "UTF-8");
        postMethod.setRequestEntity(entity);
        //解决返回值中文乱码
        postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
        String resultData = "";
        // 4.发起请求
        int code = httpClient.executeMethod(postMethod);
        if (code != 200) {
    
    
            throw new Exception("调用服务端异常.");
        }
        // 5.接收返回值
        resultData = postMethod.getResponseBodyAsString();
        System.out.println("从服务端返回结果: " + resultData);
        // 6.关闭连接
        postMethod.releaseConnection();
    }

    /**
     * 2.使用commons-httpclient调用服务端的GET请求
     * 服务端入参注解: @RequestParam
     */
    public static void f2() throws Exception {
    
    
        // 1.请求URL与组装请求参数
        String getUrl = "http://127.0.0.1:19091/server/comm/f2";
        String obj = "Vue进阶教程";
        //入参有中文需要编码
        String para = "?obj=" + URLEncoder.encode(obj, "UTF-8");
        getUrl = getUrl + para;
        // 2.创建连接与设置连接参数
        HttpClient httpClient = new HttpClient();
        GetMethod getMethod = new GetMethod(getUrl);
        //解决返回值中文乱码
        getMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
        // 3.发起请求
        int code = httpClient.executeMethod(getMethod);
        String resultData = "";
        if (code != 200) {
    
    
            throw new Exception("调用服务端异常.");
        }
        // 4.接收返回值
        resultData = getMethod.getResponseBodyAsString();
        System.out.println("从服务端返回结果: " + resultData);
        // 5.关闭连接
        getMethod.releaseConnection();
    }

    /**
     * 3.使用commons-httpclient调用服务端的GET请求
     * 服务端入参注解: @PathVariable
     */
    public static void f3() throws Exception {
    
    
        // 1.请求URL与组装请求参数
        String getUrl = "http://127.0.0.1:19091/server/comm/f3/";
        String obj = "Vue进阶教程";
        //入参有中文需要编码
        obj = URLEncoder.encode(obj, "UTF-8");
        getUrl = getUrl + obj;
        // 2.创建连接与设置连接参数
        HttpClient httpClient = new HttpClient();
        GetMethod getMethod = new GetMethod(getUrl);
        //解决返回值中文乱码
        getMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
        // 3.发起请求
        int code = httpClient.executeMethod(getMethod);
        String resultData = "";
        if (code != 200) {
    
    
            throw new Exception("调用服务端异常.");
        }
        // 4.接收返回值
        resultData = getMethod.getResponseBodyAsString();
        System.out.println("从服务端返回结果: " + resultData);
        // 5.关闭连接
        getMethod.releaseConnection();
    }
}

3. Verwenden Sie org.apache.httpcomponents, um die HTTP-Anfrage aufzurufen

(1) Speicherort des Jar-Pakets

httpcomponents, im MVNRepository-Warehouse, der letzte angezeigte Wartungszeitpunkt ist: Oktober 2020.

<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.5.13</version>
</dependency>

(2) Kundencode

public class Utils03HttpComponentsClient {
    
    
    public static void main(String[] args) throws Exception {
    
    
        f1();
        f2();
        f3();
    }

    /**
     * 1.使用org.apache.httpcomponents调用服务端的POST请求
     * 服务端入参注解: @RequestBody
     */
    public static void f1() throws Exception {
    
    
        // 1.请求URL
        String postUrl = "http://127.0.0.1:19091/server/comm/f1";
        // 2.请求参数
        Map<String, String> map = new HashMap<>();
        map.put("userName", "HangZhou20220718");
        map.put("tradeName", "Vue进阶教程");
        String json = JSON.toJSONString(map);
        // 3.创建连接与设置连接参数
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(postUrl);
        StringEntity entity = new StringEntity(json);
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        // 4.发起请求与接收返回值
        HttpResponse response = httpClient.execute(httpPost);
        if (response.getStatusLine().getStatusCode() != 200) {
    
    
            throw new Exception("调用服务端异常.");
        }
        HttpEntity res = response.getEntity();
        String resultData = EntityUtils.toString(res);
        System.out.println("从服务端返回结果: " + resultData);
        // 5.关闭连接
        httpClient.close();
    }

    /**
     * 2.使用org.apache.httpcomponents调用服务端的GET请求
     * 服务端入参注解: @RequestParam
     */
    public static void f2() throws Exception {
    
    
        // 1.请求URL与组装请求参数
        String getUrl = "http://127.0.0.1:19091/server/comm/f2";
        String obj = "Vue进阶教程";
        String para = "?obj=" + URLEncoder.encode(obj, "UTF-8");
        getUrl = getUrl + para;
        // 2.创建连接与设置连接参数
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet(getUrl);
        // 3.发起请求与接收返回值
        HttpResponse response = httpClient.execute(httpGet);
        if (response.getStatusLine().getStatusCode() != 200) {
    
    
            throw new Exception("调用服务端异常.");
        }
        HttpEntity res = response.getEntity();
        String resultData = EntityUtils.toString(res);
        System.out.println("从服务端返回结果: " + resultData);
        // 4.关闭连接
        httpClient.close();
    }

    /**
     * 3.使用org.apache.httpcomponents调用服务端的GET请求
     * 服务端入参注解: @PathVariable
     */
    public static void f3() throws Exception {
    
    
        // 1.请求URL与组装请求参数
        String getUrl = "http://127.0.0.1:19091/server/comm/f3/";
        String obj = "Vue进阶教程";
        //入参有中文需要编码
        obj = URLEncoder.encode(obj, "UTF-8");
        getUrl = getUrl + obj;
        // 2.创建连接与设置连接参数
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet(getUrl);
        // 3.发起请求与接收返回值
        HttpResponse response = httpClient.execute(httpGet);
        if (response.getStatusLine().getStatusCode() != 200) {
    
    
            throw new Exception("调用服务端异常.");
        }
        HttpEntity res = response.getEntity();
        String resultData = EntityUtils.toString(res);
        System.out.println("从服务端返回结果: " + resultData);
        // 4.关闭连接
        httpClient.close();
    }
}

4. Verwenden Sie OkHttp, um die HTTP-Anfrage aufzurufen

(1) Speicherort des Jar-Pakets

com.squareup.okhttp3, die in diesem Beispiel verwendete Version.

<dependency>
  <groupId>com.squareup.okhttp3</groupId>
  <artifactId>okhttp</artifactId>
  <version>4.10.0</version>
  <exclusions>
    <exclusion>
      <groupId>com.google.android</groupId>
      <artifactId>android</artifactId>
    </exclusion>
  </exclusions>
</dependency>

(2) Kundencode

public class Utils04OkHttpClient {
    
    
    public static void main(String[] args) throws Exception {
    
    
        f1();
        f2();
        f3();
    }

    /**
     * 1.使用okhttp调用服务端的POST请求
     * 服务端入参注解: @RequestBody
     */
    public static void f1() throws Exception {
    
    
        // 1.请求URL
        String postUrl = "http://127.0.0.1:19091/server/comm/f1";
        // 2.请求参数
        Map<String, String> map = new HashMap<>();
        map.put("userName", "HangZhou20220718");
        map.put("tradeName", "Vue进阶教程");
        String json = JSON.toJSONString(map);
        // 3.创建连接与设置连接参数
        MediaType mediaType = MediaType.parse("application/json; charset=UTF-8");
        RequestBody requestBody = RequestBody.Companion.create(json, mediaType);
        Request request = new Request.Builder().url(postUrl).post(requestBody).build();
        OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
        // 4.发起请求与接收返回值
        Response response = okHttpClient.newCall(request).execute();
        String resultData = response.body().string();
        System.out.println("从服务端返回结果: " + resultData);
    }

    /**
     * 2.使用okhttp调用服务端的GET请求
     * 服务端入参注解: @RequestParam
     */
    public static void f2() throws Exception {
    
    
        // 1.请求URL与组装请求参数
        String getUrl = "http://127.0.0.1:19091/server/comm/f2";
        String obj = "Vue进阶教程";
        String para = "?obj=" + URLEncoder.encode(obj, "UTF-8");
        getUrl = getUrl + para;
        // 2.创建连接与设置连接参数
        Request request = new Request.Builder().url(getUrl).build();
        OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
        // 3.发起请求与接收返回值
        Response response = okHttpClient.newCall(request).execute();
        String resultData = response.body().string();
        System.out.println("从服务端返回结果: " + resultData);
    }

    /**
     * 3.使用okhttp调用服务端的GET请求
     * 服务端入参注解: @PathVariable
     */
    public static void f3() throws Exception {
    
    
        // 1.请求URL与组装请求参数
        String getUrl = "http://127.0.0.1:19091/server/comm/f3/";
        String obj = "Vue进阶教程";
        obj = URLEncoder.encode(obj, "UTF-8");
        getUrl = getUrl + obj;
        // 2.创建连接与设置连接参数
        Request request = new Request.Builder().url(getUrl).build();
        OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
        // 3.发起请求与接收返回值
        Response response = okHttpClient.newCall(request).execute();
        String resultData = response.body().string();
        System.out.println("从服务端返回结果: " + resultData);
    }
}

5. Verwenden Sie RestTemplate, um die HTTP-Anfrage aufzurufen

(1) Speicherort des Jar-Pakets

RestTemplate, siehe org.springframework.web.client.RestTemplate.

In diesem Beispiel wird die Version verwendet.

dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>5.3.15</version>
  <scope>compile</scope>
</dependency>

(2) Kundencode

public class Utils05RestTemplateClient {
    
    
    public static void main(String[] args) throws Exception {
    
    
        f1();
        f2();
        f3();
    }

    /**
     * 1.使用RestTemplate调用服务端的POST请求
     * 服务端入参注解: @RequestBody
     */
    public static void f1() throws Exception {
    
    
        // 1.请求URL
        String postUrl = "http://127.0.0.1:19091/server/comm/f1";
        // 2.请求参数JSON格式
        Map<String, String> map = new HashMap<>();
        map.put("userName", "HangZhou20220718");
        map.put("tradeName", "Vue进阶教程");
        String json = JSON.toJSONString(map);
        // 3.创建RestTemplate
        RestTemplate restTemplate = new RestTemplate();
        // 4.设置RestTemplate参数(请求头和body)
        HttpHeaders headers = new HttpHeaders();
        MediaType mediaType = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(mediaType);
        headers.add("Accept", "application/json");
        HttpEntity<String> entity = new HttpEntity<>(json, headers);
        // 5.使用RestTemplate发起请求与接收返回值
        String resultData = restTemplate.postForObject(postUrl, entity, String.class);
        System.out.println("从服务端返回结果: " + resultData);
    }

    /**
     * 2.使用RestTemplate调用服务端的GET请求
     * 服务端入参注解: @RequestParam
     */
    public static void f2() throws Exception {
    
    
        // 1.请求URL与组装请求参数
        String getUrl = "http://127.0.0.1:19091/server/comm/f2";
        String obj = "Vue进阶教程";
        String para = "?obj=" + obj;
        getUrl = getUrl + para;
        // 2.创建RestTemplate
        RestTemplate restTemplate = new RestTemplate();
        // 3.使用RestTemplate发起请求与接收返回值
        String resultData = restTemplate.getForObject(getUrl, String.class);
        System.out.println("从服务端返回结果: " + resultData);
    }

    /**
     * 3.使用RestTemplate调用服务端的GET请求
     * 服务端入参注解: @PathVariable
     */
    public static void f3() throws Exception {
    
    
        // 1.请求URL与组装请求参数
        String getUrl = "http://127.0.0.1:19091/server/comm/f3/";
        String obj = "Vue进阶教程";
        getUrl = getUrl + obj;
        // 2.创建RestTemplate
        RestTemplate restTemplate = new RestTemplate();
        // 3.使用RestTemplate发起请求与接收返回值
        String resultData = restTemplate.getForObject(getUrl, String.class);
        System.out.println("从服务端返回结果: " + resultData);
    }
}

6. Verwenden Sie OpenFeign, um eine HTTP-Anfrage aufzurufen

(1) Speicherort des Jar-Pakets

Spring Cloud OpenFeign ist ein Mitglied der Bucket-Komponente der Spring Cloud-Familie.

Diese Beispielversion: Spring Cloud 2021.0.1; spring-cloud-openfeign 3.1.1

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

(2) Client-Code (Feign-Schnittstelle)

@FeignClient(contextId = "utils06OpenFeign",
        value = "example-server")
public interface Utils06OpenFeignClient {
    
    
    /**
     * 1.使用openfeign调用服务端的POST请求
     * 服务端入参注解: @RequestBody
     */
    @ResponseBody
    @PostMapping("/server/comm/f1")
    Object f1(@RequestBody Object obj);

    /**
     * 2.使用openfeign调用服务端的GET请求
     * 服务端入参注解: @RequestParam
     */
    @ResponseBody
    @GetMapping("/server/comm/f2")
    Object f2(@RequestParam("obj") String obj);

    /**
     * 3.使用openfeign调用服务端的GET请求
     * 服务端入参注解: @PathVariable
     */
    @ResponseBody
    @GetMapping("/server/comm/f3/{obj}")
    Object f3(@PathVariable("obj") String obj);
}

(3) Client-Code (Feign-Schnittstelle aufrufen)

/**
 * 触发请求:
 * http://127.0.0.1:19092/client/exam/f
 */
@Slf4j
@RestController
@RequestMapping("/exam")
public class ExampleController {
    
    
    /**
     * 1.注入Feign接口
     */
    @Autowired
    private Utils06OpenFeignClient feignClient;

    /**
     * 2.调用Feign接口
     */
    @GetMapping("/f")
    public void f() throws Exception {
    
    
        log.info("使用Feign调用服务端f1: ");
        Map<String, String> map = new HashMap<>();
        map.put("userName", "HangZhou20220718");
        map.put("tradeName", "Vue进阶教程");
        Object resultDataF1 = feignClient.f1(map);
        log.info("使用Feign调用服务端f1,返回结果: " + resultDataF1);

        log.info("使用Feign调用服务端f2: ");
        String obj = "Vue进阶教程";
        Object resultDataF2 = feignClient.f2(obj);
        log.info("使用Feign调用服务端f2,返回结果: " + resultDataF2);

        log.info("使用Feign调用服务端f3: ");
        String obj2 = "Vue进阶教程";
        Object resultDataF3 = feignClient.f3(obj2);
        log.info("使用Feign调用服务端f3,返回结果: " + resultDataF3);
    }
}

(4) Dieses Beispiel verwendet die Basis

Spring Cloud OpenFeign ist eine Spring Cloud-Komponente und die erstellten Microservices basieren auf der Spring Cloud-Architektur. In diesem Beispiel zwei Microservices:

Server: Beispielserver

Kunde: example-feign-client

Sowohl der Server als auch der Client verwenden Nacos als Dienstregistrierungs- und Erkennungszentrum. Spring Cloud OpenFeign ist in den Beispiel-Feign-Client des Clients integriert, und der Server muss nicht geändert werden. Fügen Sie die Annotation @EnableFeignClients zur Startup-Klasse hinzu und fügen Sie die Annotation @FeignClient mithilfe von OpenFeign zur Schnittstelle hinzu.

おすすめ

転載: blog.csdn.net/qq_37131111/article/details/126018175