开发环境
JDK8+Gradle4+Spring Boot Web Starter
创建项目
新建项目文件夹:
将micro-weather-report项目中的源码文件复制粘贴到新项目文件夹中:
修改源码
修改build.gradle配置,删除HttpClient、redis、quartz、thymeleaf的依赖:
//依赖关系
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();
}
}
修改application.properties配置文件,将
#热部署静态文件
spring.thymeleaf.cache=false
内容删除
此时src目录结构如下:
运行
注意一定要先运行Redis
运行应用:
运行结果如下:
访问http://localhost:8080/cities页面: