# code-with-quarkus **Repository Path**: light_shadow/code-with-quarkus ## Basic Information - **Project Name**: code-with-quarkus - **Description**: No description available - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-03-07 - **Last Updated**: 2022-03-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # code-with-quarkus Project ## 使用说明 http://localhost:8080/weather/city/北京
http://localhost:8080/weather/city/async/北京 分别会显示如下的北京的天气,都为json格式 ```json { "data": { "yesterday": { "date": "7日星期一", "high": "高温 16℃", "fx": "北风", "low": "低温 1℃", "fl": "", "type": "晴" }, "city": "北京", "forecast": [ { "date": "8日星期二", "high": "高温 17℃", "fengli": "", "low": "低温 2℃", "fengxiang": "东北风", "type": "晴" }, { "date": "9日星期三", "high": "高温 20℃", "fengli": "", "low": "低温 4℃", "fengxiang": "东北风", "type": "霾" }, { "date": "10日星期四", "high": "高温 15℃", "fengli": "", "low": "低温 5℃", "fengxiang": "东北风", "type": "晴" }, { "date": "11日星期五", "high": "高温 13℃", "fengli": "", "low": "低温 4℃", "fengxiang": "东南风", "type": "阴" }, { "date": "12日星期六", "high": "高温 13℃", "fengli": "", "low": "低温 4℃", "fengxiang": "东风", "type": "多云" } ], "ganmao": "感冒易发期,外出请适当调整衣物,注意补充水分。", "wendu": 6 }, "desc": "OK", "status": 1000 } ``` ## 实现说明 此项目是从quarkus官网生成的模板,加以自己的改造完成quarkus 使用multiny的功能。
参考博客如下:
https://quarkus.io/guides/rest-client
主要实现类 ``` org.acme.rest.client.weather.WeatherResource ``` 这个类中实现了两个接口, 一个是 http://localhost:8080/weather/city/%E5%8C%97%E4%BA%AC 访问的同步接口 另一个是 http://localhost:8080/weather/city/async/%E5%8C%97%E4%BA%AC 访问的异步接口 返回的格式都是json格式,其接口返回的是string格式,在代码内部进行了反序列化,同步接口中直接序列化,异步接口中进行了Uni的转换,代码如下: ``` return Uni.createFrom().completionStage(weatherService.getByCityAsync(city)).onItem() .transform((Function) weatherCity -> { ResponseEntity responseEntity = new ResponseEntity(); try { responseEntity = objectMapper.readValue(weatherCity, ResponseEntity.class); } catch (JsonProcessingException e) { e.printStackTrace(); } return responseEntity; }); ``` 通过 io.smallrye.mutiny.groups.UniOnItem#transform 进行了Uni类型的转换。 ## 遇到及发现的问题 #### 返回的数据中文一直乱码 通过debug发现了接口请求返回就是乱码的,通过观察和分析发现了原接口请求上带有一个如下的请求头
```properties Accept-Encoding: gzip, deflate ``` 参考了 https://quarkus.io/guides/rest-json#gzip-support 这篇博客中 GZip Support部分, 在 application.properties 文件中加入了对于gzip的支持如下: ```properties quarkus.resteasy.gzip.enabled=true ``` 重新运行,中文已经显示正常了。 ## native本地化运行 ### 文件位于 src的lib包下面(win10 系统打包的native镜像) ``` code-with-quarkus-1.0.0-SNAPSHOT-runner.exe ``` 直接在终端或cmd中运行 ```properties ./code-with-quarkus-1.0.0-SNAPSHOT-runner.exe ``` 即可,然后按照使用说明的方式请求,即会看到天气的响应json数据 ### 参考文章 #### 官方文档 https://quarkus.io/guides/building-native-image #### 个人博客(环境搭建) https://cdmana.com/2021/01/20210110154759607z.html