赞
踩
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- </dependency>
-
- <dependency>
- <groupId>io.swagger</groupId>
- <artifactId>swagger-annotations</artifactId>
- <version>1.5.13</version>
- </dependency>
-
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- <version>1.2.79</version>
- </dependency>
-
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-lang3</artifactId>
- <version>3.9</version>
- </dependency>
-
- <dependency>
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpclient</artifactId>
- <version>4.5.13</version>
- </dependency>
- package com.example.demo.controller;
-
- import com.alibaba.fastjson.JSONObject;
- import com.example.demo.util.HttpUtils;
-
- import io.swagger.annotations.ApiOperation;
-
- import org.apache.http.HttpResponse;
- import org.apache.http.util.EntityUtils;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
-
- import java.util.HashMap;
- import java.util.Map;
-
- @RestController
- @RequestMapping("/test")
- public class AddressController {
-
- @Value("${administrativeRegion.code}")
- private String Code;
-
-
- @GetMapping("/searchArea")
- @ApiOperation("用户获取收货地址省市县")
- public JSONObject searchArea(
- @RequestParam(name = "provinceId", required = false, defaultValue = "") String provinceId,
- @RequestParam(name = "cityId", required = false, defaultValue = "") String cityId,
- @RequestParam(name = "countyId", required = false, defaultValue = "") String countyId,
- @RequestParam(name = "townId", required = false, defaultValue = "") String townId,
- @RequestParam(name = "villageId", required = false, defaultValue = "") String villageId) {
- String host = "https://areasel.market.alicloudapi.com";
- String path = "/v1/area/search";
- String method = "GET";
- String appcode = Code;
- Map<String, String> headers = new HashMap<>();
- headers.put("Authorization", "APPCODE " + appcode);
- Map<String, String> querys = new HashMap<>();
- querys.put("provinceId", provinceId);
- querys.put("cityId", cityId);
- querys.put("countyId", countyId);
- querys.put("townId", townId);
- querys.put("villageId", villageId);
- JSONObject parse = null;
- try {
- HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
- // 如果需要获取response的body,可以使用下面这行代码
- String string = EntityUtils.toString(response.getEntity());
- parse = JSONObject.parseObject(string);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return parse;
- }
- }
- package com.example.demo.util;
-
- import org.apache.commons.lang3.StringUtils;
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.HttpDelete;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.client.methods.HttpPut;
- import org.apache.http.conn.ClientConnectionManager;
- import org.apache.http.conn.scheme.Scheme;
- import org.apache.http.conn.scheme.SchemeRegistry;
- import org.apache.http.conn.ssl.SSLSocketFactory;
- import org.apache.http.entity.ByteArrayEntity;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.message.BasicNameValuePair;
-
- import javax.net.ssl.SSLContext;
- import javax.net.ssl.TrustManager;
- import javax.net.ssl.X509TrustManager;
- import java.io.UnsupportedEncodingException;
- import java.net.URLEncoder;
- import java.security.KeyManagementException;
- import java.security.NoSuchAlgorithmException;
- import java.security.cert.X509Certificate;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
-
- public class HttpUtils {
-
-
- /**
- * get
- *
- * @param host
- * @param path
- * @param method
- * @param headers
- * @param querys
- * @return
- * @throws Exception
- */
- public static HttpResponse doGet(String host, String path, String method,
- Map<String, String> headers,
- Map<String, String> querys)
- throws Exception {
- HttpClient httpClient = wrapClient(host);
- HttpGet request = new HttpGet(buildUrl(host, path, querys));
- for (Map.Entry<String, String> e : headers.entrySet()) {
- request.addHeader(e.getKey(), e.getValue());
- }
- return httpClient.execute(request);
- }
- /**
- * post form
- *
- * @param host
- * @param path
- * @param method
- * @param headers
- * @param querys
- * @param bodys
- * @return
- * @throws Exception
- */
- public static HttpResponse doPost(String host, String path, String method,
- Map<String, String> headers,
- Map<String, String> querys,
- Map<String, String> bodys)
- throws Exception {
- HttpClient httpClient = wrapClient(host);
- HttpPost request = new HttpPost(buildUrl(host, path, querys));
- for (Map.Entry<String, String> e : headers.entrySet()) {
- request.addHeader(e.getKey(), e.getValue());
- }
- if (bodys != null) {
- List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
- for (String key : bodys.keySet()) {
- nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));
- }
- UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");
- formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
- request.setEntity(formEntity);
- }
- return httpClient.execute(request);
- }
- /**
- * Post String
- *
- * @param host
- * @param path
- * @param method
- * @param headers
- * @param querys
- * @param body
- * @return
- * @throws Exception
- */
- public static HttpResponse doPost(String host, String path, String method,
- Map<String, String> headers,
- Map<String, String> querys,
- String body)
- throws Exception {
- HttpClient httpClient = wrapClient(host);
- HttpPost request = new HttpPost(buildUrl(host, path, querys));
- for (Map.Entry<String, String> e : headers.entrySet()) {
- request.addHeader(e.getKey(), e.getValue());
- }
- if (StringUtils.isNotBlank(body)) {
- request.setEntity(new StringEntity(body, "utf-8"));
- }
- return httpClient.execute(request);
- }
- /**
- * Post stream
- *
- * @param host
- * @param path
- * @param method
- * @param headers
- * @param querys
- * @param body
- * @return
- * @throws Exception
- */
- public static HttpResponse doPost(String host, String path, String method,
- Map<String, String> headers,
- Map<String, String> querys,
- byte[] body)
- throws Exception {
- HttpClient httpClient = wrapClient(host);
- HttpPost request = new HttpPost(buildUrl(host, path, querys));
- for (Map.Entry<String, String> e : headers.entrySet()) {
- request.addHeader(e.getKey(), e.getValue());
- }
- if (body != null) {
- request.setEntity(new ByteArrayEntity(body));
- }
- return httpClient.execute(request);
- }
- /**
- * Put String
- * @param host
- * @param path
- * @param method
- * @param headers
- * @param querys
- * @param body
- * @return
- * @throws Exception
- */
- public static HttpResponse doPut(String host, String path, String method,
- Map<String, String> headers,
- Map<String, String> querys,
- String body)
- throws Exception {
- HttpClient httpClient = wrapClient(host);
- HttpPut request = new HttpPut(buildUrl(host, path, querys));
- for (Map.Entry<String, String> e : headers.entrySet()) {
- request.addHeader(e.getKey(), e.getValue());
- }
- if (StringUtils.isNotBlank(body)) {
- request.setEntity(new StringEntity(body, "utf-8"));
- }
- return httpClient.execute(request);
- }
- /**
- * Put stream
- * @param host
- * @param path
- * @param method
- * @param headers
- * @param querys
- * @param body
- * @return
- * @throws Exception
- */
- public static HttpResponse doPut(String host, String path, String method,
- Map<String, String> headers,
- Map<String, String> querys,
- byte[] body)
- throws Exception {
- HttpClient httpClient = wrapClient(host);
- HttpPut request = new HttpPut(buildUrl(host, path, querys));
- for (Map.Entry<String, String> e : headers.entrySet()) {
- request.addHeader(e.getKey(), e.getValue());
- }
- if (body != null) {
- request.setEntity(new ByteArrayEntity(body));
- }
- return httpClient.execute(request);
- }
- /**
- * Delete
- *
- * @param host
- * @param path
- * @param method
- * @param headers
- * @param querys
- * @return
- * @throws Exception
- */
- public static HttpResponse doDelete(String host, String path, String method,
- Map<String, String> headers,
- Map<String, String> querys)
- throws Exception {
- HttpClient httpClient = wrapClient(host);
- HttpDelete request = new HttpDelete(buildUrl(host, path, querys));
- for (Map.Entry<String, String> e : headers.entrySet()) {
- request.addHeader(e.getKey(), e.getValue());
- }
- return httpClient.execute(request);
- }
- private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException {
- StringBuilder sbUrl = new StringBuilder();
- sbUrl.append(host);
- if (!StringUtils.isBlank(path)) {
- sbUrl.append(path);
- }
- if (null != querys) {
- StringBuilder sbQuery = new StringBuilder();
- for (Map.Entry<String, String> query : querys.entrySet()) {
- if (0 < sbQuery.length()) {
- sbQuery.append("&");
- }
- if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {
- sbQuery.append(query.getValue());
- }
- if (!StringUtils.isBlank(query.getKey())) {
- sbQuery.append(query.getKey());
- if (!StringUtils.isBlank(query.getValue())) {
- sbQuery.append("=");
- sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
- }
- }
- }
- if (0 < sbQuery.length()) {
- sbUrl.append("?").append(sbQuery);
- }
- }
- return sbUrl.toString();
- }
- private static HttpClient wrapClient(String host) {
- HttpClient httpClient = new DefaultHttpClient();
- if (host.startsWith("https://")) {
- sslClient(httpClient);
- }
- return httpClient;
- }
- private static void sslClient(HttpClient httpClient) {
- try {
- SSLContext ctx = SSLContext.getInstance("TLS");
- X509TrustManager tm = new X509TrustManager() {
- public X509Certificate[] getAcceptedIssuers() {
- return null;
- }
- public void checkClientTrusted(X509Certificate[] xcs, String str) {
- }
- public void checkServerTrusted(X509Certificate[] xcs, String str) {
- }
- };
- ctx.init(null, new TrustManager[] { tm }, null);
- SSLSocketFactory ssf = new SSLSocketFactory(ctx);
- ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
- ClientConnectionManager ccm = httpClient.getConnectionManager();
- SchemeRegistry registry = ccm.getSchemeRegistry();
- registry.register(new Scheme("https", 443, ssf));
- } catch (KeyManagementException ex) {
- throw new RuntimeException(ex);
- } catch (NoSuchAlgorithmException ex) {
- throw new RuntimeException(ex);
- }
- }
-
- }
- {
- "msg": "成功",
- "code": 200,
- "data": {
- "list": [
- {
- "townname": "东华门街道办事处",
- "provincename": "北京市",
- "countyid": "110101000000",
- "cityname": "市辖区",
- "id": 1,
- "cityid": "110100000000",
- "townid": "110101001000",
- "provinceid": "110000",
- "countyname": "东城区",
- "villageid": "110101001001",
- "villagename": "多福巷社区"
- },
- {
- "townname": "劝业场街道",
- "provincename": "天津市",
- "countyid": "120101000000",
- "cityname": "市辖区",
- "id": 2,
- "cityid": "120100000000",
- "townid": "120101001000",
- "provinceid": "120000",
- "countyname": "和平区",
- "villageid": "120101001001",
- "villagename": "花园路社区"
- },
- {
- "townname": "建北街道办事处",
- "provincename": "河北省",
- "countyid": "130102000000",
- "cityname": "石家庄市",
- "id": 3,
- "cityid": "130100000000",
- "townid": "130102001000",
- "provinceid": "130000",
- "countyname": "长安区",
- "villageid": "130102001001",
- "villagename": "棉一社区"
- },
- {
- "townname": "坞城街道办事处",
- "provincename": "山西省",
- "countyid": "140105000000",
- "cityname": "太原市",
- "id": 4,
- "cityid": "140100000000",
- "townid": "140105001000",
- "provinceid": "140000",
- "countyname": "小店区",
- "villageid": "140105001001",
- "villagename": "师范街社区"
- },
- {
- "townname": "海拉尔东路街道办事处",
- "provincename": "内蒙古自治区",
- "countyid": "150102000000",
- "cityname": "呼和浩特市",
- "id": 5,
- "cityid": "150100000000",
- "townid": "150102001000",
- "provinceid": "150000",
- "countyname": "新城区",
- "villageid": "150102001001",
- "villagename": "公主府社区"
- },
- {
- "townname": "浑河湾街道",
- "provincename": "辽宁省",
- "countyid": "210102000000",
- "cityname": "沈阳市",
- "id": 6,
- "cityid": "210100000000",
- "townid": "210102001000",
- "provinceid": "210000",
- "countyname": "和平区",
- "villageid": "210102001002",
- "villagename": "水源地社区"
- },
- {
- "townname": "南岭街道办事处",
- "provincename": "吉林省",
- "countyid": "220102000000",
- "cityname": "长春市",
- "id": 7,
- "cityid": "220100000000",
- "townid": "220102001000",
- "provinceid": "220000",
- "countyname": "南关区",
- "villageid": "220102001001",
- "villagename": "华阳社区"
- },
- {
- "townname": "兆麟街道办事处",
- "provincename": "黑龙江省",
- "countyid": "230102000000",
- "cityname": "哈尔滨市",
- "id": 8,
- "cityid": "230100000000",
- "townid": "230102001000",
- "provinceid": "230000",
- "countyname": "道里区",
- "villageid": "230102001001",
- "villagename": "省报社社区"
- },
- {
- "townname": "南京东路街道",
- "provincename": "上海市",
- "countyid": "310101000000",
- "cityname": "市辖区",
- "id": 9,
- "cityid": "310100000000",
- "townid": "310101002000",
- "provinceid": "310000",
- "countyname": "黄浦区",
- "villageid": "310101002001",
- "villagename": "云南中路"
- },
- {
- "townname": "梅园新村街道",
- "provincename": "江苏省",
- "countyid": "320102000000",
- "cityname": "南京市",
- "id": 10,
- "cityid": "320100000000",
- "townid": "320102002000",
- "provinceid": "320000",
- "countyname": "玄武区",
- "villageid": "320102002001",
- "villagename": "梅园新村社区"
- },
- {
- "townname": "清波街道",
- "provincename": "浙江省",
- "countyid": "330102000000",
- "cityname": "杭州市",
- "id": 11,
- "cityid": "330100000000",
- "townid": "330102001000",
- "provinceid": "330000",
- "countyname": "上城区",
- "villageid": "330102001051",
- "villagename": "清波门社区"
- },
- {
- "townname": "明光路街道",
- "provincename": "安徽省",
- "countyid": "340102000000",
- "cityname": "合肥市",
- "id": 12,
- "cityid": "340100000000",
- "townid": "340102001000",
- "provinceid": "340000",
- "countyname": "瑶海区",
- "villageid": "340102001001",
- "villagename": "填海巷社区"
- },
- {
- "townname": "鼓东街道",
- "provincename": "福建省",
- "countyid": "350102000000",
- "cityname": "福州市",
- "id": 13,
- "cityid": "350100000000",
- "townid": "350102001000",
- "provinceid": "350000",
- "countyname": "鼓楼区",
- "villageid": "350102001001",
- "villagename": "中山社区"
- },
- {
- "townname": "公园街道办事处",
- "provincename": "江西省",
- "countyid": "360102000000",
- "cityname": "南昌市",
- "id": 14,
- "cityid": "360100000000",
- "townid": "360102001000",
- "provinceid": "360000",
- "countyname": "东湖区",
- "villageid": "360102001001",
- "villagename": "上营坊社区"
- },
- {
- "townname": "解放路街道办事处",
- "provincename": "山东省",
- "countyid": "370102000000",
- "cityname": "济南市",
- "id": 15,
- "cityid": "370100000000",
- "townid": "370102001000",
- "provinceid": "370000",
- "countyname": "历下区",
- "villageid": "370102001002",
- "villagename": "解放桥社区"
- },
- {
- "townname": "林山寨街道办事处",
- "provincename": "河南省",
- "countyid": "410102000000",
- "cityname": "郑州市",
- "id": 16,
- "cityid": "410100000000",
- "townid": "410102001000",
- "provinceid": "410000",
- "countyname": "中原区",
- "villageid": "410102001001",
- "villagename": "邮电院社区"
- },
- {
- "townname": "大智街办事处",
- "provincename": "湖北省",
- "countyid": "420102000000",
- "cityname": "武汉市",
- "id": 17,
- "cityid": "420100000000",
- "townid": "420102002000",
- "provinceid": "420000",
- "countyname": "江岸区",
- "villageid": "420102002003",
- "villagename": "保成社区"
- },
- {
- "townname": "文艺路街道",
- "provincename": "湖南省",
- "countyid": "430102000000",
- "cityname": "长沙市",
- "id": 18,
- "cityid": "430100000000",
- "townid": "430102001000",
- "provinceid": "430000",
- "countyname": "芙蓉区",
- "villageid": "430102001001",
- "villagename": "识字里社区"
- },
- {
- "townname": "沙面街道",
- "provincename": "广东省",
- "countyid": "440103000000",
- "cityname": "广州市",
- "id": 19,
- "cityid": "440100000000",
- "townid": "440103001000",
- "provinceid": "440000",
- "countyname": "荔湾区",
- "villageid": "440103001101",
- "villagename": "翠洲社区"
- },
- {
- "townname": "民生街道办事处",
- "provincename": "广西壮族自治区",
- "countyid": "450102000000",
- "cityname": "南宁市",
- "id": 20,
- "cityid": "450100000000",
- "townid": "450102001000",
- "provinceid": "450000",
- "countyname": "兴宁区",
- "villageid": "450102001003",
- "villagename": "兴宁社区"
- },
- {
- "townname": "秀英街道办事处",
- "provincename": "海南省",
- "countyid": "460105000000",
- "cityname": "海口市",
- "id": 21,
- "cityid": "460100000000",
- "townid": "460105001000",
- "provinceid": "460000",
- "countyname": "秀英区",
- "villageid": "460105001001",
- "villagename": "秀华社区"
- },
- {
- "townname": "高笋塘街道办事处",
- "provincename": "重庆市",
- "countyid": "500101000000",
- "cityname": "市辖区",
- "id": 22,
- "cityid": "500100000000",
- "townid": "500101001000",
- "provinceid": "500000",
- "countyname": "万州区",
- "villageid": "500101001001",
- "villagename": "东方广场社区"
- },
- {
- "townname": "督院街街道办事处",
- "provincename": "四川省",
- "countyid": "510104000000",
- "cityname": "成都市",
- "id": 23,
- "cityid": "510100000000",
- "townid": "510104020000",
- "provinceid": "510000",
- "countyname": "锦江区",
- "villageid": "510104020007",
- "villagename": "青石桥社区"
- },
- {
- "townname": "后巢乡",
- "provincename": "贵州省",
- "countyid": "520102000000",
- "cityname": "贵阳市",
- "id": 24,
- "cityid": "520100000000",
- "townid": "520102200000",
- "provinceid": "520000",
- "countyname": "南明区",
- "villageid": "520102200001",
- "villagename": "山水黔城"
- },
- {
- "townname": "华山街道办事处",
- "provincename": "云南省",
- "countyid": "530102000000",
- "cityname": "昆明市",
- "id": 25,
- "cityid": "530100000000",
- "townid": "530102001000",
- "provinceid": "530000",
- "countyname": "五华区",
- "villageid": "530102001001",
- "villagename": "翠湖西路社区"
- },
- {
- "townname": "八廓街道办事处",
- "provincename": "西藏自治区",
- "countyid": "540102000000",
- "cityname": "拉萨市",
- "id": 26,
- "cityid": "540100000000",
- "townid": "540102002000",
- "provinceid": "540000",
- "countyname": "城关区",
- "villageid": "540102002001",
- "villagename": "绕赛社区"
- },
- {
- "townname": "西一路街道办事处",
- "provincename": "陕西省",
- "countyid": "610102000000",
- "cityname": "西安市",
- "id": 27,
- "cityid": "610100000000",
- "townid": "610102001000",
- "provinceid": "610000",
- "countyname": "新城区",
- "villageid": "610102001001",
- "villagename": "民乐社区"
- },
- {
- "townname": "酒泉路街道",
- "provincename": "甘肃省",
- "countyid": "620102000000",
- "cityname": "兰州市",
- "id": 28,
- "cityid": "620100000000",
- "townid": "620102001000",
- "provinceid": "620000",
- "countyname": "城关区",
- "villageid": "620102001001",
- "villagename": "畅家巷社区"
- },
- {
- "townname": "东关大街街道办事处",
- "provincename": "青海省",
- "countyid": "630102000000",
- "cityname": "西宁市",
- "id": 29,
- "cityid": "630100000000",
- "townid": "630102001000",
- "provinceid": "630000",
- "countyname": "城东区",
- "villageid": "630102001001",
- "villagename": "北关社区"
- },
- {
- "townname": "凤凰北街街道办事处",
- "provincename": "宁夏回族自治区",
- "countyid": "640104000000",
- "cityname": "银川市",
- "id": 30,
- "cityid": "640100000000",
- "townid": "640104001000",
- "provinceid": "640000",
- "countyname": "兴庆区",
- "villageid": "640104001002",
- "villagename": "崇安社区"
- },
- {
- "townname": "燕儿窝街道",
- "provincename": "新疆维吾尔自治区",
- "countyid": "650102000000",
- "cityname": "乌鲁木齐市",
- "id": 31,
- "cityid": "650100000000",
- "townid": "650102002000",
- "provinceid": "650000",
- "countyname": "天山区",
- "villageid": "650102002002",
- "villagename": "燕儿窝南社区"
- },
- {
- "townname": "市府路",
- "provincename": "台湾省",
- "countyid": "710101000000",
- "cityname": "台北市",
- "id": 32,
- "cityid": "710100000000",
- "townid": "710101001000",
- "provinceid": "710000",
- "countyname": "信义区",
- "villageid": "710101001001",
- "villagename": "嘉兴街"
- },
- {
- "townname": "下亚厘毕道",
- "provincename": "香港特别行政区",
- "countyid": "810101000000",
- "cityname": "香港岛",
- "id": 33,
- "cityid": "810100000000",
- "townid": "810101001000",
- "provinceid": "810000",
- "countyname": "中西区",
- "villageid": "810101001001",
- "villagename": "政府合署西座"
- },
- {
- "townname": "南湾湖",
- "provincename": "澳门特别行政区",
- "countyid": "820101000000",
- "cityname": "市辖区",
- "id": 34,
- "cityid": "820100000000",
- "townid": "820101001000",
- "provinceid": "820000",
- "countyname": "风顺堂区",
- "villageid": "820101001001",
- "villagename": "景大马路"
- }
- ]
- },
- "requestid": "1255962151722418176"
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。