Feign⼆:@FeignClient接⼝调⽤在项⽬的启动⽂件加⼊:@EnableFeignClients 注解,
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloudflix.eureka.EnableEurekaClient;
import org.springframework.cloudflix.feign.EnableFeignClients;
import t.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients
public class FeignApp {
public static void main(String[] args) {
SpringApplication.run(FeignApp.class, args);
}
}
实例结构如下:
那么有实体类: User.java
Fengn客户端:UserFeignClient.javaspringboot结构
控制器: MovieController.java调取第三⽅user接⼝
User.java
import java.math.BigDecimal;
public class User {
private  Long id;
private String username;
private String name;
private int age;
private BigDecimal balance;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {  this.username = username;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public BigDecimal getBalance() {
return balance;
}
public void setBalance(BigDecimal balance) {  this.balance = balance;
}
}
UserFeign客户端
其中:@FeignClient("spring-boot-user"): spring-boot-user是eureka服务⾥⾯user项⽬的名称,加⼊此注解,能直接连接user项⽬接⼝
import org.springframework.cloudflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.ity.User;
@FeignClient("spring-boot-user")
public interface UserFeignClient {
// 两个坑:1. @GetMapping不⽀持  2. @PathVariable得设置value
@RequestMapping(value="/simple/{id}", method=RequestMethod.GET)
public User findById(@PathVariable("id") Long id);
@RequestMapping(value="/test", method=RequestMethod.POST)
public User postUser(@RequestBody User user);
}
MovieController控制中⼼,调取UserFeign客户端
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.RestController;
import com.ity.User;
import com.muyang.bootmovie.feign.UserFeignClient;
@RestController
public class MovieController {
@Autowired
private UserFeignClient userFeignClient;
@GetMapping("/movie/{id}")
public User findById(@PathVariable("id") Long id) {
return this.userFeignClient.findById(id);
}
@RequestMapping(value="/test", method=RequestMethod.GET)
public User userPost(User user)
{
return this.userFeignClient.postUser(user);
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。