openFeign调用接口时传递表单参数、Json参数、HttpServletRequest对象

2023-12-28 17:11:43


openFeign调用接口时传递Json参数以及httpservletrequest对象


一、控制器

@RestController
@RequestMapping("test")
public class TestController {

	/**
     * 用来测试表单参数传递:单个参数
     */
	@PostMapping(value = "/formDataTest")
	public Object formDataTest(@RequestParam String id) {
		......
	}
	
	/**
     * 用来测试表单参数传递:多个参数
     */
	@PostMapping(value = "/formDataTest2")
	public Object formDataTest2(@RequestParam String id, @RequestParam String name) {
		......
	}

	/**
     * 用来测试Json参数传递
     */
	@PostMapping(value = "/jsonTest")
	public Object jsonTest(@RequestBody JsonTestParam param) {
		......
	}
	
	 /**
     * 用来测试HttpServletRequest参数传递
     */
	@PostMapping(value = "/httpServletRequest_Test")
	public Object httpServletRequest_Test(HttpServletRequest request) {
		......
	}
}

二、Feign客户端

@FeignClient(name = "defaultXXX-cloud", contextId = "XXXClient", path = "defaultXXXPath")
@RefreshScope
public interface XXXFeignClient {

	/**
     * 传递表单参数:单个参数
     */
    @PostMapping(value = "/test/formDataTest")
    Object formDataTest(String id);

	/**
     * 传递表单参数:多个参数,需要使用@RequestParam并指定name属性,否则会报错
     */
    @PostMapping(value = "/test/formDataTest2")
    Object formDataTest2(@RequestParam(name = "id") String id, @RequestParam(name = "name") String name);


	/**
     * 传递JSON参数方式一:指定:consumes = "application/json"
     */
    @PostMapping(value = "/test/jsonTest", consumes = "application/json")
    Object jsonTest(JsonTestParam param);

	/**
     * 传递JSON参数方式二:使用@RequestBody
     */
    @PostMapping(value = "/test/jsonTest")
    Object jsonTest(@RequestBody JsonTestParam param);


	/**
     * 传递HttpServletRequest:不需要传递,直接调用即可
     */
	@PostMapping(value = "/test/httpServletRequest_Test")
    Object httpServletRequest_Test();

}

文章来源:https://blog.csdn.net/weixin_52116015/article/details/135271786
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。