You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2019/08/12 13:18:34 UTC

[GitHub] [servicecomb-java-chassis] yhs0092 commented on issue #1289: cse uri 添加参数如何操作

yhs0092 commented on issue #1289: cse uri 添加参数如何操作
URL: https://github.com/apache/servicecomb-java-chassis/issues/1289#issuecomment-520421086
 
 
   假设provider端有这样一个REST接口方法:
   ```java
   @RestSchema(schemaId = "hello")
   @RequestMapping(path = "/provider/v0")
   public class HelloService {
     @GetMapping(path = "/testQuery")
     public String testQuery(@RequestParam("q1") String q1,
         @RequestParam("q2") String q2) {
       return q1 + q2;
     }
   }
   ```
   在consumer端,你可以这样调用它:
   ```java
   @RestSchema(schemaId = "helloConsumer")
   @Path("/consumer/v0")
   public class HelloConsumerService {
     private RestTemplate restTemplate = RestTemplateBuilder.create();
   
     @Path("/testQuery")
     @GET
     public String testQuery() {
       // 可变长参数,注意与前面的占位符的顺序对应
       String response = restTemplate
           .getForObject(
               "cse://provider/provider/v0/testQuery?q1={q1}&q2={q2}",
               String.class,
               "qqq1", "qqq2");
       System.out.println("response1 = " + response);
   
       // map参数,key值和占位符里的名字对应
       Map<String, String> params = new HashMap<String, String>();
       params.put("q1", "ppp2");
       params.put("q2", "ppp2");
       response = restTemplate.getForObject(
           "cse://provider/provider/v0/testQuery?q1={q1}&q2={q2}",
           String.class,
           params);
       System.out.println("response2 = " + response);
   
       // 或者你可以直接做字符串拼接
       response = restTemplate.getForObject(
           "cse://provider/provider/v0/testQuery?q1=sss1&q2=sss2",
           String.class);
       System.out.println("response3 = " + response);
       return "OK";
     }
   }
   ```
   
   或者你也可以去网上搜一搜别的用法,在网上能找到不少RestTemplate的使用资料 : )

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services