1.在pom.xml引入
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.openfeign/feign-okhttp -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
<version>11.0</version>
</dependency>
2.在启动类上开启配置
3.OpenFeign 发送http请求
package com.xxmfl.xiaoyi.mp.service;
import com.xxmfl.bms.common.api.CommonResult;
import com.xxmfl.bms.model.UomgUser;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(url = "http://51xy.xyz:30003/api", name = "api")
public interface FeignService {
@RequestMapping(value = "/pay/demo2", method = RequestMethod.GET,
produces = {MediaType.APPLICATION_JSON_VALUE})
CommonResult demo(@RequestParam(value = "username") String username);
@RequestMapping(value = "/pay/demo4", method = RequestMethod.POST,
produces = {MediaType.APPLICATION_JSON_VALUE})
CommonResult demo2(@RequestBody UomgUser uomguser);
}
package com.xxmfl.xiaoyi.mp.controller.api;
import cn.hutool.core.date.DateTime;
import com.xxmfl.bms.common.api.CommonResult;
import com.xxmfl.bms.model.TBillRecord;
import com.xxmfl.bms.model.TGoodsDefine;
import com.xxmfl.bms.model.UomgUser;
import com.xxmfl.xiaoyi.mp.service.FeignService;
import com.xxmfl.xiaoyi.mp.service.TBillRecordService;
import com.xxmfl.xiaoyi.mp.service.TGoodsDefineService;
import com.xxmfl.xiaoyi.mp.service.UomguserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Controller
@RequestMapping("pay")
public class PayController {
@Autowired
private FeignService feignService;
@RequestMapping("/demo1")
@ResponseBody
public String demo1(){
feignService.demo("demo");
return null;
}
@RequestMapping("/demo2")
@ResponseBody
public String demo2(@RequestParam("username") String username){
System.out.println(username);
return null;
}
@RequestMapping("/demo3")
@ResponseBody
public String demo3(){
UomgUser user=new UomgUser();
user.setUsername("123");
user.setPassword("456");
feignService.demo2(user);
return null;
}
@RequestMapping("/demo4")
@ResponseBody
public String demo4(@RequestBody UomgUser user){
System.out.println("你成功了");
return null;
}
}
?
|