ApiRegionSearchController.java 1.67 KB
package cn.com.glsx.stdinterface.modules.controller;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * @Title:
 * @Description: 透传接口,查询百度地理信息
 * @Author xiehx
 * @Date 2024/12/31
 **/
@RequestMapping("/apiRegion")
@RestController
public class ApiRegionSearchController {

    // TODO ak放配置
    private static final String BAIDU_MAP_API_URL = "https://api.map.baidu.com/api_region_search/v1/?ak=pbKP9T69inAX0GVwS9uar2l1Yz6OLyyg&";

    @RequestMapping("/search")
    public Object getRegionSearch(@RequestParam Map<String, String> allParams) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        StringBuilder urlBuilder = new StringBuilder(BAIDU_MAP_API_URL);

        for (Map.Entry<String, String> entry : allParams.entrySet()) {
            urlBuilder.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.name())).append("&");
        }

        HttpGet request = new HttpGet(urlBuilder.toString());
        try (CloseableHttpResponse response = httpClient.execute(request)) {
            return EntityUtils.toString(response.getEntity());
        }
    }
}