springboot应用查询城市天气

简介: 本文的实战是开发一个springboot应用,通过RestTemplate获取公共的远程api服务,将查询到的指定城市的天气信息返回给前端

欢迎访问我的GitHub

这里分类和汇总了欣宸的全部原创(含配套源码): https://github.com/zq2599/blog_demos

本篇概览

  • 本文的实战是开发一个springboot应用,通过RestTemplate获取公共的远程api服务,将查询到的指定城市的天气信息返回给前端;

创建springboot应用

  • 基于maven创建一个springboot应用,pom信息如下,注意添加了httpclient
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bolingcavalry</groupId>
    <artifactId>weatherservice</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>weatherservice</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.7</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  • 启动类如下:
package com.bolingcavalry.weatherservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class WeatherserviceApplication {
    public static void main(String[] args) {
        SpringApplication.run(WeatherserviceApplication.class, args);
    }
}

创建配置类

  • 创建一个配置类,这里注意要使用HttpComponentsClientHttpRequestFactory作为RestTemplate构造方法的入参,这样才能支持gzip压缩的response,否则得到的就是乱码:
package com.bolingcavalry.weatherservice;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.StandardCharsets;


@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;
    }
}

创建controller

  • 创建一个controller来接收web请求,然后发起气象服务网站发起天气查询:
package com.bolingcavalry.weatherservice.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class QueryWeather {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping(value = "/get/{city}", method = RequestMethod.GET)
    public String extern(@PathVariable("city") String city){
        String apiURL = "http://wthrcdn.etouch.cn/weather_mini?city=" + city;
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(apiURL, String.class);

        if(200==responseEntity.getStatusCodeValue()){
            return responseEntity.getBody();
        }else{
            return "error with code : " + responseEntity.getStatusCodeValue();
        }
    }
}

验证

  • 启动应用WeatherserviceApplication,假如服务器IP地址是172.30.192.1,浏览器响应如下图,地址是:http://172.30.192.1:8080/get/深圳

在这里插入图片描述

源码下载

  • 接下来的实战是编写Flink应用的源码,您可以选择直接从GitHub下载这个工程的源码,地址和链接信息如下表所示:
名称 链接 备注
项目主页 https://github.com/zq2599/blog_demos 该项目在GitHub上的主页
git仓库地址(https) https://github.com/zq2599/blog_demos.git 该项目源码的仓库地址,https协议
git仓库地址(ssh) git@github.com:zq2599/blog_demos.git 该项目源码的仓库地址,ssh协议
  • 这个git项目中有多个文件夹,本章源码在flinkkafkademo这个文件夹下,如下图红框所示:

在这里插入图片描述

欢迎关注51CTO博客:程序员欣宸

学习路上,你不孤单,欣宸原创一路相伴...
相关文章
|
2月前
|
分布式计算 数据可视化 大数据
于SpringBoot+大数据城市景观画像可视化设计和实现
于SpringBoot+大数据城市景观画像可视化设计和实现
|
1月前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的武汉市公交路线查询系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的武汉市公交路线查询系统的详细设计和实现(源码+lw+部署文档+讲解等)
|
1月前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的城市公交在线查询系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的城市公交在线查询系统的详细设计和实现(源码+lw+部署文档+讲解等)
|
2月前
|
JavaScript Java 测试技术
基于SpringBoot+Vue的公共交通查询系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue的公共交通查询系统的详细设计和实现(源码+lw+部署文档+讲解等)
13 1
基于SpringBoot+Vue的公共交通查询系统的详细设计和实现(源码+lw+部署文档+讲解等)
|
1月前
|
Java 关系型数据库 API
使用Spring Boot和PostgreSQL构建高级查询
使用Spring Boot和PostgreSQL构建高级查询
|
2月前
|
JavaScript Java 测试技术
基于SpringBoot+Vue的城市郊野公园管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue的城市郊野公园管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
22 3
|
2月前
|
JavaScript Java 测试技术
基于SpringBoot+Vue的自驾游攻略查询系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue的自驾游攻略查询系统的详细设计和实现(源码+lw+部署文档+讲解等)
35 3
|
2月前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的即时空教室查询小程序的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的即时空教室查询小程序的详细设计和实现(源码+lw+部署文档+讲解等)
|
1月前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的企业职工薪资查询系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的企业职工薪资查询系统的详细设计和实现(源码+lw+部署文档+讲解等)
|
1月前
|
JavaScript Java 测试技术
基于springboot+vue.js+uniapp的公共交通查询系统附带文章源码部署视频讲解等
基于springboot+vue.js+uniapp的公共交通查询系统附带文章源码部署视频讲解等
19 0