Springcloud (10): Implementing declarative rest calls with feign-constructing multi-parameter requests

Source: Internet
Author: User

URL to request multiple parameters

Suppose the requested URL contains multiple parameters, such as: Http://localhost:8086/user1?id=1&username=nihao

1.1. Feign interface

@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 mode, multi-parameter request mode one */     @RequestMapping (value  =  "/find",  method = requestmethod.get)     public user find1 ( @RequestParam ("id")  Long id,  @RequestParam ("username")  string username)  throws  exception;    /**get mode, multi-parameter request mode two */     @RequestMapping (value  =  "/find",  method = requestmethod.get)     public user find2 ( @RequestParam  map<string, object> map)  throws Exception;     /**post mode, multi-parameter request */     @RequestMapping (value =  "/find",  method = requestmethod.post)     public user find3 (@RequestBody  user user)  throws  Exception;}

1.2, using

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;/** *   User controller * *  @Author:  I love big gold  *  @Description:  user controller *   @Date:  create in 11:07 2017/7/10 */@RestControllerpublic  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 { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Get mode, multi-parameter request mode one  : "  +  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&NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Get mode, multi-parameter request mode two  : "  + user);        if  (null == user | |  null == user.getid ())  {             return null;        }         Map<String, Object> map = new HashMap<String, Object> ();         map.put ("id",  user.getid ());         return  this.userfeignclient.find2 (map);    }      @PostMapping ("/user3")     public user find3 (User user)  throws exception {        system.out.println ("Post mode, Multi-parameter request mode  :  " + User);        if  (null == user | |  null == user.getid ())  {             return null;        }         return  this.userfeignclient.find3 (user);     }}


This article is from "I Love Big gold" blog, please be sure to keep this source http://1754966750.blog.51cto.com/7455444/1948455

Springcloud (10): Implementing declarative rest calls with feign-constructing multi-parameter requests

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.