From 27bd847e2150af6b767ca36db2f6d2272c06ff9f Mon Sep 17 00:00:00 2001 From: yidao620 Date: Tue, 27 Feb 2018 18:01:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BB=A3=E7=A0=81=E7=A4=BA?= =?UTF-8?q?=E4=BE=8Bspringboot-swagger2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/application.yml | 4 +- springboot-swagger2/.gitignore | 17 +++ springboot-swagger2/LICENSE | 20 +++ springboot-swagger2/README.md | 10 ++ springboot-swagger2/pom.xml | 143 ++++++++++++++++++ springboot-swagger2/run.sh | 72 +++++++++ .../java/com/xncoding/jwt/Application.java | 12 ++ .../xncoding/jwt/api/ExceptionController.java | 34 +++++ .../com/xncoding/jwt/api/LoginController.java | 53 +++++++ .../xncoding/jwt/api/NotifyController.java | 41 +++++ .../xncoding/jwt/api/PublicController.java | 123 +++++++++++++++ .../xncoding/jwt/api/model/BaseResponse.java | 68 +++++++++ .../com/xncoding/jwt/api/model/BindParam.java | 41 +++++ .../jwt/api/model/JoinBindResponse.java | 42 +++++ .../xncoding/jwt/api/model/LoginParam.java | 67 ++++++++ .../com/xncoding/jwt/api/model/PosParam.java | 133 ++++++++++++++++ .../xncoding/jwt/api/model/ReportParam.java | 41 +++++ .../xncoding/jwt/api/model/UnbindParam.java | 41 +++++ .../xncoding/jwt/api/model/VersionParam.java | 54 +++++++ .../xncoding/jwt/api/model/VersionResult.java | 96 ++++++++++++ .../xncoding/jwt/config/Swagger2Config.java | 55 +++++++ .../src/main/resources/application.yml | 97 ++++++++++++ .../com/xncoding/jwt/ApplicationTests.java | 15 ++ .../com/xncoding/jwt/Swagger2MarkupTest.java | 112 ++++++++++++++ .../src/test/resources/keystore.jks | Bin 0 -> 1334 bytes 25 files changed, 1389 insertions(+), 2 deletions(-) create mode 100644 springboot-swagger2/.gitignore create mode 100644 springboot-swagger2/LICENSE create mode 100644 springboot-swagger2/README.md create mode 100644 springboot-swagger2/pom.xml create mode 100644 springboot-swagger2/run.sh create mode 100644 springboot-swagger2/src/main/java/com/xncoding/jwt/Application.java create mode 100644 springboot-swagger2/src/main/java/com/xncoding/jwt/api/ExceptionController.java create mode 100644 springboot-swagger2/src/main/java/com/xncoding/jwt/api/LoginController.java create mode 100644 springboot-swagger2/src/main/java/com/xncoding/jwt/api/NotifyController.java create mode 100644 springboot-swagger2/src/main/java/com/xncoding/jwt/api/PublicController.java create mode 100644 springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/BaseResponse.java create mode 100644 springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/BindParam.java create mode 100644 springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/JoinBindResponse.java create mode 100644 springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/LoginParam.java create mode 100644 springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/PosParam.java create mode 100644 springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/ReportParam.java create mode 100644 springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/UnbindParam.java create mode 100644 springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/VersionParam.java create mode 100644 springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/VersionResult.java create mode 100644 springboot-swagger2/src/main/java/com/xncoding/jwt/config/Swagger2Config.java create mode 100644 springboot-swagger2/src/main/resources/application.yml create mode 100644 springboot-swagger2/src/test/java/com/xncoding/jwt/ApplicationTests.java create mode 100644 springboot-swagger2/src/test/java/com/xncoding/jwt/Swagger2MarkupTest.java create mode 100644 springboot-swagger2/src/test/resources/keystore.jks diff --git a/springboot-shiro/src/main/resources/application.yml b/springboot-shiro/src/main/resources/application.yml index eb1abbc..373f23a 100644 --- a/springboot-shiro/src/main/resources/application.yml +++ b/springboot-shiro/src/main/resources/application.yml @@ -63,7 +63,7 @@ spring: datasource: url: jdbc:mysql://123.207.66.156:3306/pos?useSSL=false&autoReconnect=true&tinyInt1isBit=false&useUnicode=true&characterEncoding=utf8 username: root - password: _EnZhi123 + password: ****** thymeleaf: cache: false @@ -94,7 +94,7 @@ spring: datasource: url: jdbc:mysql://123.207.66.156:3306/pos?useSSL=false&autoReconnect=true&tinyInt1isBit=false&useUnicode=true&characterEncoding=utf8 username: root - password: _EnZhi123 + password: ****** thymeleaf: cache: false diff --git a/springboot-swagger2/.gitignore b/springboot-swagger2/.gitignore new file mode 100644 index 0000000..b71e0c4 --- /dev/null +++ b/springboot-swagger2/.gitignore @@ -0,0 +1,17 @@ +# 此为注释– 将被Git 忽略 +# /结尾表示是目录,忽略目录和目录下的所有件 +# /开头表示根目录,否则是.gitignore的相对目录 +# !开头表示反选 +.idea/ +target/ +*.iml +*.ipr +*.iws +*.log +.svn/ +.project +rebel.xml +.rebel-remote.xml.* +swagger.json +swagger.adoc + diff --git a/springboot-swagger2/LICENSE b/springboot-swagger2/LICENSE new file mode 100644 index 0000000..83cd47d --- /dev/null +++ b/springboot-swagger2/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2018 Xiong Neng + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/springboot-swagger2/README.md b/springboot-swagger2/README.md new file mode 100644 index 0000000..d012d5a --- /dev/null +++ b/springboot-swagger2/README.md @@ -0,0 +1,10 @@ + +## 简介 + +集成Swagger2自动生成API文档,同时可转换成PDF格式 + +## 许可证 + +Copyright (c) 2018 Xiong Neng + +基于 MIT 协议发布: diff --git a/springboot-swagger2/pom.xml b/springboot-swagger2/pom.xml new file mode 100644 index 0000000..1187f94 --- /dev/null +++ b/springboot-swagger2/pom.xml @@ -0,0 +1,143 @@ + + + 4.0.0 + + com.xncoding + springboot-swagger2 + 1.0.0-SNAPSHOT + jar + + springboot-swagger2 + 集成Swagger2 + + + org.springframework.boot + spring-boot-starter-parent + 1.5.9.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-tomcat + + + + + org.springframework.boot + spring-boot-starter-jetty + + + + org.springframework.boot + spring-boot-starter-test + test + + + com.vaadin.external.google + android-json + + + + + commons-io + commons-io + 2.5 + + + org.apache.commons + commons-lang3 + 3.7 + + + commons-codec + commons-codec + 1.11 + + + + + io.springfox + springfox-swagger2 + 2.7.0 + + + com.xncoding + springfox-swagger-ui + 2.7.0 + + + org.pegdown + pegdown + 1.6.0 + test + + + io.github.swagger2markup + swagger2markup + 1.3.1 + test + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.1 + + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.20 + + + ${project.basedir}/src/main/resources/swagger + ${project.basedir}/src/main/resources/swagger/swagger + + true + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + + + src/main/resources + + + src/main/java + + **/*.xml + + + + + + \ No newline at end of file diff --git a/springboot-swagger2/run.sh b/springboot-swagger2/run.sh new file mode 100644 index 0000000..955efb1 --- /dev/null +++ b/springboot-swagger2/run.sh @@ -0,0 +1,72 @@ +#!/bin/bash +# 项目自动更新脚本 +# 先clone相应的分支下来: +# git clone ssh://git@120.24.173.142:7999/xxx.git +# 远程调试启动: +# nohup java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 & + +function start { + profile="$1" + echo "启动环境profile=${profile}" + jarfile=$(ls target/*.jar) + if [[ "$?" == "0" ]]; then + stop $profile $jarfile + fi + branch=$(git branch |awk '{print $2}') + git pull origin ${branch} + echo "更新完代码开始重新打包" + mvn clean && mvn clean && mvn package -DskipTests=true + if [[ "$?" != "0" ]]; then + echo "编译出错,退出!" + exit 1 + fi + echo "nohup java -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 &" + nohup java -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 & + echo "启动应用中,请查看日志文件..." +} + +function stop { + profile="$1" + jarfile="$2" + ps aux | grep "${jarfile}" | grep "spring.profiles.active=${profile}" | grep -v grep > /dev/null + if [[ "$?" == "0" ]]; then + echo "该应用还在跑,我先停了它" + pid=$(ps aux | grep "${jarfile}" | grep "spring.profiles.active=${profile}" | grep -v grep |awk '{print $2}') + if [[ "$pid" != "" ]]; then + kill -9 $pid + fi + echo "停止应用成功..." + fi +} + +if [[ "$1" == "start" ]]; then + if [[ "$#" < 2 ]]; then + echo "请输入正确参数:./epay.sh start {profile}" + exit 1 + fi + profile="$2" + if [[ "$profile" != "dev" && "$profile" != "test" && "$profile" != "show" && "$profile" != "production" ]]; then + echo "参数错误,请输入正确的profile参数,使用方法:" + echo "./epay.sh start {profile} ==> 启动应用,{profile}取值:dev|test|show|production" + exit 1 + fi + start "${profile}" +elif [[ "$1" == "stop" ]]; then + if [[ "$#" < 2 ]]; then + echo "请输入正确参数:./epay.sh stop {profile}" + exit 1 + fi + profile="$2" + if [[ "$profile" != "dev" && "$profile" != "test" && "$profile" != "show" && "$profile" != "production" ]]; then + echo "参数错误,请输入正确的profile参数,使用方法:" + echo "./epay.sh stop {profile} ==> 停止应用,{profile}取值:dev|test|show|production" + exit 1 + fi + jarfile=$(ls target/*.jar) + stop $profile $jarfile +else + echo "参数错误,使用方法:{}参数是必填的,[]参数可选" + echo "./epay.sh start {profile} ==> 启动应用,{profile}取值:dev|test|show|production" + echo "./epay.sh stop {profile} ==> 停止应用,{profile}取值:dev|test|show|production" + exit 1 +fi diff --git a/springboot-swagger2/src/main/java/com/xncoding/jwt/Application.java b/springboot-swagger2/src/main/java/com/xncoding/jwt/Application.java new file mode 100644 index 0000000..e180aef --- /dev/null +++ b/springboot-swagger2/src/main/java/com/xncoding/jwt/Application.java @@ -0,0 +1,12 @@ +package com.xncoding.jwt; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/springboot-swagger2/src/main/java/com/xncoding/jwt/api/ExceptionController.java b/springboot-swagger2/src/main/java/com/xncoding/jwt/api/ExceptionController.java new file mode 100644 index 0000000..2d9a5a8 --- /dev/null +++ b/springboot-swagger2/src/main/java/com/xncoding/jwt/api/ExceptionController.java @@ -0,0 +1,34 @@ +package com.xncoding.jwt.api; + +import com.xncoding.jwt.api.model.BaseResponse; +import io.swagger.annotations.Api; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import javax.servlet.http.HttpServletRequest; + +/** + * 注意这个统一异常处理器只对认证过的用户调用接口中的异常有作用,对AuthenticationException没有用 + */ +@RestControllerAdvice +@Api(value = "机具管理API接口类", tags = "机具管理接口", description = "提供POS机的入网和监控的统一管理") +public class ExceptionController { + + // 捕捉其他所有异常 + @ExceptionHandler(Exception.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public BaseResponse globalException(HttpServletRequest request, Throwable ex) { + return new BaseResponse(false, "其他异常", null); + } + + private HttpStatus getStatus(HttpServletRequest request) { + Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code"); + if (statusCode == null) { + return HttpStatus.INTERNAL_SERVER_ERROR; + } + return HttpStatus.valueOf(statusCode); + } +} + diff --git a/springboot-swagger2/src/main/java/com/xncoding/jwt/api/LoginController.java b/springboot-swagger2/src/main/java/com/xncoding/jwt/api/LoginController.java new file mode 100644 index 0000000..d3f21b2 --- /dev/null +++ b/springboot-swagger2/src/main/java/com/xncoding/jwt/api/LoginController.java @@ -0,0 +1,53 @@ +package com.xncoding.jwt.api; + +import com.xncoding.jwt.api.model.BaseResponse; +import com.xncoding.jwt.api.model.LoginParam; +import io.swagger.annotations.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; +import springfox.documentation.annotations.ApiIgnore; + +/** + * 登录接口类 + */ +@Api(value = "登录请求接口类", tags = "登录", description = "用户请求登录获取Token") +@ApiResponses(value = { + @ApiResponse(code = 200, message = "请求已完成"), + @ApiResponse(code = 201, message = "资源成功被创建"), + @ApiResponse(code = 400, message = "请求中有语法问题,或不能满足请求"), + @ApiResponse(code = 401, message = "未授权客户机访问数据"), + @ApiResponse(code = 403, message = "服务器接受请求,但是拒绝处理"), + @ApiResponse(code = 404, message = "服务器找不到给定的资源;文档不存在"), + @ApiResponse(code = 500, message = "服务器出现异常")} +) +@RestController +public class LoginController { + + private static final Logger _logger = LoggerFactory.getLogger(LoginController.class); + + @ApiOperation(value = "登录认证接口", notes = "不管是接口还是WebSocket连接都需要先调用此接口拿到Token,然后再通过Token调用相应接口,最好使用HTTPS协议。调用RESTful API方式不需要传后面两个参数,如果通过WebSocket连接需要传递", produces = "application/json") + @PostMapping("/login") + public BaseResponse login(@RequestHeader(name="Content-Type", defaultValue = "application/json") String contentType, + @ApiParam(value = "登录参数") @RequestBody LoginParam loginParam) { + _logger.info("用户请求登录获取Token"); + return new BaseResponse<>(true, "Login success", "JWT"); + } + + @ApiOperation(value = "推送消息登录认证接口", notes = "这里的密码是加密后的密码", produces = "application/json") + @PostMapping("/notifyLogin") + public BaseResponse notifyLogin(@RequestHeader(name="Content-Type", defaultValue = "application/json") String contentType, + @ApiParam(value = "登录参数") @RequestBody LoginParam loginParam) { + _logger.info("登录用户推送请求登录获取Token"); + return new BaseResponse<>(true, "Login success", "JWT"); + } + + @GetMapping(path = "/401") + @ResponseStatus(HttpStatus.UNAUTHORIZED) + @ApiIgnore + public BaseResponse unauthorized() { + return new BaseResponse<>(false, "Unauthorized", null); + } + +} diff --git a/springboot-swagger2/src/main/java/com/xncoding/jwt/api/NotifyController.java b/springboot-swagger2/src/main/java/com/xncoding/jwt/api/NotifyController.java new file mode 100644 index 0000000..c5fcc83 --- /dev/null +++ b/springboot-swagger2/src/main/java/com/xncoding/jwt/api/NotifyController.java @@ -0,0 +1,41 @@ +package com.xncoding.jwt.api; + +import com.xncoding.jwt.api.model.BaseResponse; +import com.xncoding.jwt.api.model.UnbindParam; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.*; + +/** + * 通知接口类 + */ +@Api(value = "通知接口类", tags = "通知", description = "服务器给相应客户端推送通知") +@ApiResponses(value = { + @ApiResponse(code = 200, message = "请求已完成"), + @ApiResponse(code = 201, message = "资源成功被创建"), + @ApiResponse(code = 400, message = "请求中有语法问题,或不能满足请求"), + @ApiResponse(code = 401, message = "未授权客户机访问数据"), + @ApiResponse(code = 403, message = "服务器接受请求,但是拒绝处理"), + @ApiResponse(code = 404, message = "服务器找不到给定的资源;文档不存在"), + @ApiResponse(code = 500, message = "服务器出现异常")} +) +@RestController +@RequestMapping(value = "/notify") +public class NotifyController { + + private static final Logger _logger = LoggerFactory.getLogger(NotifyController.class); + + @ApiOperation(value = "解绑通知接口", notes = "后台管理员解除网点绑定后需调用此接口通知相应的POS机", produces = "application/json") + @PostMapping("/unbind") + public BaseResponse unbind(@RequestHeader(name = "Content-Type", defaultValue = "application/json") String contentType, + @RequestHeader(name = "Authorization", defaultValue = "token") String token, + @RequestBody UnbindParam unbindParam) { + _logger.info("解绑通知接口start"); + return new BaseResponse<>(true, "解绑通知发送成功", null); + } + +} diff --git a/springboot-swagger2/src/main/java/com/xncoding/jwt/api/PublicController.java b/springboot-swagger2/src/main/java/com/xncoding/jwt/api/PublicController.java new file mode 100644 index 0000000..6d67f66 --- /dev/null +++ b/springboot-swagger2/src/main/java/com/xncoding/jwt/api/PublicController.java @@ -0,0 +1,123 @@ +package com.xncoding.jwt.api; + +import com.xncoding.jwt.api.model.*; +import io.swagger.annotations.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.Date; + +/** + * 机具管理API接口类 + */ +@Api(value = "机具管理业务接口类", tags = "机具管理", description = "机具管理业务接口类") +@ApiResponses(value = { + @ApiResponse(code = 200, message = "请求已完成"), + @ApiResponse(code = 201, message = "资源成功被创建"), + @ApiResponse(code = 400, message = "请求中有语法问题,或不能满足请求"), + @ApiResponse(code = 401, message = "未授权客户机访问数据"), + @ApiResponse(code = 403, message = "服务器接受请求,但是拒绝处理"), + @ApiResponse(code = 404, message = "服务器找不到给定的资源;文档不存在"), + @ApiResponse(code = 500, message = "服务器出现异常")} +) +@RestController +@RequestMapping(value = "/api/v1") +public class PublicController { + + private static final Logger _logger = LoggerFactory.getLogger(PublicController.class); + + /** + * 入网绑定查询接口 + * + * @return 是否入网 + */ + @ApiOperation(value = "入网绑定查询接口", notes = "根据IMEI码来查询该POS机是否入网并绑定了网点
每次打开APP的时候需要调用这个接口来进行检查
如果入网并且绑定了网点才能打开APP,否则必须调用入网或绑定网点接口成功后才能继续下一步", produces = "application/json") + @ApiImplicitParam(name = "imei", value = "IMEI码", required = true, dataType = "String", paramType = "query", defaultValue = "1234555SHA") + @RequestMapping(value = "/join", method = RequestMethod.GET) + public JoinBindResponse join(@RequestHeader(name="Accept", defaultValue = "application/json", required = false) String accept, + @RequestHeader(name="Authorization", defaultValue="token") String token, + @RequestParam("imei") String imei) { + _logger.info("入网查询接口 start...."); + JoinBindResponse result = new JoinBindResponse(); + result.setSuccess(true); + result.setCode(1); + result.setMsg("已入网并绑定了网点"); + return result; + } + + /** + * 请求入网接口 + * + * @return 处理结果 + */ + @ApiOperation(value = "请求入网接口", notes = "通过收集POS硬件信息请求入网,另外还必须传入网点名称", produces = "application/json") + @RequestMapping(value = "/join", method = RequestMethod.POST) + public ResponseEntity doJoin(@RequestHeader(name="Content-Type", defaultValue = "application/json") String contentType, + @RequestHeader(name="Authorization", defaultValue="token") String token, + @RequestBody PosParam posParam) { + _logger.info("请求入网接口 start...."); + BaseResponse result = new BaseResponse(); + result.setSuccess(true); + result.setMsg("入网成功"); + return new ResponseEntity<>(result, HttpStatus.CREATED); + } + + /** + * 请求绑定网点接口 + * + * @return 处理结果 + */ + @ApiOperation(value = "请求绑定网点接口", notes = "POS机重新绑定新的网点", produces = "application/json") + @RequestMapping(value = "/bind", method = RequestMethod.POST) + public ResponseEntity doBind(@RequestHeader(name="Content-Type", defaultValue = "application/json") String contentType, + @RequestHeader(name="Authorization", defaultValue="token") String token, + @RequestBody BindParam bindParam) { + _logger.info("请求绑定网点接口 start...."); + BaseResponse result = new BaseResponse(); + result.setSuccess(true); + result.setMsg("绑定网点成功"); + return new ResponseEntity<>(result, HttpStatus.OK); + } + + /** + * 报告位置接口 + * + * @return 报告结果 + */ + @ApiOperation(value = "报告位置接口", notes = "终端设备定时报告位置信息", produces = "application/json") + @RequestMapping(value = "/report", method = RequestMethod.POST) + public BaseResponse report(@RequestHeader(name="Content-Type", defaultValue = "application/json") String contentType, + @RequestHeader(name="Authorization", defaultValue="token") String token, + @RequestBody ReportParam param) { + _logger.info("定时报告接口 start...."); + BaseResponse result = new BaseResponse(); + result.setSuccess(true); + result.setMsg("心跳报告成功"); + return result; + } + + /** + * 版本检查接口 + * + * @return 版本检查结果 + */ + @ApiOperation(value = "APP版本更新检查", notes = "APP版本更新检查", produces = "application/json") + @RequestMapping(value = "/version", method = RequestMethod.POST) + public VersionResult version(@RequestHeader(name="Content-Type", defaultValue = "application/json") String contentType, + @RequestHeader(name="Authorization", defaultValue="token") String token, + @RequestBody VersionParam param) { + _logger.info("版本检查接口 start...."); + VersionResult result = new VersionResult(); + result.setAppName("AppName"); + result.setDownloadUrl("http://www.baidu.com/"); + result.setFindNew(true); + result.setPublishtime(new Date()); + result.setTips("tips"); + result.setVersion("v1.2"); + return result; + } + +} diff --git a/springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/BaseResponse.java b/springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/BaseResponse.java new file mode 100644 index 0000000..1415107 --- /dev/null +++ b/springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/BaseResponse.java @@ -0,0 +1,68 @@ +package com.xncoding.jwt.api.model; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import springfox.documentation.annotations.ApiIgnore; + +/** + * API接口的基础返回类 + * + * @author XiongNeng + * @version 1.0 + * @since 2018/1/7 + */ +@ApiModel(value = "BaseResponse", description = "API接口的返回对象") +public class BaseResponse { + /** + * 是否成功 + */ + @ApiModelProperty(value = "是否成功", name = "success", example = "true", required = true) + private boolean success; + + /** + * 说明 + */ + @ApiModelProperty(value = "返回的详细说明", name = "msg", example = "成功") + private String msg; + + /** + * 返回数据 + */ + @ApiModelProperty(value = "返回的数据", name = "data") + private T data; + + public BaseResponse() { + + } + + public BaseResponse(boolean success, String msg, T data) { + this.success = success; + this.msg = msg; + this.data = data; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } + +} diff --git a/springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/BindParam.java b/springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/BindParam.java new file mode 100644 index 0000000..06b2abf --- /dev/null +++ b/springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/BindParam.java @@ -0,0 +1,41 @@ +package com.xncoding.jwt.api.model; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +/** + * POS绑定网点参数 + * + * @author XiongNeng + * @version 1.0 + * @since 2018/1/7 + */ +@ApiModel(value = "POS绑定网点参数", description = "POSPOS绑定网点请求时候需要传入的参数") +public class BindParam { + /** + * 机具IMEI码 + */ + @ApiModelProperty(value = "IMEI码(长度1-32位)", name = "imei", dataType = "String", example = "2324DEEFAXX122", required = true) + private String imei; + /** + * 归属网点 + */ + @ApiModelProperty(value = "归属网点(长度1-64位)", name = "location", dataType = "String", example = "昆明市公安局交通警察支队车辆管理所", required = true) + private String location; + + public String getImei() { + return imei; + } + + public void setImei(String imei) { + this.imei = imei; + } + + public String getLocation() { + return location; + } + + public void setLocation(String location) { + this.location = location; + } +} diff --git a/springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/JoinBindResponse.java b/springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/JoinBindResponse.java new file mode 100644 index 0000000..4e92720 --- /dev/null +++ b/springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/JoinBindResponse.java @@ -0,0 +1,42 @@ +package com.xncoding.jwt.api.model; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +/** + * 入网绑定返回 + * + * @author XiongNeng + * @version 1.0 + * @since 2018/1/16 + */ +@ApiModel(value = "入网绑定返回信息", description = "入网绑定的返回对象") +public class JoinBindResponse extends BaseResponse { + /** + * 返回码 1:已入网并绑定了网点 2:已入网但尚未绑定网点 3:未入网 + */ + @ApiModelProperty(value = "返回码 1:已入网并绑定了网点 2:已入网但尚未绑定网点 3:未入网", name = "code", example = "1", required = true) + private int code; + + /** + * 机具状态: 1:正常 2:故障 3:维修中(返厂) 4:已禁用(丢失) 5:已停用(回收) + */ + @ApiModelProperty(value = "机具状态 1:正常 2:故障 3:维修中(返厂) 4:已禁用(丢失) 5:已停用(回收)", name = "posState", example = "1", required = true) + private Integer posState; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public Integer getPosState() { + return posState; + } + + public void setPosState(Integer posState) { + this.posState = posState; + } +} diff --git a/springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/LoginParam.java b/springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/LoginParam.java new file mode 100644 index 0000000..7ad9928 --- /dev/null +++ b/springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/LoginParam.java @@ -0,0 +1,67 @@ +package com.xncoding.jwt.api.model; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +/** + * 登录认证接口参数 + * + * @author XiongNeng + * @version 1.0 + * @since 2018/1/9 + */ +@ApiModel(value = "登录认证接口参数", description = "登录认证接口参数") +public class LoginParam { + /** + * 用户名 + */ + @ApiModelProperty(value = "用户名", name = "username", example = "admin", required = true) + private String username; + /** + * 密码 + */ + @ApiModelProperty(value = "密码", name = "password", example = "123456", required = true) + private String password; + /** + * Application ID + */ + @ApiModelProperty(value = "Application ID", name = "appid", example = "com.xncoding.xzpay", required = false) + private String appid; + /** + * IMEI码 + */ + @ApiModelProperty(value = "IMEI码", name = "imei", example = "TUYIUOIU234234YUII", required = false) + private String imei; + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getAppid() { + return appid; + } + + public void setAppid(String appid) { + this.appid = appid; + } + + public String getImei() { + return imei; + } + + public void setImei(String imei) { + this.imei = imei; + } +} diff --git a/springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/PosParam.java b/springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/PosParam.java new file mode 100644 index 0000000..9c2577e --- /dev/null +++ b/springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/PosParam.java @@ -0,0 +1,133 @@ +package com.xncoding.jwt.api.model; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +/** + * POS入网参数 + * + * @author XiongNeng + * @version 1.0 + * @since 2018/1/7 + */ +@ApiModel(value = "POS入网参数", description = "POS入网请求时候需要传入的参数") +public class PosParam { + /** + * 机具IMEI码 + */ + @ApiModelProperty(value = "IMEI码(长度1-32位)", name = "imei", dataType = "String", example = "2324DEEFAXX122", required = true) + private String imei; + /** + * 序列号(SN) + */ + @ApiModelProperty(value = "序列号SN(长度1-64位)", name = "sn", dataType = "String", example = "ssssssnnnn", required = true) + private String sn; + /** + * 机具型号 + */ + @ApiModelProperty(value = "机具型号(长度1-32位)", name = "series", dataType = "String", example = "APOS A8", required = true) + private String series; + /** + * Android版本 + */ + @ApiModelProperty(value = "Android版本(长度1-32位)", name = "androidVersion", dataType = "String", example = "6.2.0", required = true) + private String androidVersion; + /** + * 版本号 + */ + @ApiModelProperty(value = "版本号(长度1-32位)", name = "version", dataType = "String", example = "B0182_C2BOM_V1.1.4_20171213/4.0.28", required = true) + private String version; + /** + * 归属网点 + */ + @ApiModelProperty(value = "归属网点(长度1-64位)", name = "location", dataType = "String", example = "昆明市公安局交通警察支队车辆管理所", required = true) + private String location; + /** + * 产权方 + */ + @ApiModelProperty(value = "机具产权方(长度1-64位)", name = "owner", dataType = "String", example = "招商银行昆明分行", required = true) + private String owner; + /** + * 备注 + */ + @ApiModelProperty(value = "备注说明(长度0-255位)", name = "tips", dataType = "String", example = "备注说明") + private String tips; + /** + * 应用ID + */ + @ApiModelProperty(value = "APP应用ID(长度1-64位),每个APP都有唯一的Application Id,并且这个应用ID必须已经存在于系统之中", + name = "applicationId", dataType = "String", example = "com.xncoding.xzpay", required = true) + private String applicationId; + + public String getImei() { + return imei; + } + + public void setImei(String imei) { + this.imei = imei; + } + + public String getSn() { + return sn; + } + + public void setSn(String sn) { + this.sn = sn; + } + + public String getSeries() { + return series; + } + + public void setSeries(String series) { + this.series = series; + } + + public String getAndroidVersion() { + return androidVersion; + } + + public void setAndroidVersion(String androidVersion) { + this.androidVersion = androidVersion; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public String getLocation() { + return location; + } + + public void setLocation(String location) { + this.location = location; + } + + public String getOwner() { + return owner; + } + + public void setOwner(String owner) { + this.owner = owner; + } + + public String getTips() { + return tips; + } + + public void setTips(String tips) { + this.tips = tips; + } + + public String getApplicationId() { + return applicationId; + } + + public void setApplicationId(String applicationId) { + this.applicationId = applicationId; + } +} diff --git a/springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/ReportParam.java b/springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/ReportParam.java new file mode 100644 index 0000000..d99a499 --- /dev/null +++ b/springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/ReportParam.java @@ -0,0 +1,41 @@ +package com.xncoding.jwt.api.model; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +/** + * 报告参数 + * + * @author XiongNeng + * @version 1.0 + * @since 2018/1/7 + */ +@ApiModel(value = "报告参数", description = "报告参数") +public class ReportParam { + /** + * IMEI码 + */ + @ApiModelProperty(value = "IMEI码(长度1-32位)", name = "imei", example = "2324DEEFAXX122", required = true) + private String imei; + /** + * 位置 + */ + @ApiModelProperty(value = "位置(长度1-255位)", name = "location", example = "广东省广州市天河区五山路321号", required = true) + private String location; + + public String getImei() { + return imei; + } + + public void setImei(String imei) { + this.imei = imei; + } + + public String getLocation() { + return location; + } + + public void setLocation(String location) { + this.location = location; + } +} diff --git a/springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/UnbindParam.java b/springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/UnbindParam.java new file mode 100644 index 0000000..14f8bc7 --- /dev/null +++ b/springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/UnbindParam.java @@ -0,0 +1,41 @@ +package com.xncoding.jwt.api.model; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +/** + * 解绑通知参数 + * + * @author XiongNeng + * @version 1.0 + * @since 2018/1/9 + */ +@ApiModel(value = "解绑通知参数", description = "解绑通知参数") +public class UnbindParam { + /** + * IMEI码 + */ + @ApiModelProperty(value = "IMEI码", name = "imei", example = "TUYIUOIU234234YUII") + private String imei; + /** + * 网点 + */ + @ApiModelProperty(value = "网点", name = "location", example = "广州市天河区交警7区") + private String location; + + public String getImei() { + return imei; + } + + public void setImei(String imei) { + this.imei = imei; + } + + public String getLocation() { + return location; + } + + public void setLocation(String location) { + this.location = location; + } +} diff --git a/springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/VersionParam.java b/springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/VersionParam.java new file mode 100644 index 0000000..dab0db9 --- /dev/null +++ b/springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/VersionParam.java @@ -0,0 +1,54 @@ +package com.xncoding.jwt.api.model; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +/** + * APP版本检查接口参数 + * + * @author XiongNeng + * @version 1.0 + * @since 2018/1/9 + */ +@ApiModel(value = "APP版本检查参数", description = "APP版本检查参数") +public class VersionParam { + /** + * 机具IMEI码 + */ + @ApiModelProperty(value = "IMEI码", name = "imei", example = "2324DEEFAXX122", required = true) + private String imei; + /** + * 应用ID + */ + @ApiModelProperty(value = "APP应用ID,每个APP都有唯一的Application Id", name = "applicationId", example = "com.xncoding.xzpay", required = true) + private String applicationId; + /** + * 当前版本号 + */ + @ApiModelProperty(value = "APP的当前版本号", name = "version", example = "1.2.0", required = true) + private String version; + + public String getApplicationId() { + return applicationId; + } + + public void setApplicationId(String applicationId) { + this.applicationId = applicationId; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public String getImei() { + return imei; + } + + public void setImei(String imei) { + this.imei = imei; + } +} diff --git a/springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/VersionResult.java b/springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/VersionResult.java new file mode 100644 index 0000000..60b3aa5 --- /dev/null +++ b/springboot-swagger2/src/main/java/com/xncoding/jwt/api/model/VersionResult.java @@ -0,0 +1,96 @@ +package com.xncoding.jwt.api.model; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +import java.util.Date; + +/** + * 版本检查结果 + * + * @author XiongNeng + * @version 1.0 + * @since 2018/1/9 + */ +@ApiModel(value = "APP版本检查结果", description = "APP版本检查结果") +public class VersionResult { + /** + * 是否发现新版本 + */ + @ApiModelProperty(value = "是否发现新版本(true:发现新版本,false:没有发现新版本)", name = "findNew", example = "true", required = true) + private boolean findNew; + /** + * APP名称 + */ + @ApiModelProperty(value = "APP名称", name = "appName", example = "行政收费") + private String appName; + /** + * 新版本号 + */ + @ApiModelProperty(value = "新版本号", name = "version", example = "v1.3.8") + private String version; + /** + * 新版本说明 + */ + @ApiModelProperty(value = "新版本说明", name = "tips", example = "增加人脸识别功能") + private String tips; + /** + * 新版本发布时间 + */ + @ApiModelProperty(value = "新版本发布时间", name = "publishtime", example = "2017-12-24 12:32:19") + private Date publishtime; + + /** + * 新版本下载地址 + */ + @ApiModelProperty(value = "新版本下载地址", name = "downloadUrl", example = "http://xncoding.net/files/行政收费_1.3.0.apk") + private String downloadUrl; + + public boolean isFindNew() { + return findNew; + } + + public void setFindNew(boolean findNew) { + this.findNew = findNew; + } + + public String getDownloadUrl() { + return downloadUrl; + } + + public void setDownloadUrl(String downloadUrl) { + this.downloadUrl = downloadUrl; + } + + public String getAppName() { + return appName; + } + + public void setAppName(String appName) { + this.appName = appName; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public String getTips() { + return tips; + } + + public void setTips(String tips) { + this.tips = tips; + } + + public Date getPublishtime() { + return publishtime; + } + + public void setPublishtime(Date publishtime) { + this.publishtime = publishtime; + } +} diff --git a/springboot-swagger2/src/main/java/com/xncoding/jwt/config/Swagger2Config.java b/springboot-swagger2/src/main/java/com/xncoding/jwt/config/Swagger2Config.java new file mode 100644 index 0000000..fdd1b50 --- /dev/null +++ b/springboot-swagger2/src/main/java/com/xncoding/jwt/config/Swagger2Config.java @@ -0,0 +1,55 @@ +package com.xncoding.jwt.config; + +import com.google.common.collect.Sets; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import springfox.documentation.builders.ApiInfoBuilder; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.service.ApiInfo; +import springfox.documentation.service.Contact; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.swagger2.annotations.EnableSwagger2; + +/** + * Swagger2的Java配置类 + * + * @author XiongNeng + * @version 1.0 + * @since 2018/1/7 + */ +@Configuration +@EnableSwagger2 +public class Swagger2Config { + + @Bean + public Docket createRestApi() { + return new Docket(DocumentationType.SWAGGER_2) + .produces(Sets.newHashSet("application/json")) + .consumes(Sets.newHashSet("application/json")) + .protocols(Sets.newHashSet("http", "https")) + .apiInfo(apiInfo()) + .forCodeGeneration(true) + .select() + // 指定controller存放的目录路径 + .apis(RequestHandlerSelectors.basePackage("com.xncoding.jwt.api")) +// .paths(PathSelectors.ant("/api/v1/*")) + .paths(PathSelectors.any()) + .build(); + } + + private ApiInfo apiInfo() { + return new ApiInfoBuilder() + // 文档标题 + .title("系统API服务") + // 文档描述 + .description("系统API接口文档") + // .termsOfServiceUrl("https://github.com/yidao620c") + .version("v1") + // .license("MIT 协议") + // .licenseUrl("http://www.opensource.org/licenses/MIT") + // .contact(new Contact("熊能","https://github.com/yidao620c","yidao620@gmail.com")) + .build(); + } +} diff --git a/springboot-swagger2/src/main/resources/application.yml b/springboot-swagger2/src/main/resources/application.yml new file mode 100644 index 0000000..1cc07fe --- /dev/null +++ b/springboot-swagger2/src/main/resources/application.yml @@ -0,0 +1,97 @@ +########################################################## +################## 所有profile共有的配置 ################# +########################################################## + +################### 自定义项目配置 ################### +xncoding: + socket-port: 9096 #socket端口 + ping-interval: 60000 #Ping消息间隔(毫秒) + ping-timeout: 180000 #Ping消息超时时间(毫秒) + +################### 项目启动端口 ################### +server.port: 9095 + +################### spring配置 ################### +spring: + profiles: + active: dev + http: + multipart: + max-request-size: 100MB #最大请求大小 + max-file-size: 100MB #最大文件大小 + +################### mybatis-plus配置 ################### +mybatis-plus: + mapper-locations: classpath*:com/xncoding/jwt/dao/repository/mapping/*.xml + typeAliasesPackage: > + com.xncoding.jwt.api.model, + com.xncoding.jwt.dao.entity, + com.xncoding.jwt.common.dao.entity + global-config: + id-type: 0 # 0:数据库ID自增 1:用户输入id 2:全局唯一id(IdWorker) 3:全局唯一ID(uuid) + db-column-underline: false + refresh-mapper: true + configuration: + map-underscore-to-camel-case: true + cache-enabled: true #配置的缓存的全局开关 + lazyLoadingEnabled: true #延时加载的开关 + multipleResultSetsEnabled: true #开启的话,延时加载一个属性时会加载该对象全部属性,否则按需加载属性 + +################### spring security配置 ################### +security: + ignored: /static/** + +logging: + level: + org.springframework.web.servlet: ERROR + +--- + +##################################################################### +######################## 开发环境profile ########################## +##################################################################### +spring: + profiles: dev + datasource: + url: jdbc:mysql://123.207.66.156:3306/pos?useSSL=false&autoReconnect=true&tinyInt1isBit=false&useUnicode=true&characterEncoding=utf8 + username: root + password: ****** + thymeleaf: + cache: false + +################### 自定义项目配置 ################### +xncoding: + apk-url-prefix: http://xncoding.net/files/ #APK文件访问URL前缀 + +logging: + level: + ROOT: INFO + com: + xncoding: DEBUG + file: E:/logs/pos-api.log + +--- + +##################################################################### +######################## 测试环境profile ########################## +##################################################################### + +spring: + profiles: test + datasource: + url: jdbc:mysql://123.207.66.156:3306/pos?useSSL=false&autoReconnect=true&tinyInt1isBit=false&useUnicode=true&characterEncoding=utf8 + username: root + password: ****** + thymeleaf: + cache: false + +################### 自定义项目配置 ################### +xncoding: + apk-url-prefix: https://show.xncoding.net/files/ #APK文件访问URL前缀 + +logging: + level: + ROOT: INFO + com: + xncoding: DEBUG + file: /var/logs/app-manage-api.log diff --git a/springboot-swagger2/src/test/java/com/xncoding/jwt/ApplicationTests.java b/springboot-swagger2/src/test/java/com/xncoding/jwt/ApplicationTests.java new file mode 100644 index 0000000..7a7d2b8 --- /dev/null +++ b/springboot-swagger2/src/test/java/com/xncoding/jwt/ApplicationTests.java @@ -0,0 +1,15 @@ +package com.xncoding.jwt; + +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * 测试密码加密 + */ + +@RunWith(SpringRunner.class) +@SpringBootTest +public class ApplicationTests { + +} diff --git a/springboot-swagger2/src/test/java/com/xncoding/jwt/Swagger2MarkupTest.java b/springboot-swagger2/src/test/java/com/xncoding/jwt/Swagger2MarkupTest.java new file mode 100644 index 0000000..16fdfca --- /dev/null +++ b/springboot-swagger2/src/test/java/com/xncoding/jwt/Swagger2MarkupTest.java @@ -0,0 +1,112 @@ +package com.xncoding.jwt; + +import io.github.swagger2markup.GroupBy; +import io.github.swagger2markup.Language; +import io.github.swagger2markup.Swagger2MarkupConfig; +import io.github.swagger2markup.Swagger2MarkupConverter; +import io.github.swagger2markup.builder.Swagger2MarkupConfigBuilder; +import io.github.swagger2markup.markup.builder.MarkupLanguage; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; + +import java.io.BufferedWriter; +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +/** + * Swagger2MarkupTest + * + * @author XiongNeng + * @version 1.0 + * @since 2018/1/26 + */ +@AutoConfigureMockMvc +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class Swagger2MarkupTest { + @Autowired + private MockMvc mockMvc; + + private static final Logger LOG = LoggerFactory.getLogger(Swagger2MarkupTest.class); + + /** + * 自动生成adoc文件 + * @throws Exception e + * + * 执行完成后生成PDF文件方法: + * + * 首先在`build/swagger.adoc`的顶部加入: + ``` + :toclevels: 3 + :numbered: + ``` + 注意有个空行分割,目的是左边导航菜单是3级,并且自动加序号。 + 为了美化显示,还要将`swagger.adoc`中全局替换一下,将 + ``` + cols=".^2,.^3,.^9,.^4,.^2" + ``` + 替换成: + ``` + cols=".^2,.^3,.^6,.^4,.^5" + ``` + 然后在/resources目录下面执行: + ``` + asciidoctor-pdf -r asciidoctor-pdf-cjk-kai_gen_gothic -a pdf-style=KaiGenGothicCN swagger/swagger.adoc + ``` + 会在`swagger.adoc`的同级目录生成`swagger.pdf`文件。 + */ + @Test + public void createSpringFoxSwaggerJson() throws Exception { +// String outputDir = System.getProperty("swaggerOutputDir"); // mvn test + MvcResult mvcResult = this.mockMvc.perform(get("/v2/api-docs") + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andReturn(); + MockHttpServletResponse response = mvcResult.getResponse(); + String swaggerJson = response.getContentAsString(); +// Files.createDirectories(Paths.get(outputDir)); +// try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(outputDir, "swagger.json"), StandardCharsets.UTF_8)){ +// writer.write(swaggerJson); +// } + LOG.info("--------------------swaggerJson create --------------------"); + convertAsciidoc(swaggerJson); + LOG.info("--------------------swagon.json to asciiDoc finished --------------------"); + } + + /** + * 将swagger.yaml或swagger.json转换成漂亮的 AsciiDoc + * 访问:http://localhost:9095/v2/api-docs + * 将页面结果保存为src/main/resources/swagger.json + */ + private void convertAsciidoc(String swaggerStr) { +// Path localSwaggerFile = Paths.get(System.getProperty("swaggerOutputDir"), "swagger.json"); + Path outputFile = Paths.get(System.getProperty("asciiDocOutputDir")); + Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() + .withMarkupLanguage(MarkupLanguage.ASCIIDOC) + .withOutputLanguage(Language.ZH) + .withPathsGroupedBy(GroupBy.TAGS) + .withGeneratedExamples() + .withoutInlineSchema() + .build(); + Swagger2MarkupConverter converter = Swagger2MarkupConverter.from(swaggerStr) + .withConfig(config) + .build(); + converter.toFile(outputFile); + } +} diff --git a/springboot-swagger2/src/test/resources/keystore.jks b/springboot-swagger2/src/test/resources/keystore.jks new file mode 100644 index 0000000000000000000000000000000000000000..a714115b265f23b6e072e95aa91d1ee892bdc2c0 GIT binary patch literal 1334 zcmezO_TO6u1_mY|W&~rllGNgotL8FFTi2PkQ-$*RMNTD@?B(*yu80`TZw(iLXWCCGw{pP?BzV?t1+$ z%aISwOzWL2HCLNV-#6pt{2MbCeN#7?aZaN@*-v0rThp8EF*hVSn_|I zsyk!7e zYLnjVMQyt!b7n0(ZK>1nP-C;~QfZ~c3Ln~o7}?wj5;p*?f|A%)!!r?5_4{8;gL z)Zqo*eeHIOybj1FYfkcF=3%FX^9b2r(y*(Ge6#$!E& z^pyuXS69jBmcBCHwNEEy_0IG&GQAsWnRMr}-~0A|r_+YrwYx6PPUI5VE3tKn4A<8S z(^8LUnRPC43w~l|X;_?ni+^EpuNh0hjaNHQ{Ql85eb(IgWyvy|mX;mNPWU(@mRVZ) zs*}apq!L&4)Vv4lMYEk~u>_OEK}wHrYKpI5}JEVo=Z7w<;MkHQKL=FF%>S?SDJt=KuGdDutd+ z(Ed8-2+y?t@s)GGEV;^S_t=r=ao5RbPpVpV#qZj__SR53E<10Tnl-vN&S2tXm~2_bFEyv@g#j-cr&gOs+jm}Ix@KiCh%w|g;ACSCWnmL$att*T zHV_1HIC!{%^K(;mgG&-iQVkUiC6x+3B`JoY z2ErgE%sf2h<>h*yOr@8cpKBl|&TD97U}0=%Xl`U|Y#asVS{fTbxiq%DaXzwdf!Ub3 zv6sQ1v6HE>k>OIZNq+5w9d9|NE#K)Vz5LjMh?ljwY^<)Y9`yFs9h%hnwnA>h5)Jv) zCN2i&eIJKMy*oESn=>q-!7}UG4AtLCUp5P#E_jowQ1op2;#2NC6PMT7?2BJhCz7qq z@N`L~aLKL(K{GV}n%-FQW#@P2%VoJoEAv0dirYAuO*hK7lZj2-txt-0wanWV-{$=~;pSb^?#h&B0kAUe7shx_i zGoOeh#H@R$vcJhmpV7Cln)j5QQhD<1#f>v(t_f-T!}CsPe$!c=yrMV$)-TlAFETX# z-4yvhdtt>*Q)zjnr~cV4v#wi7E|l$7&UjqgZP_cc|Dp-A!NNBW7FgC8J-#kk@@x0% Gy#D|dcP%;q literal 0 HcmV?d00001