城市数据API微服务(springBoot-city)
项目结构图:
pom文件:
<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>
<groupId>com.csq.study</groupId>
<artifactId>springBoot-city</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
controller层:
package com.csq.study.springboot.controller;
import java.util.List;
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.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.csq.study.springboot.service.CityDataService;
import com.csq.study.springboot.vo.City;
@RestController
@RequestMapping("/cities")
public class CityController {
@Autowired
private CityDataService cityDataService;
@RequestMapping(method=RequestMethod.GET)
public List<City> listCity() throws Exception {
return cityDataService.listCity();
}
}
service接口以及实现类:
接口:
package com.csq.study.springboot.service;
import java.util.List;
import com.csq.study.springboot.vo.City;
public interface CityDataService {
//获取城市列表
List<City> listCity() throws Exception;
}
实现类:
package com.csq.study.springboot.service;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import com.csq.study.springboot.util.XmlBuilder;
import com.csq.study.springboot.vo.City;
import com.csq.study.springboot.vo.CityList;
@Service
public class CityDataServiceImpl implements CityDataService{
@Override
public List<City> listCity() throws Exception {
//读取xml文件
Resource resource=new ClassPathResource("cityList.xml");
BufferedReader br=new BufferedReader(new InputStreamReader(resource.getInputStream(),"utf-8"));
StringBuffer buffer=new StringBuffer();
String line="";
while((line=br.readLine())!=null){
buffer.append(line);
}
br.close();
//xml转为Java对象
CityList cityList= (CityList) XmlBuilder.xmlStrToObject(CityList.class, buffer.toString());
return cityList.getCityList();
}
}
util工具类XmlBuilder :
package com.csq.study.springboot.util;
import java.io.Reader;
import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
public class XmlBuilder {
//将xml转为指定的POJO
public static Object xmlStrToObject(Class<?> clazz,String xmlStr) throws Exception{
Object xmlObject=null;
Reader reader=null;
JAXBContext context=JAXBContext.newInstance(clazz);
//xml转为对象的接口
Unmarshaller unmarshaller=context.createUnmarshaller();
reader=new StringReader(xmlStr);
xmlObject=unmarshaller.unmarshal(reader);
if(reader!=null){
reader.close();
}
return xmlObject;
}
}
vo类:
package com.csq.study.springboot.vo;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
//声明为xml的根元素
@XmlRootElement(name = "d")
//声明xml的访问类型为FIELD(字段)
@XmlAccessorType(XmlAccessType.FIELD)
public class City{
//声明为xml的属性
@XmlAttribute(name = "d1")
private String cityId;
//声明为xml的属性
@XmlAttribute(name = "d2")
private String cityName;
//声明为xml的属性
@XmlAttribute(name = "d3")
private String cityCode;
//声明为xml的属性
@XmlAttribute(name = "d4")
private String province;
public String getCityId() {
return cityId;
}
public void setCityId(String cityId) {
this.cityId = cityId;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public String getCityCode() {
return cityCode;
}
public void setCityCode(String cityCode) {
this.cityCode = cityCode;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
@Override
public String toString() {
return "City [cityId=" + cityId + ", cityName=" + cityName + ", cityCode=" + cityCode + ", province="
+ province + "]";
}
}
citylist类:
package com.csq.study.springboot.vo;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "c")
@XmlAccessorType(XmlAccessType.FIELD)
public class CityList {
@XmlElement(name = "d")
private List<City> cityList;
public List<City> getCityList() {
return cityList;
}
public void setCityList(List<City> cityList) {
this.cityList = cityList;
}
}
快速启动类:
package com.csq.study.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Aplication {
public static void main(String[] args) {
SpringApplication.run(Aplication.class, args);
}
}
配置文件:
citylist.xml
<?xml version="1.0" encoding="UTF-8" ?>
<c c1="0">
<d d1="101010100" d2="北京" d3="beijing" d4="北京"/>
<d d1="101010200" d2="海淀" d3="haidian" d4="北京"/>
<d d1="101010300" d2="朝阳" d3="chaoyang" d4="北京"/>
<d d1="101010400" d2="顺义" d3="shunyi" d4="北京"/>
<d d1="101010500" d2="怀柔" d3="huairou" d4="北京"/>
<d d1="101010600" d2="通州" d3="tongzhou" d4="北京"/>
<d d1="101010700" d2="昌平" d3="changping" d4="北京"/>
<d d1="101010800" d2="延庆" d3="yanqing" d4="北京"/>
<d d1="101010900" d2="丰台" d3="fengtai" d4="北京"/>
<d d1="101011000" d2="石景山" d3="shijingshan" d4="北京"/>
<d d1="101011100" d2="大兴" d3="daxing" d4="北京"/>
<d d1="101011200" d2="房山" d3="fangshan" d4="北京"/>
<d d1="101011300" d2="密云" d3="miyun" d4="北京"/>
<d d1="101011400" d2="门头沟" d3="mentougou" d4="北京"/>
<d d1="101011500" d2="平谷" d3="pinggu" d4="北京"/>
<d d1="101020100" d2="上海" d3="shanghai" d4="上海"/>
<d d1="101020200" d2="闵行" d3="minhang" d4="闵行"/>
<d d1="101020300" d2="宝山" d3="baoshan" d4="上海"/>
<d d1="101020500" d2="嘉定" d3="jiading" d4="上海"/>
<d d1="101020600" d2="南汇" d3="nanhui" d4="上海"/>
<d d1="101020700" d2="金山" d3="jinshan" d4="上海"/>
<d d1="101020800" d2="青浦" d3="qingpu" d4="上海"/>
<d d1="101020900" d2="松江" d3="songjiang" d4="上海"/>
<d d1="101021000" d2="奉贤" d3="fengxian" d4="上海"/>
<d d1="101021100" d2="崇明" d3="chongming" d4="上海"/>
<d d1="101021200" d2="徐家汇" d3="xujiahui" d4="上海"/>
<d d1="101021300" d2="浦东" d3="pudong" d4="上海"/>
</c>
访问:http://localhost:8082/cities 可看到如下截图: