初始化
parent
01f34e61a5
commit
438e06429e
|
@ -1,68 +1,41 @@
|
||||||
package com.muyu.market.controller;
|
package com.muyu.market.controller;
|
||||||
|
|
||||||
|
import com.muyu.common.core.domain.Result;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import java.io.BufferedReader;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import java.io.IOException;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import java.io.InputStreamReader;
|
import org.springframework.web.client.RestTemplate;
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.net.URLEncoder;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/third")
|
@RequestMapping("/third")
|
||||||
public class ThirdPartyController {
|
public class ThirdPartyController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RestTemplate restTemplate;
|
||||||
|
|
||||||
@GetMapping("/getAirQuality")
|
@GetMapping("/getAirQuality")
|
||||||
public ResponseEntity<String> getAirQuality(@Validated String cityId) {
|
public ResponseEntity<Result<String>> getAirQuality(@RequestParam String cityId) {
|
||||||
String apiKey = "02ccc3ea04810432b45399ecb183195d";
|
String apiKey = "02ccc3ea04810432b45399ecb183195d";
|
||||||
String apiUrl = "http://apis.juhe.cn/fapigw/air/historyHours";
|
String apiUrl = "http://apis.juhe.cn/fapigw/air/historyHours?key=" + apiKey + "&cityId=" + cityId;
|
||||||
|
|
||||||
Map<String, String> params = new HashMap<>();
|
// 调用第三方API
|
||||||
params.put("key", apiKey);
|
String responseBody = restTemplate.getForObject(apiUrl, String.class);
|
||||||
params.put("cityId", cityId);
|
|
||||||
|
|
||||||
try {
|
// 创建Result对象并设置数据
|
||||||
URL url = new URL(apiUrl + "?" + buildParams(params));
|
Result<String> result = new Result<>();
|
||||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
result.setCode(200); // 假设API调用成功
|
||||||
connection.setRequestMethod("GET");
|
result.setMsg("查询成功");
|
||||||
int responseCode = connection.getResponseCode();
|
result.setData(responseBody); // 将API响应作为数据
|
||||||
|
|
||||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
// 返回ResponseEntity对象,包含HTTP状态码和响应体
|
||||||
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
return ResponseEntity.ok(result);
|
||||||
String inputLine;
|
|
||||||
StringBuffer response = new StringBuffer();
|
|
||||||
while ((inputLine = in.readLine()) != null) {
|
|
||||||
response.append(inputLine);
|
|
||||||
}
|
|
||||||
in.close();
|
|
||||||
return ResponseEntity.ok(response.toString());
|
|
||||||
} else {
|
|
||||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to retrieve data: HTTP error code " + responseCode);
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to retrieve data: " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String buildParams(Map<String, String> map) {
|
// 如果您还没有RestTemplate Bean,请确保在配置中创建它
|
||||||
return map.entrySet().stream()
|
// 例如,在Spring Boot的配置类中,您可以添加一个@Bean方法来创建RestTemplate
|
||||||
.map(entry -> {
|
|
||||||
try {
|
|
||||||
return entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.toString());
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect(Collectors.joining("&"));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue