初始化
parent
01f34e61a5
commit
438e06429e
|
@ -1,68 +1,41 @@
|
|||
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.ResponseEntity;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
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;
|
||||
import org.springframework.stereotype.Controller;
|
||||
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 org.springframework.web.client.RestTemplate;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/third")
|
||||
public class ThirdPartyController {
|
||||
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@GetMapping("/getAirQuality")
|
||||
public ResponseEntity<String> getAirQuality(@Validated String cityId) {
|
||||
public ResponseEntity<Result<String>> getAirQuality(@RequestParam String cityId) {
|
||||
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<>();
|
||||
params.put("key", apiKey);
|
||||
params.put("cityId", cityId);
|
||||
// 调用第三方API
|
||||
String responseBody = restTemplate.getForObject(apiUrl, String.class);
|
||||
|
||||
try {
|
||||
URL url = new URL(apiUrl + "?" + buildParams(params));
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("GET");
|
||||
int responseCode = connection.getResponseCode();
|
||||
// 创建Result对象并设置数据
|
||||
Result<String> result = new Result<>();
|
||||
result.setCode(200); // 假设API调用成功
|
||||
result.setMsg("查询成功");
|
||||
result.setData(responseBody); // 将API响应作为数据
|
||||
|
||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
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());
|
||||
}
|
||||
// 返回ResponseEntity对象,包含HTTP状态码和响应体
|
||||
return ResponseEntity.ok(result);
|
||||
}
|
||||
|
||||
private String buildParams(Map<String, String> map) {
|
||||
return map.entrySet().stream()
|
||||
.map(entry -> {
|
||||
try {
|
||||
return entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.toString());
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
})
|
||||
.collect(Collectors.joining("&"));
|
||||
}
|
||||
// 如果您还没有RestTemplate Bean,请确保在配置中创建它
|
||||
// 例如,在Spring Boot的配置类中,您可以添加一个@Bean方法来创建RestTemplate
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue