Skip to content

Commit 33f127d

Browse files
committed
添加代码示例springboot-restful
1 parent 9f8444b commit 33f127d

File tree

13 files changed

+502
-24
lines changed

13 files changed

+502
-24
lines changed

README.md

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,32 @@ Spring Boot 现在已经成为Java 开发领域的一颗璀璨明珠,它本身
1515

1616
每个项目一篇博客文章的详细讲解,点击链接可以查看文章:
1717

18-
* springboot-thymeleaf [集成Thymeleaf构建Web应用](https://www.xncoding.com/2017/07/01/spring/sb-thymeleaf.html)
19-
* springboot-mybatis [集成MyBatis](https://www.xncoding.com/2017/07/02/spring/sb-mybatis.html)
20-
* springboot-hibernate [集成Hibernate](https://www.xncoding.com/2017/07/03/spring/sb-hibernate.html)
21-
* springboot-mongodb [集成MongoDB](https://www.xncoding.com/2017/07/04/spring/sb-mongodb.html)
22-
* springboot-restful [实现RESTful接口](https://www.xncoding.com/2017/07/05/spring/sb-restful.html)
23-
* springboot-resttemplate [使用RestTemplate](https://www.xncoding.com/2017/07/06/spring/sb-restclient.html)
24-
* springboot-shiro [集成Shiro权限管理](https://www.xncoding.com/2017/07/07/spring/sb-shiro.html)
25-
* springboot-swagger2 [集成Swagger2](https://www.xncoding.com/2017/07/08/spring/sb-swagger2.html)
26-
* springboot-jwt [集成JWT实现接口权限认证](https://www.xncoding.com/2017/07/09/spring/sb-jwt.html)
27-
* springboot-multisource[多数据源配置](https://www.xncoding.com/2017/07/10/spring/sb-multisource.html)
28-
* springboot-schedule [定时任务](https://www.xncoding.com/2017/07/12/spring/sb-schedule.html)
29-
* springboot-websocket [使用WebScoket实时通信](https://www.xncoding.com/2017/07/15/spring/sb-websocket.html)
30-
* springboot-socketio [集成SocketIO实时通信](https://www.xncoding.com/2017/07/16/spring/sb-socketio.html)
31-
* springboot-async [异步线程池](https://www.xncoding.com/2017/07/20/spring/sb-async.html)
32-
* springboot-starter [教你自己写starter](https://www.xncoding.com/2017/07/22/spring/sb-starter.html)
33-
* springboot-aop [使用AOP](https://www.xncoding.com/2017/07/24/spring/sb-aop.html)
34-
* springboot-transaction [声明式事务](https://www.xncoding.com/2017/07/26/spring/sb-transaction.html)
35-
* springboot-cache [使用缓存](https://www.xncoding.com/2017/07/28/spring/sb-cache.html)
36-
* springboot-redis [Redis数据库](https://www.xncoding.com/2017/07/30/spring/sb-redis.html)
37-
* springboot-batch [批处理](https://www.xncoding.com/2017/08/01/spring/sb-batch.html)
38-
* springboot-rabbitmq [集成消息队列RabbitMQ](https://www.xncoding.com/2017/08/06/spring/sb-rabbitmq.html)
39-
* springboot-echarts [集成Echarts导出图片](https://www.xncoding.com/2017/08/19/spring/sb-echarts.html)
40-
* app-manage [一个完整的Web后台管理系统,组合使用了多种技术]
41-
* app-manage-api [同时实现了需要认证授权访问的RESTful API接口和WebSocket接口]
18+
项目名称 | 文章地址
19+
----------------------------|------------------------------------------------------------------------------------------
20+
springboot-thymeleaf | [集成Thymeleaf构建Web应用](https://www.xncoding.com/2017/07/01/spring/sb-thymeleaf.html)
21+
springboot-mybatis | [集成MyBatis](https://www.xncoding.com/2017/07/02/spring/sb-mybatis.html)
22+
springboot-hibernate | [集成Hibernate](https://www.xncoding.com/2017/07/03/spring/sb-hibernate.html)
23+
springboot-mongodb | [集成MongoDB](https://www.xncoding.com/2017/07/04/spring/sb-mongodb.html)
24+
springboot-restful | [实现RESTful接口](https://www.xncoding.com/2017/07/05/spring/sb-restful.html)
25+
springboot-resttemplate | [使用RestTemplate](https://www.xncoding.com/2017/07/06/spring/sb-restclient.html)
26+
springboot-shiro | [集成Shiro权限管理](https://www.xncoding.com/2017/07/07/spring/sb-shiro.html)
27+
springboot-swagger2 | [集成Swagger2](https://www.xncoding.com/2017/07/08/spring/sb-swagger2.html)
28+
springboot-jwt | [集成JWT实现接口权限认证](https://www.xncoding.com/2017/07/09/spring/sb-jwt.html)
29+
springboot-multisource | [多数据源配置](https://www.xncoding.com/2017/07/10/spring/sb-multisource.html)
30+
springboot-schedule | [定时任务](https://www.xncoding.com/2017/07/12/spring/sb-schedule.html)
31+
springboot-websocket | [使用WebScoket实时通信](https://www.xncoding.com/2017/07/15/spring/sb-websocket.html)
32+
springboot-socketio | [集成SocketIO实时通信](https://www.xncoding.com/2017/07/16/spring/sb-socketio.html)
33+
springboot-async | [异步线程池](https://www.xncoding.com/2017/07/20/spring/sb-async.html)
34+
springboot-starter | [教你自己写starter](https://www.xncoding.com/2017/07/22/spring/sb-starter.html)
35+
springboot-aop | [使用AOP](https://www.xncoding.com/2017/07/24/spring/sb-aop.html)
36+
springboot-transaction | [声明式事务](https://www.xncoding.com/2017/07/26/spring/sb-transaction.html)
37+
springboot-cache | [使用缓存](https://www.xncoding.com/2017/07/28/spring/sb-cache.html)
38+
springboot-redis | [Redis数据库](https://www.xncoding.com/2017/07/30/spring/sb-redis.html)
39+
springboot-batch | [批处理](https://www.xncoding.com/2017/08/01/spring/sb-batch.html)
40+
springboot-rabbitmq | [集成消息队列RabbitMQ](https://www.xncoding.com/2017/08/06/spring/sb-rabbitmq.html)
41+
springboot-echarts | [集成Echarts导出图片](https://www.xncoding.com/2017/08/19/spring/sb-echarts.html)
42+
app-manage | [一个完整的Web后台管理系统,组合使用了多种技术]
43+
app-manage-api | [同时实现了需要认证授权访问的RESTful API接口和WebSocket接口]
4244

4345
## 环境
4446

springboot-restful/.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 此为注释– 将被Git 忽略
2+
# /结尾表示是目录,忽略目录和目录下的所有件
3+
# /开头表示根目录,否则是.gitignore的相对目录
4+
# !开头表示反选
5+
.idea/
6+
target/
7+
*.iml
8+
*.ipr
9+
*.iws
10+
*.log
11+
.svn/
12+
.project
13+
rebel.xml
14+
.rebel-remote.xml.*

springboot-restful/LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 Xiong Neng
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

springboot-restful/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## 实现RESTful API接口
2+
3+
利用SpringMVC实现RESTful API接口
4+
5+
## 许可证
6+
7+
Copyright (c) 2018 Xiong Neng
8+
9+
基于 MIT 协议发布: <http://www.opensource.org/licenses/MIT>

springboot-restful/pom.xml

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.xncoding</groupId>
8+
<artifactId>springboot-restful</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
12+
<name>springboot-restful</name>
13+
<description>实现RESTful API接口</description>
14+
15+
<parent>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-parent</artifactId>
18+
<version>1.5.9.RELEASE</version>
19+
<relativePath/>
20+
</parent>
21+
22+
<properties>
23+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
25+
<java.version>1.8</java.version>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-web</artifactId>
32+
<exclusions>
33+
<exclusion>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-tomcat</artifactId>
36+
</exclusion>
37+
</exclusions>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-jetty</artifactId>
42+
</dependency>
43+
</dependencies>
44+
45+
<build>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.apache.maven.plugins</groupId>
49+
<artifactId>maven-compiler-plugin</artifactId>
50+
<version>3.6.1</version>
51+
<configuration>
52+
<!--<proc>none</proc>-->
53+
<source>1.8</source>
54+
<target>1.8</target>
55+
</configuration>
56+
</plugin>
57+
<plugin>
58+
<groupId>org.apache.maven.plugins</groupId>
59+
<artifactId>maven-surefire-plugin</artifactId>
60+
<version>2.20</version>
61+
<configuration>
62+
<skip>true</skip>
63+
</configuration>
64+
</plugin>
65+
<plugin>
66+
<groupId>org.springframework.boot</groupId>
67+
<artifactId>spring-boot-maven-plugin</artifactId>
68+
<executions>
69+
</executions>
70+
</plugin>
71+
</plugins>
72+
73+
<resources>
74+
<resource>
75+
<directory>src/main/resources</directory>
76+
</resource>
77+
<resource>
78+
<directory>src/main/java</directory>
79+
<includes>
80+
<include>**/*.xml</include>
81+
</includes>
82+
</resource>
83+
</resources>
84+
</build>
85+
86+
</project>

springboot-restful/run.sh

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/bin/bash
2+
# 项目自动更新脚本
3+
# 先clone相应的分支下来:
4+
# git clone ssh://[email protected]:7999/xxx.git
5+
# 远程调试启动:
6+
# 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 &
7+
8+
function start {
9+
profile="$1"
10+
echo "启动环境profile=${profile}"
11+
jarfile=$(ls target/*.jar)
12+
if [[ "$?" == "0" ]]; then
13+
stop $profile $jarfile
14+
fi
15+
branch=$(git branch |awk '{print $2}')
16+
git pull origin ${branch}
17+
echo "更新完代码开始重新打包"
18+
mvn clean && mvn clean && mvn package -DskipTests=true
19+
if [[ "$?" != "0" ]]; then
20+
echo "编译出错,退出!"
21+
exit 1
22+
fi
23+
echo "nohup java -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 &"
24+
nohup java -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 &
25+
echo "启动应用中,请查看日志文件..."
26+
}
27+
28+
function stop {
29+
profile="$1"
30+
jarfile="$2"
31+
ps aux | grep "${jarfile}" | grep "spring.profiles.active=${profile}" | grep -v grep > /dev/null
32+
if [[ "$?" == "0" ]]; then
33+
echo "该应用还在跑,我先停了它"
34+
pid=$(ps aux | grep "${jarfile}" | grep "spring.profiles.active=${profile}" | grep -v grep |awk '{print $2}')
35+
if [[ "$pid" != "" ]]; then
36+
kill -9 $pid
37+
fi
38+
echo "停止应用成功..."
39+
fi
40+
}
41+
42+
if [[ "$1" == "start" ]]; then
43+
if [[ "$#" < 2 ]]; then
44+
echo "请输入正确参数:./epay.sh start {profile}"
45+
exit 1
46+
fi
47+
profile="$2"
48+
if [[ "$profile" != "dev" && "$profile" != "test" && "$profile" != "show" && "$profile" != "production" ]]; then
49+
echo "参数错误,请输入正确的profile参数,使用方法:"
50+
echo "./epay.sh start {profile} ==> 启动应用,{profile}取值:dev|test|show|production"
51+
exit 1
52+
fi
53+
start "${profile}"
54+
elif [[ "$1" == "stop" ]]; then
55+
if [[ "$#" < 2 ]]; then
56+
echo "请输入正确参数:./epay.sh stop {profile}"
57+
exit 1
58+
fi
59+
profile="$2"
60+
if [[ "$profile" != "dev" && "$profile" != "test" && "$profile" != "show" && "$profile" != "production" ]]; then
61+
echo "参数错误,请输入正确的profile参数,使用方法:"
62+
echo "./epay.sh stop {profile} ==> 停止应用,{profile}取值:dev|test|show|production"
63+
exit 1
64+
fi
65+
jarfile=$(ls target/*.jar)
66+
stop $profile $jarfile
67+
else
68+
echo "参数错误,使用方法:{}参数是必填的,[]参数可选"
69+
echo "./epay.sh start {profile} ==> 启动应用,{profile}取值:dev|test|show|production"
70+
echo "./epay.sh stop {profile} ==> 停止应用,{profile}取值:dev|test|show|production"
71+
exit 1
72+
fi
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.xncoding.pos;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
public static void main(String[] args) {
9+
SpringApplication.run(Application.class, args);
10+
}
11+
12+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.xncoding.pos.controller;
2+
3+
import com.xncoding.pos.model.LoginParam;
4+
import com.xncoding.pos.model.UnbindParam;
5+
import com.xncoding.pos.model.BaseResponse;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.web.bind.annotation.PostMapping;
9+
import org.springframework.web.bind.annotation.RequestBody;
10+
import org.springframework.web.bind.annotation.RequestHeader;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
/**
14+
* 登录接口类
15+
*/
16+
@RestController
17+
public class LoginController {
18+
19+
private static final Logger _logger = LoggerFactory.getLogger(LoginController.class);
20+
21+
@PostMapping("/login")
22+
public BaseResponse<String> login(@RequestHeader(name = "Content-Type", defaultValue = "application/json") String contentType,
23+
@RequestBody LoginParam loginParam) {
24+
_logger.info("用户请求登录获取Token");
25+
String username = loginParam.getUsername();
26+
String password = loginParam.getPassword();
27+
return new BaseResponse<>(true, "Login success", username + password);
28+
}
29+
30+
@PostMapping("/unbind")
31+
public BaseResponse<String> unbind(@RequestHeader(name = "Content-Type", defaultValue = "application/json") String contentType,
32+
@RequestHeader(name = "Authorization", defaultValue = "token") String token,
33+
@RequestBody UnbindParam unbindParam) {
34+
_logger.info("解绑通知接口start");
35+
String imei = unbindParam.getImei();
36+
String location = unbindParam.getLocation();
37+
return new BaseResponse<>(true, "解绑通知发送成功", "unbind");
38+
}
39+
40+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.xncoding.pos.model;
2+
3+
/**
4+
* Controller的基础返回类
5+
*
6+
* @author XiongNeng
7+
* @version 1.0
8+
* @since 2018/1/7
9+
*/
10+
public class BaseResponse<T> {
11+
/**
12+
* 是否成功
13+
*/
14+
private boolean success;
15+
16+
/**
17+
* 说明
18+
*/
19+
private String msg;
20+
21+
/**
22+
* 返回数据
23+
*/
24+
private T data;
25+
26+
public BaseResponse() {
27+
28+
}
29+
30+
public BaseResponse(boolean success, String msg, T data) {
31+
this.success = success;
32+
this.msg = msg;
33+
this.data = data;
34+
}
35+
36+
public boolean isSuccess() {
37+
return success;
38+
}
39+
40+
public void setSuccess(boolean success) {
41+
this.success = success;
42+
}
43+
44+
public String getMsg() {
45+
return msg;
46+
}
47+
48+
public void setMsg(String msg) {
49+
this.msg = msg;
50+
}
51+
52+
public T getData() {
53+
return data;
54+
}
55+
56+
public void setData(T data) {
57+
this.data = data;
58+
}
59+
60+
}

0 commit comments

Comments
 (0)