我从网上找了个接口的提供方 里面的接口有很多免费的哦!
分享一下:
https://www.juhe.cn/
注册完账号申请个接口 用来测试
举例 会有个Key保存好用得到
点进去看API文档
接下来撸代码:
第一步:引入springcloud的openfeign
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
第二步:编写启动类
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@MapperScan("com.astronomy.user.mapper")
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
注意:
必须得写上这个注解@EnableFeignClients,作用是启动feign客户端
第三步:根据API提供的请求方式和url编写feign调第三方的接口
@FeignClient(name = "test", url="http://apis.juhe.cn")
public interface FeignService {
/**
* 功能描述: <br>
* <手机号归属地查询>
* @Param [phone, key]
* @return com.astronomy.ResponseResult
* @Author: wlt
* @Date: 2020/12/2 15:17
**/
@GetMapping(path = "/mobile/get")
String queryMobile(@RequestParam String phone,@RequestParam String key);
}
第四步:编写Controller
@RestController //@RestController返回的就是json
public class MobileController {
@Autowired
FeignService feignService;
@GetMapping(path = "/mobile", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public String queryMobile() {
//用自己手机号
String phone = "187xxxx8125";
String key = "4858c6fe36aa470fe03fbf1de4c1855d";
String res = feignService.queryMobile(phone, key);
return res;
}
}
这里说一下 如果返回的结果你想继续封装成实体类,用JSONObject转一下格式
第五步:测试
控制台打印结果
页面访问得到的结果
API成功返回示例
controller指定的对象返回数据
需要引入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
返回对象类ResponseResult
package com.astronomy;
import lombok.Data;
import java.io.Serializable;
/**
* 类说明: <br>
* 〈〉
*
* @Author: wlt
* @Date: 2020/11/30 15:26
*/
@Data
public class ResponseResult implements Serializable {
private static final long serialVersionUID = 6252124889414807812L;
private int resultcode;
private String reason;
private String result;
public ResponseResult(int resultcode, String reason) {
this.resultcode = resultcode;
this.reason = reason;
}
}
controller代码
package com.astronomy.user.controller;
import com.alibaba.fastjson.JSONObject;
import com.astronomy.ResponseResult;
import com.astronomy.user.service.feign.FeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* 类说明: <br>
* 〈〉
*
* @Author: wlt
* @Date: 2020/12/2 11:33
*/
@Slf4j
@RestController
public class MobileController {
public static final String KEY = "4858c6fe36aa470fe03fbf1de4c1855d";
@Autowired
FeignService feignService;
@GetMapping(path = "/mobile/{phone}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseResult queryMobile(@PathVariable String phone) {
String res = null;
try {
//第三方接口
res = feignService.queryMobile(phone, KEY);
log.info(res);
} catch (Exception e) {
log.error("手机号格式错误 {}", phone);
return new ResponseResult(500, "远程调用手机归属地查询接口失败");
}
if (null != res) {
//String转JSONObject
JSONObject jsonObject = JSONObject.parseObject(res);
//JSONObject转指定的返回对象ResponseResult
ResponseResult responseResult = jsonObject.toJavaObject(ResponseResult.class);
System.out.println(responseResult);
return responseResult;
}
return null;
}
}
测试结果:
访问页面