第五节 第三方接口访问:使用Feign发送HTTP请求

亮子 2023-05-17 08:22:03 9974 0 0 0

1、添加依赖

需要使用SpringBoot框架

        <!-- Feign Client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

2、在启动类添加注解@EnableFeignClients

package com.shenma2009;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class ServerShopUserApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServerShopUserApplication.class, args);
    }

}

3、创建service接口

  • 接口参数类
package com.shenma2009.vo;

import lombok.Data;

/**
 * @author 军哥
 * @version 1.0
 * @description: CommParamVo
 * @date 2023/5/17 15:40
 */

@Data
public class CommParamVo {
    private Integer userId;
    private String message;
}
  • service代码
package com.shenma2009.service;

import com.shenma2009.domain.ResultResponse;
import com.shenma2009.vo.CommParamVo;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

/**
 * @author 军哥
 * @version 1.0
 * @description: ITestApiService
 * @date 2023/5/17 14:58
 */

@FeignClient(url = "https://shenmazong.com",name = "test")
public interface ITestApiService {

    @RequestMapping(value = "/test/getMd5",method = RequestMethod.GET)
    public ResultResponse getMd5(@RequestParam("message") String message);

    @RequestMapping(value = "/test/sendEmail",method = RequestMethod.POST)
    public ResultResponse sendEmail(@RequestBody CommParamVo commParamVo);

    @GetMapping(value = "/test/sendSms/{userId}/{message}")
    public ResultResponse sendSms(@PathVariable("userId") Integer userId, @PathVariable("message") String message);

    @RequestMapping(value = "/test/login",method = RequestMethod.POST)
    public ResultResponse login(@RequestParam("username") String userName, @RequestParam("userpass") String userPass);

    @RequestMapping(value = "/test/getRandCode",method = RequestMethod.GET)
    public ResultResponse getRandCode();
}
  • 测试类
package com.shenma2009;

import com.shenma2009.domain.ResultResponse;
import com.shenma2009.service.ITestApiService;
import com.shenma2009.vo.CommParamVo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * @author 军哥
 * @version 1.0
 * @description: TestOpenFeignApp
 * @date 2023/5/17 15:48
 */

@SpringBootTest
public class TestOpenFeignApp {
    @Autowired
    ITestApiService iTestApiService;

    @Test
    public void test1() {
        ResultResponse response = iTestApiService.getMd5("123456");
        System.out.println(response);
    }

    @Test
    public void test2() {
        ResultResponse response = iTestApiService.getRandCode();
        System.out.println(response);
    }

    @Test
    public void test3() {
        ResultResponse response = iTestApiService.sendSms(666, "hello,world");
        System.out.println(response);
    }

    @Test
    public void test4() {
        ResultResponse response = iTestApiService.login("andy", "123456");
        System.out.println(response);
    }

    @Test
    public void test5() {
        CommParamVo commParamVo = new CommParamVo();
        commParamVo.setUserId(666);
        commParamVo.setMessage("hello,world");
        ResultResponse response = iTestApiService.sendEmail(commParamVo);
        System.out.println(response);
    }
}