From 33f127db44d8c8288b57e1e5f68cc3f320c2b7df Mon Sep 17 00:00:00 2001 From: yidao620 Date: Wed, 28 Feb 2018 10:46:09 +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-restful?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 50 +++++------ springboot-restful/.gitignore | 14 +++ springboot-restful/LICENSE | 20 +++++ springboot-restful/README.md | 9 ++ springboot-restful/pom.xml | 86 +++++++++++++++++++ springboot-restful/run.sh | 72 ++++++++++++++++ .../java/com/xncoding/pos/Application.java | 12 +++ .../pos/controller/LoginController.java | 40 +++++++++ .../com/xncoding/pos/model/BaseResponse.java | 60 +++++++++++++ .../com/xncoding/pos/model/LoginParam.java | 59 +++++++++++++ .../com/xncoding/pos/model/UnbindParam.java | 35 ++++++++ .../src/main/resources/application.yml | 46 ++++++++++ .../src/main/resources/banner.txt | 23 +++++ 13 files changed, 502 insertions(+), 24 deletions(-) create mode 100644 springboot-restful/.gitignore create mode 100644 springboot-restful/LICENSE create mode 100644 springboot-restful/README.md create mode 100644 springboot-restful/pom.xml create mode 100644 springboot-restful/run.sh create mode 100644 springboot-restful/src/main/java/com/xncoding/pos/Application.java create mode 100644 springboot-restful/src/main/java/com/xncoding/pos/controller/LoginController.java create mode 100644 springboot-restful/src/main/java/com/xncoding/pos/model/BaseResponse.java create mode 100644 springboot-restful/src/main/java/com/xncoding/pos/model/LoginParam.java create mode 100644 springboot-restful/src/main/java/com/xncoding/pos/model/UnbindParam.java create mode 100644 springboot-restful/src/main/resources/application.yml create mode 100644 springboot-restful/src/main/resources/banner.txt diff --git a/README.md b/README.md index c9c0bb9..a144f57 100644 --- a/README.md +++ b/README.md @@ -15,30 +15,32 @@ Spring Boot 现在已经成为Java 开发领域的一颗璀璨明珠,它本身 每个项目一篇博客文章的详细讲解,点击链接可以查看文章: -* springboot-thymeleaf [集成Thymeleaf构建Web应用](https://www.xncoding.com/2017/07/01/spring/sb-thymeleaf.html) -* springboot-mybatis [集成MyBatis](https://www.xncoding.com/2017/07/02/spring/sb-mybatis.html) -* springboot-hibernate [集成Hibernate](https://www.xncoding.com/2017/07/03/spring/sb-hibernate.html) -* springboot-mongodb [集成MongoDB](https://www.xncoding.com/2017/07/04/spring/sb-mongodb.html) -* springboot-restful [实现RESTful接口](https://www.xncoding.com/2017/07/05/spring/sb-restful.html) -* springboot-resttemplate [使用RestTemplate](https://www.xncoding.com/2017/07/06/spring/sb-restclient.html) -* springboot-shiro [集成Shiro权限管理](https://www.xncoding.com/2017/07/07/spring/sb-shiro.html) -* springboot-swagger2 [集成Swagger2](https://www.xncoding.com/2017/07/08/spring/sb-swagger2.html) -* springboot-jwt [集成JWT实现接口权限认证](https://www.xncoding.com/2017/07/09/spring/sb-jwt.html) -* springboot-multisource[多数据源配置](https://www.xncoding.com/2017/07/10/spring/sb-multisource.html) -* springboot-schedule [定时任务](https://www.xncoding.com/2017/07/12/spring/sb-schedule.html) -* springboot-websocket [使用WebScoket实时通信](https://www.xncoding.com/2017/07/15/spring/sb-websocket.html) -* springboot-socketio [集成SocketIO实时通信](https://www.xncoding.com/2017/07/16/spring/sb-socketio.html) -* springboot-async [异步线程池](https://www.xncoding.com/2017/07/20/spring/sb-async.html) -* springboot-starter [教你自己写starter](https://www.xncoding.com/2017/07/22/spring/sb-starter.html) -* springboot-aop [使用AOP](https://www.xncoding.com/2017/07/24/spring/sb-aop.html) -* springboot-transaction [声明式事务](https://www.xncoding.com/2017/07/26/spring/sb-transaction.html) -* springboot-cache [使用缓存](https://www.xncoding.com/2017/07/28/spring/sb-cache.html) -* springboot-redis [Redis数据库](https://www.xncoding.com/2017/07/30/spring/sb-redis.html) -* springboot-batch [批处理](https://www.xncoding.com/2017/08/01/spring/sb-batch.html) -* springboot-rabbitmq [集成消息队列RabbitMQ](https://www.xncoding.com/2017/08/06/spring/sb-rabbitmq.html) -* springboot-echarts [集成Echarts导出图片](https://www.xncoding.com/2017/08/19/spring/sb-echarts.html) -* app-manage [一个完整的Web后台管理系统,组合使用了多种技术] -* app-manage-api [同时实现了需要认证授权访问的RESTful API接口和WebSocket接口] +项目名称 | 文章地址 +----------------------------|------------------------------------------------------------------------------------------ +springboot-thymeleaf | [集成Thymeleaf构建Web应用](https://www.xncoding.com/2017/07/01/spring/sb-thymeleaf.html) +springboot-mybatis | [集成MyBatis](https://www.xncoding.com/2017/07/02/spring/sb-mybatis.html) +springboot-hibernate | [集成Hibernate](https://www.xncoding.com/2017/07/03/spring/sb-hibernate.html) +springboot-mongodb | [集成MongoDB](https://www.xncoding.com/2017/07/04/spring/sb-mongodb.html) +springboot-restful | [实现RESTful接口](https://www.xncoding.com/2017/07/05/spring/sb-restful.html) +springboot-resttemplate | [使用RestTemplate](https://www.xncoding.com/2017/07/06/spring/sb-restclient.html) +springboot-shiro | [集成Shiro权限管理](https://www.xncoding.com/2017/07/07/spring/sb-shiro.html) +springboot-swagger2 | [集成Swagger2](https://www.xncoding.com/2017/07/08/spring/sb-swagger2.html) +springboot-jwt | [集成JWT实现接口权限认证](https://www.xncoding.com/2017/07/09/spring/sb-jwt.html) +springboot-multisource | [多数据源配置](https://www.xncoding.com/2017/07/10/spring/sb-multisource.html) +springboot-schedule | [定时任务](https://www.xncoding.com/2017/07/12/spring/sb-schedule.html) +springboot-websocket | [使用WebScoket实时通信](https://www.xncoding.com/2017/07/15/spring/sb-websocket.html) +springboot-socketio | [集成SocketIO实时通信](https://www.xncoding.com/2017/07/16/spring/sb-socketio.html) +springboot-async | [异步线程池](https://www.xncoding.com/2017/07/20/spring/sb-async.html) +springboot-starter | [教你自己写starter](https://www.xncoding.com/2017/07/22/spring/sb-starter.html) +springboot-aop | [使用AOP](https://www.xncoding.com/2017/07/24/spring/sb-aop.html) +springboot-transaction | [声明式事务](https://www.xncoding.com/2017/07/26/spring/sb-transaction.html) +springboot-cache | [使用缓存](https://www.xncoding.com/2017/07/28/spring/sb-cache.html) +springboot-redis | [Redis数据库](https://www.xncoding.com/2017/07/30/spring/sb-redis.html) +springboot-batch | [批处理](https://www.xncoding.com/2017/08/01/spring/sb-batch.html) +springboot-rabbitmq | [集成消息队列RabbitMQ](https://www.xncoding.com/2017/08/06/spring/sb-rabbitmq.html) +springboot-echarts | [集成Echarts导出图片](https://www.xncoding.com/2017/08/19/spring/sb-echarts.html) +app-manage | [一个完整的Web后台管理系统,组合使用了多种技术] +app-manage-api | [同时实现了需要认证授权访问的RESTful API接口和WebSocket接口] ## 环境 diff --git a/springboot-restful/.gitignore b/springboot-restful/.gitignore new file mode 100644 index 0000000..1e3c5bb --- /dev/null +++ b/springboot-restful/.gitignore @@ -0,0 +1,14 @@ +# 此为注释– 将被Git 忽略 +# /结尾表示是目录,忽略目录和目录下的所有件 +# /开头表示根目录,否则是.gitignore的相对目录 +# !开头表示反选 +.idea/ +target/ +*.iml +*.ipr +*.iws +*.log +.svn/ +.project +rebel.xml +.rebel-remote.xml.* diff --git a/springboot-restful/LICENSE b/springboot-restful/LICENSE new file mode 100644 index 0000000..83cd47d --- /dev/null +++ b/springboot-restful/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-restful/README.md b/springboot-restful/README.md new file mode 100644 index 0000000..9edd7cf --- /dev/null +++ b/springboot-restful/README.md @@ -0,0 +1,9 @@ +## 实现RESTful API接口 + +利用SpringMVC实现RESTful API接口 + +## 许可证 + +Copyright (c) 2018 Xiong Neng + +基于 MIT 协议发布: diff --git a/springboot-restful/pom.xml b/springboot-restful/pom.xml new file mode 100644 index 0000000..c6c0247 --- /dev/null +++ b/springboot-restful/pom.xml @@ -0,0 +1,86 @@ + + + 4.0.0 + + com.xncoding + springboot-restful + 1.0.0-SNAPSHOT + jar + + springboot-restful + 实现RESTful API接口 + + + 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.apache.maven.plugins + maven-compiler-plugin + 3.6.1 + + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.20 + + 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-restful/run.sh b/springboot-restful/run.sh new file mode 100644 index 0000000..955efb1 --- /dev/null +++ b/springboot-restful/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-restful/src/main/java/com/xncoding/pos/Application.java b/springboot-restful/src/main/java/com/xncoding/pos/Application.java new file mode 100644 index 0000000..adc4c48 --- /dev/null +++ b/springboot-restful/src/main/java/com/xncoding/pos/Application.java @@ -0,0 +1,12 @@ +package com.xncoding.pos; + +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-restful/src/main/java/com/xncoding/pos/controller/LoginController.java b/springboot-restful/src/main/java/com/xncoding/pos/controller/LoginController.java new file mode 100644 index 0000000..7ec4e60 --- /dev/null +++ b/springboot-restful/src/main/java/com/xncoding/pos/controller/LoginController.java @@ -0,0 +1,40 @@ +package com.xncoding.pos.controller; + +import com.xncoding.pos.model.LoginParam; +import com.xncoding.pos.model.UnbindParam; +import com.xncoding.pos.model.BaseResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RestController; + +/** + * 登录接口类 + */ +@RestController +public class LoginController { + + private static final Logger _logger = LoggerFactory.getLogger(LoginController.class); + + @PostMapping("/login") + public BaseResponse login(@RequestHeader(name = "Content-Type", defaultValue = "application/json") String contentType, + @RequestBody LoginParam loginParam) { + _logger.info("用户请求登录获取Token"); + String username = loginParam.getUsername(); + String password = loginParam.getPassword(); + return new BaseResponse<>(true, "Login success", username + password); + } + + @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"); + String imei = unbindParam.getImei(); + String location = unbindParam.getLocation(); + return new BaseResponse<>(true, "解绑通知发送成功", "unbind"); + } + +} diff --git a/springboot-restful/src/main/java/com/xncoding/pos/model/BaseResponse.java b/springboot-restful/src/main/java/com/xncoding/pos/model/BaseResponse.java new file mode 100644 index 0000000..b7c3e5e --- /dev/null +++ b/springboot-restful/src/main/java/com/xncoding/pos/model/BaseResponse.java @@ -0,0 +1,60 @@ +package com.xncoding.pos.model; + +/** + * Controller的基础返回类 + * + * @author XiongNeng + * @version 1.0 + * @since 2018/1/7 + */ +public class BaseResponse { + /** + * 是否成功 + */ + private boolean success; + + /** + * 说明 + */ + private String msg; + + /** + * 返回数据 + */ + 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-restful/src/main/java/com/xncoding/pos/model/LoginParam.java b/springboot-restful/src/main/java/com/xncoding/pos/model/LoginParam.java new file mode 100644 index 0000000..466e8e3 --- /dev/null +++ b/springboot-restful/src/main/java/com/xncoding/pos/model/LoginParam.java @@ -0,0 +1,59 @@ +package com.xncoding.pos.model; + +/** + * 登录认证接口参数 + * + * @author XiongNeng + * @version 1.0 + * @since 2018/1/9 + */ +public class LoginParam { + /** + * 用户名 + */ + private String username; + /** + * 密码 + */ + private String password; + /** + * Application ID + */ + private String appid; + /** + * IMEI码 + */ + 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-restful/src/main/java/com/xncoding/pos/model/UnbindParam.java b/springboot-restful/src/main/java/com/xncoding/pos/model/UnbindParam.java new file mode 100644 index 0000000..c8f5737 --- /dev/null +++ b/springboot-restful/src/main/java/com/xncoding/pos/model/UnbindParam.java @@ -0,0 +1,35 @@ +package com.xncoding.pos.model; + +/** + * 解绑通知参数 + * + * @author XiongNeng + * @version 1.0 + * @since 2018/1/9 + */ +public class UnbindParam { + /** + * IMEI码 + */ + private String imei; + /** + * 网点 + */ + 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-restful/src/main/resources/application.yml b/springboot-restful/src/main/resources/application.yml new file mode 100644 index 0000000..ea74468 --- /dev/null +++ b/springboot-restful/src/main/resources/application.yml @@ -0,0 +1,46 @@ +########################################################## +################## 所有profile共有的配置 ################# +########################################################## + +################### 项目启动端口 ################### +server.port: 8092 + +################### spring配置 ################### +spring: + profiles: + active: dev + +logging: + level: + org.springframework.web.servlet: ERROR + +--- + +##################################################################### +######################## 开发环境profile ########################## +##################################################################### +spring: + profiles: dev + +logging: + level: + ROOT: INFO + com: + xncoding: DEBUG + file: E:/logs/app.log + +--- + +##################################################################### +######################## 测试环境profile ########################## +##################################################################### + +spring: + profiles: test + +logging: + level: + ROOT: INFO + com: + xncoding: DEBUG + file: /var/logs/app.log diff --git a/springboot-restful/src/main/resources/banner.txt b/springboot-restful/src/main/resources/banner.txt new file mode 100644 index 0000000..859b78f --- /dev/null +++ b/springboot-restful/src/main/resources/banner.txt @@ -0,0 +1,23 @@ + + _____ _______ _____ _____ + /\ \ /::\ \ /\ \ /\ \ + /::\____\ /::::\ \ /::\____\ /::\ \ + /:::/ / /::::::\ \ /:::/ / /::::\ \ + /:::/ / /::::::::\ \ /:::/ / /::::::\ \ + /:::/ / /:::/~~\:::\ \ /:::/ / /:::/\:::\ \ + /:::/ / /:::/ \:::\ \ /:::/____/ /:::/__\:::\ \ + /:::/ / /:::/ / \:::\ \ |::| | /::::\ \:::\ \ + /:::/ / /:::/____/ \:::\____\ |::| | _____ /::::::\ \:::\ \ + /:::/ / |:::| | |:::| | |::| | /\ \ /:::/\:::\ \:::\ \ +/:::/____/ |:::|____| |:::| | |::| | /::\____\/:::/__\:::\ \:::\____\ +\:::\ \ \:::\ \ /:::/ / |::| | /:::/ /\:::\ \:::\ \::/ / + \:::\ \ \:::\ \ /:::/ / |::| | /:::/ / \:::\ \:::\ \/____/ + \:::\ \ \:::\ /:::/ / |::|____|/:::/ / \:::\ \:::\ \ + \:::\ \ \:::\__/:::/ / |:::::::::::/ / \:::\ \:::\____\ + \:::\ \ \::::::::/ / \::::::::::/____/ \:::\ \::/ / + \:::\ \ \::::::/ / ~~~~~~~~~~ \:::\ \/____/ + \:::\ \ \::::/ / \:::\ \ + \:::\____\ \::/____/ \:::\____\ + \::/ / ~~ \::/ / + \/____/ \/____/ +