使用Feign构造多参数请求

一 编写电影微服务控制器
package com.itmuch.cloud.study.user.controller;

import java.util.Map;

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.RestController;

import com.google.common.collect.Maps;
import com.itmuch.cloud.study.user.entity.User;
import com.itmuch.cloud.study.user.feign.UserFeignClient;

@RestController
public class MovieController {
  @Autowired
  private UserFeignClient userFeignClient;

  @GetMapping("/user/{id}")
  public User findById(@PathVariable Long id) {
    return this.userFeignClient.findById(id);
  }

  /**
   * 测试URL:http://localhost:8010/user/get0?id=1&username=张三
   * 该请求不会成功。
   * @param user user
   * @return 用户信息
   */
  @GetMapping("/user/get0")
  public User get0(User user) {
    return this.userFeignClient.get0(user);
  }

  /**
   * 测试URL:http://localhost:8010/user/get1?id=1&username=张三
   * @param user user
   * @return 用户信息
   */
  @GetMapping("/user/get1")
  public User get1(User user) {
    return this.userFeignClient.get1(user.getId(), user.getUsername());
  }

  /**
   * 测试URL:http://localhost:8010/user/get2?id=1&username=张三
   * @param user user
   * @return 用户信息
   */
  @GetMapping("/user/get2")
  public User get2(User user) {
    Map map = Maps.newHashMap();
    map.put("id", user.getId());
    map.put("username", user.getUsername());
    return this.userFeignClient.get2(map);
  }
二 编写电影微服务UserFeignClient
package com.itmuch.cloud.study.user.feign;

import com.itmuch.cloud.study.user.entity.User;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
  @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  public User findById(@PathVariable("id") Long id);

  // 该请求不会成功
  @RequestMapping(value = "/get", method = RequestMethod.GET)
  public User get0(User user);

  @RequestMapping(value = "/get", method = RequestMethod.GET)
  public User get1(@RequestParam("id") Long id, @RequestParam("username") String username);

  @RequestMapping(value = "/get", method = RequestMethod.GET)
  public User get2(@RequestParam Map map);
}
三 编写用户微服务控制器
package com.itmuch.cloud.study.controller;

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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.itmuch.cloud.study.entity.User;
import com.itmuch.cloud.study.repository.UserRepository;

@RestController
public class UserController {
  @Autowired
  private UserRepository userRepository;

  @GetMapping("/{id}")
  public User findById(@PathVariable Long id) {
    User findOne = this.userRepository.findOne(id);
    return findOne;
  }

  @GetMapping("/get")
  public User get(User user) {
    return user;
  }
}
四 测试
1 启动Eureka
2 启动用户微服务
3 启动电影微服务
4 浏览器 http://localhost:8010/user/get1?id=1&username=%E5%BC%A0%E4%B8%89
使用Feign构造多参数请求_第1张图片
5 浏览器 http://localhost:8010/user/get2?id=1&username=%E5%BC%A0%E4%B8%89
使用Feign构造多参数请求_第2张图片

你可能感兴趣的:(微服务)