Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit95a71ef

Browse files
committed
update
1 parent63e1466 commit95a71ef

File tree

5 files changed

+38
-10
lines changed

5 files changed

+38
-10
lines changed

‎SUMMARY.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*[微服务架构概述](docs/msa-boot/msa-boot-overview.md)
1010
*[微服务 API 设计原则](docs/msa-boot/msa-boot-api.md)
1111
*[微服务存储设计原则](docs/msa-boot/msa-boot-store.md)
12-
*[基于 Spring Boot 的一个微服务实现](docs/msa-boot/first-api.md)
12+
*[基于 Spring Boot 的一个微服务实现](docs/msa-boot/first-api.md)(ing...)
1313
*[微服务的注册与发现](docs/register-discover/register-discover.md)
1414
*[服务发现的意义](docs/register-discover/discover-meaning.md)
1515
*[如何集成Eureka](docs/register-discover/eureka.md)

‎docs/msa-boot/first-api.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@
121121

122122

123123

124+
##访问API
125+
126+
*<http://localhost:8080/weather/cityId/101280601>
127+
*<http://localhost:8080/weather/cityName/惠州>
124128

125129
##参考
126130

‎samples/micro-weather-basic/src/main/java/com/waylau/spring/cloud/controller/WeatherController.java‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
importorg.springframework.beans.factory.annotation.Autowired;
77
importorg.springframework.web.bind.annotation.GetMapping;
8+
importorg.springframework.web.bind.annotation.PathVariable;
89
importorg.springframework.web.bind.annotation.RequestMapping;
9-
importorg.springframework.web.bind.annotation.RequestParam;
1010
importorg.springframework.web.bind.annotation.RestController;
1111

1212
importcom.waylau.spring.cloud.service.WeatherDataService;
@@ -24,8 +24,13 @@ public class WeatherController {
2424
privateWeatherDataServiceweatherDataService;
2525

2626
@GetMapping("/cityId/{cityId}")
27-
publicWeatherResponsegetReportByCityId(@RequestParam(value ="name",required =false,defaultValue ="101280601")StringcityId) {
27+
publicWeatherResponsegetReportByCityId(@PathVariable("cityId")StringcityId) {
2828
returnweatherDataService.getDataByCityId(cityId);
2929
}
30+
31+
@GetMapping("/cityName/{cityName}")
32+
publicWeatherResponsegetReportByCityName(@PathVariable("cityName")StringcityName) {
33+
returnweatherDataService.getDataByCityName(cityName);
34+
}
3035

3136
}

‎samples/micro-weather-basic/src/main/java/com/waylau/spring/cloud/service/WeatherDataServiceImpl.java‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
*/
44
packagecom.waylau.spring.cloud.service;
55

6+
importjava.io.IOException;
7+
68
importorg.springframework.beans.factory.annotation.Autowired;
79
importorg.springframework.http.ResponseEntity;
810
importorg.springframework.stereotype.Service;
911
importorg.springframework.web.client.RestTemplate;
1012

13+
importcom.fasterxml.jackson.databind.ObjectMapper;
1114
importcom.waylau.spring.cloud.vo.WeatherResponse;
1215

1316
/**
@@ -25,16 +28,33 @@ public class WeatherDataServiceImpl implements WeatherDataService {
2528
@Override
2629
publicWeatherResponsegetDataByCityId(StringcityId) {
2730
Stringuri ="http://wthrcdn.etouch.cn/weather_mini?citykey=" +cityId;
28-
29-
ResponseEntity<WeatherResponse>response =restTemplate.getForEntity(uri,WeatherResponse.class);
30-
//ResponseEntity<String> response = restTemplate.getForEntity(uri, String.class);
31-
returnnull;
31+
returnthis.doGetWeatherData(uri);
3232
}
3333

3434
@Override
3535
publicWeatherResponsegetDataByCityName(StringcityName) {
36-
// TODO Auto-generated method stub
37-
returnnull;
36+
Stringuri ="http://wthrcdn.etouch.cn/weather_mini?city=" +cityName;
37+
returnthis.doGetWeatherData(uri);
38+
}
39+
40+
privateWeatherResponsedoGetWeatherData(Stringuri) {
41+
ResponseEntity<String>response =restTemplate.getForEntity(uri,String.class);
42+
StringstrBody =null;
43+
44+
if (response.getStatusCodeValue() ==200) {
45+
strBody =response.getBody();
46+
}
47+
48+
ObjectMappermapper =newObjectMapper();
49+
WeatherResponseweather =null;
50+
51+
try {
52+
weather =mapper.readValue(strBody,WeatherResponse.class);
53+
}catch (IOExceptione) {
54+
e.printStackTrace();
55+
}
56+
57+
returnweather;
3858
}
3959

4060
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
spring.http.converters.preferred-json-mapper=gson

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp