1.引入依赖
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
启动类添加:
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.securitySchemes(securitySchemes())
.securityContexts(securityContexts())
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.cennavi.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("接口测试文档--Powerby ")
.description("用于开发阶段提供测试")
.termsOfServiceUrl("")
.version("1.0")
.build();
}
/**
* swagger加入全局Authorization header 将在ui界面右上角新增token输入界面
* @return
*/
private List<ApiKey> securitySchemes() {
ApiKey apiKey = new ApiKey("token", "token", "query");
ArrayList arrayList = new ArrayList();
arrayList.add(apiKey);
return arrayList;
}
/**
* 在Swagger2的securityContexts中通过正则表达式,设置需要使用参数的接口(或者说,是去除掉不需要使用参数的接口),
* 如下列代码所示,通过PathSelectors.regex("^(?!auth).*$"),
* 所有包含"auth"的接口不需要使用securitySchemes。即不需要使用上文中设置的名为“Authorization”,
* type为“header”的参数。
*
*/
private List<SecurityContext> securityContexts() {
SecurityContext build = SecurityContext.builder()
.securityReferences(defaultAuth())
.forPaths(PathSelectors.any())
.build();
ArrayList arrayList = new ArrayList();
arrayList.add(build);
return arrayList;
}
List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
SecurityReference authorization = new SecurityReference("token", authorizationScopes);
ArrayList arrayList = new ArrayList<>();
arrayList.add(authorization);
return arrayList;
}
controller添加
package com.cennavi.controller.resourcesearch;
import com.alibaba.fastjson.JSONObject;
import com.cennavi.bean.KakouPosition;
import com.cennavi.iservice.IKafkaSwanVehicleService;
import com.cennavi.iservice.IKakouPositionService;
import com.cennavi.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.*;
@EnableAutoConfiguration
@RestController
@ResponseBody
@RequestMapping("source-search")
@Api(value = "车辆过卡口数据查询", tags = "车辆过卡口数据查询")
public class BayonetSearchController
{
@Autowired
IKafkaSwanVehicleService kafkaSwanVehicleService;
@Autowired
IKakouPositionService kakouPositionService;
@Value("${lbs.url}")
String lbsurl;
@Value("${lbs.appKey}")
String lbsappKey;
DateUtil dateUtil = new DateUtil();
@Value("${params.bayonelisturl}")
String bayonelisturl;
@PostMapping("/bayonets")
@ApiOperation(value = "所有卡口位置信息",notes = "所有卡口位置信息")
@ApiImplicitParams({
@ApiImplicitParam(value = "当前页码", name = "PageIndex"),
@ApiImplicitParam(value = "页大小", name = "PageSize"),
@ApiImplicitParam(value = "登录鉴权", name = "token")
})
public Object Query(
@RequestParam(value = "PageIndex", required = false) Integer PageIndex,
@RequestParam(value = "PageSize", required = false) Integer PageSize,
@RequestParam(value = "token", required = false) String token
)
{
try
{
Map<String,Object> m=new LinkedHashMap<>();
m.put("TOTAL",null);
m.put("list",null);
Map<String,Object> map=new LinkedHashMap<>();
map.put("code",200);
map.put("data",m);
if (PageSize != null && PageSize > 0){
Long count = kakouPositionService.ExecuteScalar();
List<KakouPosition> list = kakouPositionService.Query(PageIndex, PageSize);
m.put("TOTAL",list.size());
m.put("list",list);
m.put("PageIndex",PageIndex);
m.put("PageSize",PageSize);
map.put("TotalPage",((count / PageSize) + (count % PageSize > 0 ? 1 : 0)));
map.put("DataCount",count);
return map;
}else {
List<KakouPosition> list = kakouPositionService.Query(PageIndex, PageSize);
m.put("TOTAL",list.size());
m.put("list",list);
return map;
}
}
catch (Exception ex)
{
ex.printStackTrace();
return new RRException(RongRunErrorCodeEnum.SYSTEM_ERROR).getCodeMsg();
}
}
@ApiImplicitParams({
@ApiImplicitParam(value = "过车时间", name = "gcsj", required = false),
@ApiImplicitParam(value = "机动车号牌号码", name = "sb03_jdchphm", required = false),
@ApiImplicitParam(value = "页码(默认值为1)", name = "pageNum", required = false, defaultValue = "1"),
@ApiImplicitParam(value = "页数(默认值为10)", name = "pageSize", required = false, defaultValue = "10"),
@ApiImplicitParam(value = "登录后返回的鉴权信息", name = "token")
})
@PostMapping("bayonelist_zw")
@ApiOperation(value = "车辆过卡口列表检索(中奥原始接口)", notes = "车辆过卡口列表检索(中奥原始接口)")
public Object BayonetListSearch_zw(
@RequestParam(value = "gcsj", required = false) String gcsj,
@RequestParam(value = "sb03_jdchphm", required = false) String sb03_jdchphm,
@RequestParam(value = "pageNum", required = false) Integer pageNum,
@RequestParam(value = "pageSize", required = false) Integer pageSize,
@RequestParam(value = "token", required = false) String token,
HttpServletRequest request
)
{
try
{
Map<String, Object> map = new FitDataUtil().FitData(request);
if (StringUtils.isEmpty(pageNum))
{
pageNum = 1;
}
if (StringUtils.isEmpty(pageSize))
{
pageSize = 10;
}
if (!StringUtils.isEmpty(gcsj))
{
map.put("gcsj", new Timestamp(dateUtil.CreateDate(gcsj).getTime()));
}
map.put("pageSize", pageSize);
map.put("pageNum", pageNum);
return ZAHttpRequest.setPost(bayonelisturl, map);
}
catch (Exception ex)
{
ex.printStackTrace();
return new RRException(RongRunErrorCodeEnum.SYSTEM_ERROR).getCodeMsg();
}
}
@ApiImplicitParams({
@ApiImplicitParam(value = "开始时间",name = "starttime",required = false),
@ApiImplicitParam(value = "结束时间",name = "endtime", required = false),
@ApiImplicitParam(value = "机动车号牌号码", name = "sb03_jdchphm", required = false),
@ApiImplicitParam(value = "页码(默认值为1)", name = "pageNum", required = false, defaultValue = "1"),
@ApiImplicitParam(value = "页数(默认值为10)", name = "pageSize", required = false, defaultValue = "10"),
@ApiImplicitParam(value = "登录后返回的鉴权信息", name = "token")
})
@PostMapping("bayonelist_zw_f")
@ApiOperation(value = "车辆过卡口列表检索(中奥原始接口全量数据过滤)", notes = "车辆过卡口列表检索(中奥原始接口全量数据过滤)")
public Object BayonetListSearch_f(
@RequestParam(value = "sb03_jdchphm", required = false) String sb03_jdchphm,
@RequestParam(value = "starttime", required = false) String starttime,
@RequestParam(value = "endtime", required = false) String endtime,
@RequestParam(value = "pageNum", required = false) Integer pageNum,
@RequestParam(value = "pageSize", required = false) Integer pageSize,
@RequestParam(value = "token", required = false) String token,
HttpServletRequest request
)
{
try
{
Map<String, Object> map = new FitDataUtil().FitData(request);
if (StringUtils.isEmpty(pageNum))
{
pageNum = 1;
}
if (StringUtils.isEmpty(pageSize))
{
pageSize = 10;
}
map.put("pageSize", pageSize);
map.put("pageNum", pageNum);
Object o = ZAHttpRequest.setPost(bayonelisturl, map);
if (!StringUtils.isEmpty(o))
{
o= JSONObject.parse(o.toString());
}
List<Map<String, Object>> l = new LinkedList<>();
Map<String, Object> rm = new LinkedHashMap<>();
Map<String, Object> d = new LinkedHashMap<>();
d.put("TOTAL", 0);
d.put("list", l);
rm.put("code", null);
rm.put("data", d);
rm.put("message", null);
rm.put("serverTime", null);
if (StringUtils.isEmpty(starttime) && StringUtils.isEmpty(endtime))
{
return o;
}
else if (!StringUtils.isEmpty(starttime) && !StringUtils.isEmpty(endtime))
{
if (o instanceof Map)
{
rm.put("code", ((Map<String, Object>) o).get("code"));
rm.put("message", ((Map<String, Object>) o).get("message"));
rm.put("serverTime", ((Map<String, Object>) o).get("serverTime"));
if (((Map<String, Object>) o).get("code").toString().equals("200"))
{
if (null != ((Map<String, Object>) o).get("data"))
{
if (null != ((Map<String, Object>) (((Map<String, Object>) o).get("data"))).get("list"))
{
List<Map<String, Object>> list = ((List) ((Map<String, Object>) (((Map<String, Object>) o).get("data"))).get("list"));
for (Map<String, Object> m : list)
{
Long t = Timestamp.valueOf(m.get("gcsj").toString()).getTime();
Long s = dateUtil.CreateDate(starttime).getTime();
Long e = dateUtil.CreateDate(endtime).getTime();
if (t >= s && t <= e)
{
l.add(m);
}
}
d.put("TOTAL", l.size());
}
}
}
}
Collections.sort(l, new Comparator<Map<String, Object>>()
{
@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2)
{
Long t1 = Timestamp.valueOf(o1.get("gcsj").toString()).getTime();
Long t2 = Timestamp.valueOf(o2.get("gcsj").toString()).getTime();
if ((t1 - t2) > 0)
{
return 1;
}
else if ((t1 - t2) < 0)
{
return -1;
}
return 0;
}
});
return rm;
}
else if (!StringUtils.isEmpty(starttime))
{
if (o instanceof Map)
{
rm.put("code", ((Map<String, Object>) o).get("code"));
rm.put("message", ((Map<String, Object>) o).get("message"));
rm.put("serverTime", ((Map<String, Object>) o).get("serverTime"));
if (((Map<String, Object>) o).get("code").toString().equals("200"))
{
if (null != ((Map<String, Object>) o).get("data"))
{
if (null != ((Map<String, Object>) (((Map<String, Object>) o).get("data"))).get("list"))
{
List<Map<String, Object>> list = ((List) ((Map<String, Object>) (((Map<String, Object>) o).get("data"))).get("list"));
for (Map<String, Object> m : list)
{
Long t = Timestamp.valueOf(m.get("gcsj").toString()).getTime();
Long s = dateUtil.CreateDate(starttime).getTime();
if (t >= s)
{
l.add(m);
}
}
d.put("TOTAL", l.size());
}
}
}
}
Collections.sort(l, new Comparator<Map<String, Object>>()
{
@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2)
{
Long t1 = Timestamp.valueOf(o1.get("gcsj").toString()).getTime();
Long t2 = Timestamp.valueOf(o2.get("gcsj").toString()).getTime();
if ((t1 - t2) > 0)
{
return 1;
}
else if ((t1 - t2) < 0)
{
return -1;
}
return 0;
}
});
return rm;
}
else if (!StringUtils.isEmpty(endtime))
{
if (o instanceof Map)
{
rm.put("code", ((Map<String, Object>) o).get("code"));
rm.put("message", ((Map<String, Object>) o).get("message"));
rm.put("serverTime", ((Map<String, Object>) o).get("serverTime"));
if (((Map<String, Object>) o).get("code").toString().equals("200"))
{
if (null != ((Map<String, Object>) o).get("data"))
{
if (null != ((Map<String, Object>) (((Map<String, Object>) o).get("data"))).get("list"))
{
List<Map<String, Object>> list = ((List) ((Map<String, Object>) (((Map<String, Object>) o).get("data"))).get("list"));
for (Map<String, Object> m : list)
{
Long t = Timestamp.valueOf(m.get("gcsj").toString()).getTime();
Long e = dateUtil.CreateDate(endtime).getTime();
if (t <= e)
{
l.add(m);
}
}
d.put("TOTAL", l.size());
}
}
}
}
Collections.sort(l, new Comparator<Map<String, Object>>()
{
@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2)
{
Long t1 = Timestamp.valueOf(o1.get("gcsj").toString()).getTime();
Long t2 = Timestamp.valueOf(o2.get("gcsj").toString()).getTime();
if ((t1 - t2) > 0)
{
return 1;
}
else if ((t1 - t2) < 0)
{
return -1;
}
return 0;
}
});
return rm;
}
else
{
return o;
}
}
catch (Exception ex)
{
ex.printStackTrace();
return new RRException(RongRunErrorCodeEnum.SYSTEM_ERROR).getCodeMsg();
}
}
@ApiImplicitParams({
@ApiImplicitParam(value = "机动车号牌号码", name = "sb03_jdchphm", required = true),
@ApiImplicitParam(value = "开始时间",name = "starttime",required = false),
@ApiImplicitParam(value = "结束时间",name = "endtime", required = false),
@ApiImplicitParam(value = "页码", name = "pageNum"),
@ApiImplicitParam(value = "页数", name = "pageSize"),
@ApiImplicitParam(value = "登录后返回的鉴权信息", name = "token")
})
@PostMapping("bayonelist")
@ApiOperation(value = "车辆过卡口列表检索(不传分页参数查所有)", notes = "车辆过卡口列表检索(不传分页参数查所有)")
public Object BayonetListSearch(
@RequestParam(value = "sb03_jdchphm", required = true) String sb03_jdchphm,
@RequestParam(value = "starttime", required = false) String starttime,
@RequestParam(value = "endtime", required = false) String endtime,
@RequestParam(value = "pageNum", required = false) Integer pageNum,
@RequestParam(value = "pageSize", required = false) Integer pageSize,
@RequestParam(value = "token", required = false) String token,
HttpServletRequest request
)
{
try
{
List<Map<String,Object>> list= kafkaSwanVehicleService.Query(pageNum,pageSize,starttime,endtime,sb03_jdchphm);
Map<String,Object> rm=new LinkedHashMap<>();
rm.put("code",200);
rm.put("data",
new LinkedHashMap<String,Object>(){
{
put("TOTAL",list.size());
put("list",list);
}});
// rm.put("lbs",null);
String sp="";
String ep="";
StringBuilder waypBuilder=new StringBuilder();
String routelatlon="";
List<Map<String,Object>> GeoList=new LinkedList<>();
Map<String,Object> multLine=new LinkedHashMap<>();
multLine.put("type","FeatureCollection");
multLine.put("features",GeoList);
rm.put("routelatlon",multLine);
for (int i=0;i<list.size();i++)
{
Map<String,Object> map=list.get(i);
if (i==0)
{
String lon= map.get("jd").toString();
String lat=map.get("wd").toString();
sp=String.format("%s,%s",lon,lat);
continue;
}
if (i>=list.size()-1)
{
String lon= map.get("jd").toString();
String lat=map.get("wd").toString();
ep=String.format("%s,%s",lon,lat);
continue;
}
String lon= map.get("jd").toString();
String lat=map.get("wd").toString();
waypBuilder.append(String.format("%s,%s",lon,lat));
waypBuilder.append(",");
}
Map<String,Object> map=new LinkedHashMap<>();
map.put("appKey",lbsappKey);
map.put("st",1);
map.put("sPoint",sp);
map.put("ePoint",ep);
if (!StringUtils.isEmpty(waypBuilder.toString()))
{
map.put("wayPoints", waypBuilder.deleteCharAt(waypBuilder.length() - 1).toString());
}
if (!StringUtils.isEmpty(sp)&&!StringUtils.isEmpty(ep))
{
String lbsstr = ZAHttpRequest.Get(lbsurl, map);
if (!StringUtils.isEmpty(lbsstr))
{
Object o = JSONObject.parse(lbsstr);
//rm.put("lbs", o);
if (o instanceof Map)
{
if (((Map<?, ?>) o).get("data") instanceof Map)
{
Object rows=((Map)((Map<?, ?>) o).get("data")).get("rows");
if (!StringUtils.isEmpty(rows))
{
if (rows instanceof List)
{
List<Map<String,Object>> mrow=(List<Map<String, Object>>)rows;
for (Map<String,Object> row :mrow)
{
Object roln= row.get("routelatlon");
if (!StringUtils.isEmpty(roln))
{
List<Double[]> PointList=new LinkedList<>();
String [] Points= roln.toString().split(";");
for (int i=0;i<Points.length;i++)
{
String[] Point=Points[i].split(",");
Double[] p=new Double[Point.length];
for (int j=0;j<Point.length;j++)
{
p[j]=Double.valueOf(Point[j]);
}
PointList.add(p);
}
Map<String,Object> LineMap=new LinkedHashMap<>();
LineMap.put("type","LineString");
LineMap.put("coordinates",PointList);
Map<String,Object> GeoMap=new LinkedHashMap<>();
GeoMap.put("type","Feature");
GeoMap.put("geometry",LineMap);
GeoMap.put("properties",new HashMap<String,Object>());
GeoList.add(GeoMap);
}
}
}
}
}
}
}
}
return rm;
}
catch (Exception ex)
{
ex.printStackTrace();
return new RRException(RongRunErrorCodeEnum.SYSTEM_ERROR).getCodeMsg();
}
}
}
测试url: