利用springboot快速实现一个查询数据系统(一)
上班的时候,公司负责人给我说让我看下springboot和springcloud,后面项目也许会用到,我就查看相关资料,然后结合资料自己动手写了下代码,从中出现一些问题已经解决一些问题。参考书籍:柳伟卫编著的书籍《SpringCloud微服务架构开发实战》首先我们需要找到天气预报的数据源:通过网上查找,有免费的,可用的天气数据接口:有两种方式:通过城市名称获取天气数据信息: "ht...
上班的时候,公司负责人给我说让我看下springboot和springcloud,后面项目也许会用到,我就查看相关资料,然后结合资料自己动手写了下代码,从中出现一些问题已经解决一些问题。
参考书籍:柳伟卫编著的书籍《SpringCloud微服务架构开发实战》
首先我们需要找到天气预报的数据源:通过网上查找,有免费的,可用的天气数据接口:
有两种方式:
通过城市名称获取天气数据信息: "http://wthrcdn.etouch.cn/weather_mini?city=城市名称
通过城市ID获取天气数据信息: "http://wthrcdn.etouch.cn/weather_mini?citykey=城市编号ID
一。首先放出我的项目的结构:
二。创建springboot项目,并添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
三。创建用到的相关对象vo
天气信息类对象:Weather
package com.yian.springboot.vo;
import java.io.Serializable;
import java.util.List;
/**
*
* @Description: 天气信息类对象
* @ClassName: Weather
* @author chengshengqing-YiAn 2019年2月28日 上午10:02:48
* @see TODO
*/
public class Weather implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String city;
private String aqi;//
private String wendu;
private String ganmao;
private Yesterday yesterday;
private List<Forecast> forecast;
public String getGanmao() {
return ganmao;
}
public void setGanmao(String ganmao) {
this.ganmao = ganmao;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getAqi() {
return aqi;
}
public void setAqi(String aqi) {
this.aqi = aqi;
}
public String getWendu() {
return wendu;
}
public void setWendu(String wendu) {
this.wendu = wendu;
}
public Yesterday getYesterday() {
return yesterday;
}
public void setYesterday(Yesterday yesterday) {
this.yesterday = yesterday;
}
public List<Forecast> getForecast() {
return forecast;
}
public void setForecast(List<Forecast> forecast) {
this.forecast = forecast;
}
@Override
public String toString() {
return "Weather [city=" + city + ", aqi=" + aqi + ", wendu=" + wendu + ", yesterday="
+ yesterday + ", forecast=" + forecast + "]";
}
}
2。昨日天气信息类:Yesterday
package com.yian.springboot.vo;
import java.io.Serializable;
/**
*
* @Description: 昨日天气信息类
* @ClassName: Yesterday
* @author chengshengqing-YiAn 2019年2月28日 上午10:04:29
* @see TODO
*/
public class Yesterday implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String date;
private String high;
private String fx;
private String low;
private String fl;
private String type;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getHigh() {
return high;
}
public void setHigh(String high) {
this.high = high;
}
public String getFx() {
return fx;
}
public void setFx(String fx) {
this.fx = fx;
}
public String getLow() {
return low;
}
public void setLow(String low) {
this.low = low;
}
public String getFl() {
return fl;
}
public void setFl(String fl) {
this.fl = fl;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "Yesterday [date=" + date + ", high=" + high + ", fx=" + fx + ", low=" + low + ", fl=" + fl + ", type="
+ type + "]";
}
}
3。未来天气信息类:Forecast
package com.yian.springboot.vo;
import java.io.Serializable;
/**
*
* @Description: 未来天气信息类:Forecast
* @ClassName: Forecast
* @author chengshengqing-YiAn 2019年2月28日 上午10:05:25
* @see TODO
*/
public class Forecast implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String date;//琪日期
private String high;//最高温度
private String fengxiang;//风向
private String low;//最低温度
private String fengli;//风力
private String type;//天气类型
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getHigh() {
return high;
}
public void setHigh(String high) {
this.high = high;
}
public String getFengxiang() {
return fengxiang;
}
public void setFengxiang(String fengxiang) {
this.fengxiang = fengxiang;
}
public String getLow() {
return low;
}
public void setLow(String low) {
this.low = low;
}
public String getFengli() {
return fengli;
}
public void setFengli(String fengli) {
this.fengli = fengli;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "Forecast [date=" + date + ", high=" + high + ", fengxiang=" + fengxiang + ", low=" + low + ", fengli="
+ fengli + ", type=" + type + "]";
}
}
4. 消息返回对象:WeatherResponse
package com.yian.springboot.vo;
import java.io.Serializable;
/**
*
* @Description: 消息返回对象:WeatherResponse
* @ClassName: WeatherResponse
* @author chengshengqing-YiAn 2019年2月28日 上午10:06:35
* @see TODO
*/
public class WeatherResponse implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private Weather data; //消息数据
private String status; //消息状态
private String desc;//消息描述
public Weather getData() {
return data;
}
public void setData(Weather data) {
this.data = data;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Override
public String toString() {
return "WeatherResponse [data=" + data + ", status=" + status + ", desc=" + desc + "]";
}
}
四:创建根据城市名称和ID查询天气的接口
package com.yian.springboot.service;
import com.yian.springboot.vo.WeatherResponse;
/**
*
* @Description: 根据城市和城市ID查询天气的接口
* @ClassName: WeatherDateService
* @author chengshengqing-YiAn 2019年2月28日 上午10:20:02
* @see TODO
*/
public interface WeatherDateService {
//根据城市id查询天气
WeatherResponse getDateByCityId(String cityId);
//根据城市名字查询天气
WeatherResponse getDateByCityName(String cityName);
}
五:创建实现类实现这个接口
package com.yian.springboot.service.Impl;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yian.springboot.service.WeatherDateService;
import com.yian.springboot.vo.WeatherResponse;
@Service
public class WeatherDateServiceImpl implements WeatherDateService{
@Autowired
private RestTemplate restTemplate;
private final String WEATHER_API="http://wthrcdn.etouch.cn/weather_mini";
@Override
public WeatherResponse getDateByCityId(String cityId) {
String uri=WEATHER_API+"?citykey="+cityId;
return this.doGetWeatherData(uri);
}
@Override
public WeatherResponse getDateByCityName(String cityName) {
String uri=WEATHER_API+"?city="+cityName;
return this.doGetWeatherData(uri);
}
private WeatherResponse doGetWeatherData(String uri) {
ResponseEntity<String> response=restTemplate.getForEntity(uri, String.class);
String strBody=null;
if(response.getStatusCodeValue()==200){
strBody=response.getBody();
}
//jackson库中的类
ObjectMapper mapper= new ObjectMapper();
WeatherResponse weather=null;
try {
//mapper.readValue是将json结构转换成成java对象
//mapper.writeValue是将java对象转成json结构
weather=mapper.readValue(strBody, WeatherResponse.class);
} catch (IOException e) {
e.printStackTrace();
}
return weather;
}
}
六:控制层代码
package com.yian.springboot.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yian.springboot.service.WeatherDateService;
import com.yian.springboot.vo.WeatherResponse;
@SpringBootApplication
@RestController
@ComponentScan(basePackages={"com.yian.springboot.service","com.yian.springboot.config"})
@RequestMapping("/weather")
public class WeatherController {
@Autowired
private WeatherDateService weatherDateService;
@RequestMapping("/cityId/{cityId}")
public WeatherResponse getReportByCityId( @PathVariable("cityId") String cityId){
return weatherDateService.getDateByCityId(cityId);
}
@RequestMapping("/cityName/{cityName}")
public WeatherResponse getReportByCityName( @PathVariable("cityName") String cityName){
return weatherDateService.getDateByCityName(cityName);
}
public static void main(String[] args) {
SpringApplication.run(WeatherController.class, args);
}
}
七:配置类
package com.yian.springboot.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* 配置类
* @Description: TODO
* @ClassName: RestConfiguration
* @author chengshengqing-YiAn 2019年2月28日 上午10:25:38
* @see TODO
*/
@Configuration
public class RestConfiguration {
@Autowired
private RestTemplateBuilder builder;
@Bean
public RestTemplate restTemplate(){
return builder.build();
}
}
八:启动项目,访问:http://localhost:8080/weather/cityId/101280603以后出现下面截图,证明访问成功。
访问:http://localhost:8080/weather/cityName/福田区会出现以下截图,也说明访问成功。
注意:在这个项目启动的时候,报错了如下面截图所示:
据英文的提示是在配置中找不到一个指定自动注入类型的bean,经过多方排查得出结论:
正常情况下加上@Component注解的类会自动被Spring扫描到生成Bean注册到spring容器中,既然他说没找到,也就是该注解被没有被spring识别,问题的核心关键就在application类的注解SpringBootApplication上
解决办法:
1 .将接口与对应的实现类放在与application启动类的同一个目录或者他的子目录下,这样注解可以被扫描到,这是最省事的办法
2 .在指定的application类上加上这么一行注解,手动指定application类要扫描哪些包下的注解
我当时的做法就是加了下面这个注解:问题解决了。
//@ComponentScan(basePackages={"com.yian.springboot.service","com.yian.springboot.config"})
劳动不易:转载请注明出处:https://blog.csdn.net/FindHuni/article/details/88016970
再次感谢:柳伟卫编著的书籍《SpringCloud微服务架构开发实战》
更多推荐
所有评论(0)