采用Feign调用外部http/https链接,比传统httpclient方法要简单,废话不多了,直接上代码
本文是调用微信 逆地址解析接口文档
https://apis.map.qq.com/ws/geocoder/v1?location=40.100834,117.089607&key=你自己的key&get_poi=1
其中key的值需要自己申请,申请地址 https://lbs.qq.com/
1、pom.xml
注意:不能使用最新的springboot版本2.4.0 ,要使用2.2.11release版本(要与Eurka版本一样)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.11.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.2.3</version>
<classifier>jdk15</classifier>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、application.yml
wx:
url: https://apis.map.qq.com
server:
port: 9090
3、DemoApplication,要添加 @EnableFeignClients注解
@SpringBootApplication
@EnableFeignClients
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
4、WxService
package com.example.demo.service;
import net.sf.json.JSONObject;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @author zqing
* @description: TODO
* @date: 2020/12/7 10:28
*/
@FeignClient(url="${wx.url}",name="wxServer")
public interface WxService {
/**
* 完整地址是:https://apis.map.qq.com/ws/geocoder/v1?location=40.100834,117.089607&key=4UXBZ-3YFE4-XXXXXXXXXX-XXXXX-D5BFV&get_poi=1
* @param location
* @param key
* @param get_poi
* @return
*/
@RequestMapping(value = "/ws/geocoder/v1")
public JSONObject getInfo(@RequestParam String location,
@RequestParam String key,
@RequestParam String get_poi);
}
5、Wxcontroller
package com.example.demo.controller;
import com.example.demo.service.WxService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author zqing
* @description: TODO
* @date: 2020/12/7 10:13
*/
@RestController
@CrossOrigin
public class WxController {
@Autowired
private WxService wxService;
@RequestMapping("/getData")
public JSONObject getInfo(@RequestParam String location,
@RequestParam String key,
@RequestParam String get_poi){
JSONObject json = wxService.getInfo(location,key,get_poi);
return (JSONObject) json.get("result");
}
}
6、直接访问http://localhost:9090/getData?location=40.100834,117.089607&key=你自己的key&get_poi=1