本文主要是介绍实现调用阿里云API第二章——API调用实战(全球IP地址归属地查询),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本篇将实现利用springboot实现阿里云API调用实战。
一、购买阿里云API
https://market.aliyun.com/data?spm=5176.9502607.1387859.3.c0634d50uqp5Dd
登陆到阿里云API市场,搜索并购买“全球IP地址归属地查询” API
二、新建maven工程
添加如下pom.xml文件
<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 http://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.0.0.RELEASE</version></parent><groupId>com.picc</groupId><artifactId>01-spring-boot-Hello</artifactId><version>0.0.1-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><!--以下是ali云依赖 --><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.15</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.2.1</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpcore</artifactId><version>4.2.1</version></dependency><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.6</version></dependency><dependency><groupId>org.eclipse.jetty</groupId><artifactId>jetty-util</artifactId><version>9.3.7.v20160115</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.5</version><scope>test</scope></dependency></dependencies>
</project>
三、创建HttpUtils.java文件
并添加该路径下的代码
https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
四、创建showIPOrigin.java文件
粘贴请求示例
public static void main(String[] args) {String host = "https://hcapi20.market.alicloudapi.com";String path = "/ip";String method = "GET";String appcode = "你自己的AppCode";Map<String, String> headers = new HashMap<String, String>();//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105headers.put("Authorization", "APPCODE " + appcode);Map<String, String> querys = new HashMap<String, String>();querys.put("ip", "218.18.228.178");try {/*** 重要提示如下:* HttpUtils请从* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java* 下载** 相应的依赖请参照* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml*/HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);System.out.println(response.toString());//获取response的body//System.out.println(EntityUtils.toString(response.getEntity()));} catch (Exception e) {e.printStackTrace();}}
五、创建controller
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import com.picc.pojo.HttpUtils;
import com.picc.pojo.Users;
import com.picc.pojo.showIPOrigin;@Controller
@RequestMapping("/test")
public class HelloWorld {@RequestMapping("API")public String showIP(Model model) {showIPOrigin API=new showIPOrigin();//调用showIPOrigin.java的输出String myAPI=API.showIP();System.out.println(myAPI);model.addAttribute("api",myAPI);return "index5";}
}
六、创建templates
新建index5.html文件
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body><span th:text="${api}"></span>
</body>
</html>
七、创建启动类并启动
App.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class App {public static void main(String[] args) {// TODO Auto-generated method stubSpringApplication.run(App.class, args);}}
八、访问并验证
http://localhost:8087/test/API
这篇关于实现调用阿里云API第二章——API调用实战(全球IP地址归属地查询)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!