需要使用SpringBoot框架
<!-- Feign Client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
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);
}
}
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;
}
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);
}
}