初始化
parent
438e06429e
commit
46879ee349
|
@ -1,41 +1,33 @@
|
||||||
package com.muyu.market.controller;
|
package com.muyu.market.controller;
|
||||||
|
|
||||||
import com.muyu.common.core.domain.Result;
|
import com.muyu.common.core.domain.Result;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import java.io.BufferedReader;
|
||||||
import org.springframework.web.client.RestTemplate;
|
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;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/third")
|
@RequestMapping("/third")
|
||||||
public class ThirdPartyController {
|
public class ThirdPartyController {
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private RestTemplate restTemplate;
|
|
||||||
|
|
||||||
@GetMapping("/getAirQuality")
|
@GetMapping("/getAirQuality")
|
||||||
public ResponseEntity<Result<String>> getAirQuality(@RequestParam String cityId) {
|
public ModelAndView getAirQuality(@Validated @RequestParam String cityId, HttpServletResponse httpResponse) {
|
||||||
String apiKey = "02ccc3ea04810432b45399ecb183195d";
|
String apiKey = "02ccc3ea04810432b45399ecb183195d";
|
||||||
String apiUrl = "http://apis.juhe.cn/fapigw/air/historyHours?key=" + apiKey + "&cityId=" + cityId;
|
String apiUrl = "http://apis.juhe.cn/fapigw/air/historyHours?key="+apiKey+"&cityId="+cityId;
|
||||||
|
return new ModelAndView(apiUrl);
|
||||||
// 调用第三方API
|
|
||||||
String responseBody = restTemplate.getForObject(apiUrl, String.class);
|
|
||||||
|
|
||||||
// 创建Result对象并设置数据
|
|
||||||
Result<String> result = new Result<>();
|
|
||||||
result.setCode(200); // 假设API调用成功
|
|
||||||
result.setMsg("查询成功");
|
|
||||||
result.setData(responseBody); // 将API响应作为数据
|
|
||||||
|
|
||||||
// 返回ResponseEntity对象,包含HTTP状态码和响应体
|
|
||||||
return ResponseEntity.ok(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果您还没有RestTemplate Bean,请确保在配置中创建它
|
|
||||||
// 例如,在Spring Boot的配置类中,您可以添加一个@Bean方法来创建RestTemplate
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue