热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SpringMVC&FeignClient传参形式以及@RequestBody不能继承的问题

传参的几种形式@RequestParam(value"userName")StringuserName单个属性,适用于基本类型及其包装类型和String的传递@

Spring MVC & Feign Client 传参形式以及 @RequestBody 不能继承的问题

传参的几种形式

  • @RequestParam(value="userName") String userName 单个属性,适用于基本类型及其包装类型和String的传递
  • @RequestParam Map mapParams 通用型方式,通过 JDK 内置对象 Map,接收所有键值对的参数
  • @RequestBody User user 将整个 json 请求体包裹起来,作为 raw 传值;Content-Type: application/json
  • @ModelAttribute User user 将所有键值对映射到自定义对象的属性上,有点类似于@RequestParam Map(注意对象为空和对象的所有属性为空的区分)
  • 支持复杂参数类型以及多参数的传递,如: rest-api 接口定义:
    @GetMapping(value = "/rpc/erp/itemGift/page")
    ItemMapResponse giftList(@RequestParam("searchWord") String searchWord, @RequestParam("restPageJson") String restPageJson, @RequestBody ItemSupplierSearchRequest itemSupplierSearchRequest);

接口实现类:

    @Override
    public ItemMapResponse giftList(String searchWord, String restPageJson, @RequestBody ItemSupplierSearchRequest itemSupplierSearchRequest) {
        logger.info("查询赠品商品列表入参:restPageJsOnc={},searchWord={},restItemSearchDTO={}", restPageJson, searchWord,
                JSONObject.toJSONString(itemSupplierSearchRequest));
        ...
    }

用 cURL 请求(或者用 Charles 抓包拷贝 cURL 地址):

curl --location --request GET "http://localhost:9012/rpc/erp/itemGift/page?restPageJson=%7B%22pageNo%22:1,%22pageSize%22:10%7D&searchWord=%E8%B5%A0%E5%93%81" 
--header "Content-Type: application/json" 
--data-raw "{"title":"测试"}"

解决 @RequestBody 不能继承的问题

  • https://snippets.cacher.io/snippet/de950fbc3c9169c3e92f
  • 框架层面改动大,建议所有实现类 resouce 层还是全部将参数注解覆盖一遍接口中的定义。

参考

  • https://blog.csdn.net/uotail/article/details/84673347
  • https://www.jianshu.com/p/eedb068d4227

推荐阅读
author-avatar
局外人2502854057
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有