请求多参数的URL
1.1、Feign接口@FeignClient(name = "spring-ribbon-eureka-client2")
public interface UserFeignClient {
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public User findById(@PathVariable("id") Long id) throws Exception;
/**get方式,多参数请求方式一*/
@RequestMapping(value = "/find", method = RequestMethod.GET)
public User find1(@RequestParam("id") Long id, @RequestParam("username") String username) throws Exception;
/**get方式,多参数请求方式二*/
@RequestMapping(value = "/find", method = RequestMethod.GET)
public User find2(@RequestParam Map map) throws Exception;
/**post方式,多参数请求*/
@RequestMapping(value = "/find", method = RequestMethod.POST)
public User find3(@RequestBody User user) throws Exception;
}
1.2、使用package com.example.demo.controller;
import com.example.demo.feign.UserFeignClient;
import com.example.demo.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
/**
* 用户controller
*
* @Author: 我爱大金子
* @Description: 用户controller
* @Date: Create in 11:07 2017/7/10
*/
@RestController
public class UserController {
@Autowired
private UserFeignClient userFeignClient;
@GetMapping("/user/{id}")
public User findById(@PathVariable Long id) throws Exception {
if (null == id) {
return null;
}
return this.userFeignClient.findById(id);
}
@GetMapping("/user1")
public User find1(User user) throws Exception {
System.out.println("get方式,多参数请求方式一 : " + user);
if (null == user || null == user.getId()) {
return null;
}
return this.userFeignClient.find1(user.getId(), user.getUsername());
}
@GetMapping("/user2")
public User find2(User user) throws Exception {
System.out.println("get方式,多参数请求方式二 : " + user);
if (null == user || null == user.getId()) {
return null;
}
Map map = new HashMap();
map.put("id", user.getId());
return this.userFeignClient.find2(map);
}
@PostMapping("/user3")
public User find3(User user) throws Exception {
System.out.println("POST方式,多参数请求方式 : " + user);
if (null == user || null == user.getId()) {
return null;
}
return this.userFeignClient.find3(user);
}
}
本文介绍了在Spring Cloud Feign中如何处理包含NULL值的多参数请求。通过创建Feign接口,展示了GET和POST方法的不同参数传递方式,包括使用@RequestParam、@RequestBody和Map。在Controller中,针对NULL值进行了条件判断和处理,确保了调用的正确性。
:使用Feign实现声明式REST调用-构造多参数请求...&spm=1001.2101.3001.5002&articleId=112963014&d=1&t=3&u=d425791640874523985d8e5e6b68d6f5)
486

被折叠的 条评论
为什么被折叠?



