首页 > 其他分享 >RestTemplate调用天地图api

RestTemplate调用天地图api

时间:2023-02-10 15:12:31浏览次数:44  
标签:调用 String RestTemplate UriComponentsBuilder api postStr queryParam

代码

  ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(HttpClients.createDefault());
  RestTemplate client = new RestTemplate(requestFactory);

  String postStr = "{\"keyWord\":\"北京大学\",\"level\":12,\"mapBound\":\"116.02524,39.83833,116.65592,39.99185\",\"queryType\":1,\"start\":0,\"count\":10}";
  String url = "http://api.tianditu.gov.cn/v2/search";
  UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url)
    .queryParam("postStr",postStr)
    .queryParam("type","query")
    .queryParam("tk",密钥);
  ResponseEntity<JSONObject> responseEntity = client.getForEntity(builder.build(false).toUri(), JSONObject.class);

部分依赖

        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.13</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>

 

标签:调用,String,RestTemplate,UriComponentsBuilder,api,postStr,queryParam
From: https://www.cnblogs.com/zqyn/p/17108992.html

相关文章