##前言最近因人们一大堆乱七八糟的需求,所以需要一个天气查询,有时候也不明为什么会有这么一大堆废话本人写文章不太喜欢这么多话,所以我总结了一下几点.......
返回数据
目录结构
WeatherConfig
@Configuration
public class WeatherConfig {
@Bean
public RestTemplate restTemplate(){
RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
return restTemplate;
}
}
WeatherService
String tide(@PathVariable("city")String city);
String port(@PathVariable("city")String city,@PathVariable("port")String port);
Impl
@Service
public class WeatherServiceImpl implements WeatherService {
@Autowired
private RestTemplate restTemplate;
@Override
public String tide(@PathVariable("city")String city) {
String apiUrl = "你的天气api" + 查询的城市;
ResponseEntity forEntity = restTemplate.getForEntity(apiUrl, String.class);
if (200 == forEntity.getStatusCodevalue()) {
return forEntity.getBody();
} else {
return "error with code : " + forEntity.getStatusCodevalue();
}
}
controller
@RestController
@RequestMapping("/weather/")
public class queryWeatherController {
@Autowired
private WeatherServiceImpl weatherService;
@RequestMapping(value = "/tide/{city}",method = RequestMethod.GET)
public String tied(@PathVariable("city")String city) {
return weatherService.tide(city);
}
总结:复制进去即可使用,记得填写自己的天气Api,然后如果想要多几个,就复制service,跟Impl,改个方法名即可,controller多个请求就好了. 如果有大佬可以对我做出指点,那我将对您感激涕零。



