feign调用接口参数可以为null吗_springCloud(10):使用Feign实现声明式REST调用-构造多参数请求...

本文介绍了在Spring Cloud Feign中如何处理包含NULL值的多参数请求。通过创建Feign接口,展示了GET和POST方法的不同参数传递方式,包括使用@RequestParam、@RequestBody和Map。在Controller中,针对NULL值进行了条件判断和处理,确保了调用的正确性。
摘要由CSDN通过智能技术生成

请求多参数的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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值