Feign传参注意
最近在用SpringCloud尝试重构以前的项目,使用Feign客户端组件来调用微服务,经常出现参数传不过去变成null的问题,网上查了一下发现feign在参数上的使用还是有一定的限制的,主要是要注意:
-
当参数比较复杂时,feign即使声明为get请求也会强行使用post请求
-
不支持@GetMapping类似注解声明请求,需使用@RequestMapping(value = "url",method = RequestMethod.GET)
-
使用@RequestParam注解时必须要在后面加上参数名
写了一个简单案例,同时传输一个对象和一个字符串作为请求参数,
消费端controller
package com.taotao.feignchuanzhi.controller;
import com.sun.org.apache.xerces.internal.xs.datatypes.ObjectList;
import com.taotao.feignchuanzhi.entity.User;
import com.taotao.feignchuanzhi.service.FeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
public class FeignController {
@Autowired
private FeignService feignService;
//String 类型传值通过feign传递
@RequestMapping("/getOrderName")
public String getOrderName(String name){
return feignService.getName(name);
}
/**
* feign 多个参数传值
* @param name
* @param pass
* @return
*/
@RequestMapping("/getOrderNameMore")
public String getOrderNameMore(String name,String pass){
return feignService.getNameMore(name,pass);
}
/**
* feign 传递 model 类型的值
* @param user
* @return
*/
@RequestMapping("/getuserOrder")
public String user(User user){
return feignService.getUser(user);
}
}
feignService
package com.taotao.feignchuanzhi.service;
import com.taotao.feignchuanzhi.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
@FeignClient("app-itmayiedu-member")
public interface FeignService {
/**
* feign 传递 string 类型的值
* @RequestParam("name") String name 必须要有
* 也可以 @RequestParam(value="name") String name + value
*
* @param name
* @return
*/
@RequestMapping("/getName")
public String getName(@RequestParam("name") String name); //必须加上@RequestParam("name") 才能传值
/**
* feign 多个参数传值
* @param name
* @param pass
* @return
*/
@RequestMapping("/getNameMore")
public String getNameMore(@RequestParam("name") String name,@RequestParam(value="pass") String pass); //必须加上@RequestParam("name") 才能传值
/**
* feign 传递 model 类型的值
* @param user
* @RequestBody 必须要有
* @return
*/
@RequestMapping("/getUser")
public String getUser(@RequestBody User user);
}
会员端被调用的controller
package com.taotao.springcloud_erueka_member.controller;
import com.taotao.springcloud_erueka_member.entity.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@RestController
public class MemberController {
@Value("${server.port}")
private String port;
@RequestMapping("/getMember")
public String getMember(){
return "this is getMember,端口为:" +port;
}
/**
* 传递 String 类型的值
* @param name
* @return
*/
@RequestMapping("/getName")
public String getName(String name){
return "feign传递参数"+name;
}
/**
* feign 多个参数传值
* @param name
* @param pass
* @return
*/
@RequestMapping("/getNameMore")
public String getNameMore(@RequestParam("name") String name, @RequestParam(value="pass") String pass){
return "feign传递多参数的值"+name+pass;
}
/**
* feign 传递 module l类型的值
* @param user
* @return
*/
@RequestMapping("/getUser")
public String getUser(@RequestBody User user){
return "feign传递model类型的值"+user.getName()+user.getPass();
}
}
网友评论