因为接口是个get请求,开放式的,不需要引用什么sdk,纯调接口就行了
首先获取key!
在腾讯位置服务-》控制台-》应用管理-》我的应用-》创建应用,添加key,我使用的是webapi
https://lbs.qq.com/dev/console/application/mine
依赖辅助工具
- 父工程依赖
要用到spring-web依赖,此处省略。。。。
<!--json工具包,代理接口也需要,fast-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!--隔断,父工程中统一版本管理-->
<properties>
<fastjson.version>1.2.47</fastjson.version>
</properties>
- 子工程中依赖
<!--代理接口和json转化用着舒服-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
ps:非父子工程,直接使用
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
调用接口配置类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
return new RestTemplate(factory);
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(6000);
factory.setReadTimeout(6000);
return factory;
}
}
调用接口
封装的包在下边!!!!
//调接口
@Autowired
private RestTemplate restTemplate;
public Result getMapLeading(LeadingQuery query) {
Map<String, Object> param = new HashMap<>();
String pFrom = query.getFrom();
String pTo = query.getTo();
String way = query.getWay();
String key = AllApiSecret.KEY_LEAVES;//key是自己在腾讯地图上创建获取的
param.put("from", pFrom);
param.put("to", pTo);
param.put("key", key);
String url = "https://apis.map.qq.com/ws/direction/v1/" + way + "/?from={from}&to={to}&output=json&callback=cb&key={key}";
HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.AUTHORIZATION, null);
ResponseEntity<String> entity = null;
Result result = new Result(ResultCode.LEADING_FAILED); //默认调用失败,后边调用成功后覆盖
try {
entity = restTemplate.exchange(
url,
HttpMethod.GET,
new HttpEntity<String>(headers),
String.class,
param);
if (entity != null) {
String response = JSONObject.toJSONString(entity);
ResponseResult resultTemp = JSONObject.parseObject(response, ResponseResult.class);
LeadingResultBody resultBody = JSONObject.parseObject(resultTemp.getBody(), LeadingResultBody.class);
if (resultBody.getStatus().equals(0)) {
Object result1 = resultBody.getResult();
result = new Result(result1);
}
}
} catch (Exception e) {
log.info("导航调用失败,错误信息{}", e);
}
return result;
}
封装的包
//ResponseResult
import lombok.Data;
@Data
public class ResponseResult {
private String body;
private String statusCode;
private int statusCodeValue;
}
import lombok.Data;
import java.io.Serializable;
@Data
public class LeadingResultBody implements Serializable {
private String message;
private Integer status;
private String request_id;
private Object result;//不具体拆下去了
}