开发环境

  • JDK8+
  • Gradle4+
  • Spring Boot Web Starter

创建项目

新建项目文件夹:
Spring Cloud学习笔记11——天气预报系统微服务(5)城市数据 API 微服务
micro-weather-report项目中的源码文件复制粘贴到新项目文件夹中:
Spring Cloud学习笔记11——天气预报系统微服务(5)城市数据 API 微服务

修改源码

修改build.gradle配置,删除HttpClientredisquartzthymeleaf的依赖:

//依赖关系
dependencies {

    //该依赖用于编译阶段
	compile('org.springframework.boot:spring-boot-starter-web')

    //该依赖用于测试阶段
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

com.study.spring.cloud.weather.controller包下新建类CityController

package com.study.spring.cloud.weather.controller;

import com.study.spring.cloud.weather.service.CityDataService;
import com.study.spring.cloud.weather.vo.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//用于处理rest请求的controller
@RestController
@RequestMapping("/cities")
public class CityController {

	@Autowired
	private CityDataService cityDataService;

	@GetMapping
	public List<City> listCity() throws Exception {
		return cityDataService.listCity();
	}
	
}

Spring Cloud学习笔记11——天气预报系统微服务(5)城市数据 API 微服务
Spring Cloud学习笔记11——天气预报系统微服务(5)城市数据 API 微服务
修改application.properties配置文件,将

#热部署静态文件
spring.thymeleaf.cache=false

内容删除

此时src目录结构如下:
Spring Cloud学习笔记11——天气预报系统微服务(5)城市数据 API 微服务

运行

注意一定要先运行Redis

运行应用:
Spring Cloud学习笔记11——天气预报系统微服务(5)城市数据 API 微服务
运行结果如下:
Spring Cloud学习笔记11——天气预报系统微服务(5)城市数据 API 微服务
访问http://localhost:8080/cities页面:
Spring Cloud学习笔记11——天气预报系统微服务(5)城市数据 API 微服务

相关文章:

  • 2021-04-14
  • 2021-11-28
  • 2021-11-10
  • 2021-08-27
  • 2022-12-23
  • 2021-12-02
  • 2021-09-18
  • 2022-01-01
猜你喜欢
  • 2021-05-23
  • 2022-01-09
  • 2021-12-24
  • 2021-12-12
  • 2021-11-18
  • 2021-09-20
  • 2021-11-28
相关资源
相似解决方案