springboot应用查询城市天气

版权声明:欢迎转载,请注明出处,谢谢。 https://blog.csdn.net/boling_cavalry/article/details/86770023

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

创建springboot应用

  1. 基于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>
  1. 启动类如下:
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这个文件夹下,如下图红框所示:
在这里插入图片描述

没有更多推荐了,返回首页