feignClient傳參(參數為對象類型)的一個坑


客戶端

    @RequestMapping(value = "/friendCircleComment/comment",method = RequestMethod.POST)
    R comment(@RequestBody FriendCircleComment friendCircleComment);

服務端

1   @RequestMapping(value = "/comment")
2   public R comment(@RequestBody FriendCircleComment friendCircleComment){
3     friendCircleCommentService.comment(friendCircleComment);
4     return new R();
5   }

這么傳參是沒問題的,服務端也能接收到

但是,問題來了,

小程序的post請求的header必須為

  header:{ 'content-type':'application/x-www-form-urlencoded'  }, 

導致后台為@RequestBody接收不到參數,

feignClient默認參數請求類型是

header:{ 'content-type':'application/json'  }, 

定義@RequestBody接收參數的headers類型必須為 header:{ 'content-type':'application/json'  }, 

所以這樣就有沖突,feignClient和定義為'content-type':'application/x-www-form-urlencoded'的請求接口不能共用

解決方法

不使用對象接收,使用基本類型接收

如下

客戶端

1     @RequestMapping(value = "/friendCircleComment/comment",method = RequestMethod.POST)
2     R comment(@RequestParam(value = "friendCircleId",required = false)Integer friendCircleId,
3               @RequestParam(value = "memberId",required = false)Integer memberId,
4               @RequestParam(value = "parentId",required = false)Integer parentId,
5               @RequestParam(value = "comment",required = false)String comment,
6               @RequestParam(value = "replyMemberId",required = false)Integer replyMemberId);

服務端

 1   @RequestMapping(value = "/comment")
 2   public R comment(@RequestParam(value = "friendCircleId",required = false)Integer friendCircleId,
 3                    @RequestParam(value = "memberId",required = false)Integer memberId,
 4                    @RequestParam(value = "parentId",required = false)Integer parentId,
 5                    @RequestParam(value = "comment",required = false)String comment,
 6                    @RequestParam(value = "replyMemberId",required = false)Integer replyMemberId
 7                    ){
 8     FriendCircleComment friendCircleComment = new FriendCircleComment();
 9     friendCircleComment.setFriendCircleId(friendCircleId);
10     friendCircleComment.setMemberId(memberId);
11     friendCircleComment.setParentId(parentId);
12     friendCircleComment.setComment(comment);
13     friendCircleComment.setReplyMemberId(replyMemberId);
14     friendCircleCommentService.comment(friendCircleComment);
15     return new R();
16   }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM