Skip to content

Commit 9f8444b

Browse files
committed
添加代码示例springboot-resttemplate
1 parent 465ad8d commit 9f8444b

File tree

14 files changed

+631
-0
lines changed

14 files changed

+631
-0
lines changed

springboot-resttemplate/.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-resttemplate/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-resttemplate/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## 使用RestTemplate
2+
3+
使用RestTemplate访问RESTful API接口
4+
5+
## 测试用例
6+
7+
`com.xncoding.pos.ApplicationTests.java`
8+
9+
## 许可证
10+
11+
Copyright (c) 2018 Xiong Neng
12+
13+
基于 MIT 协议发布: <http://www.opensource.org/licenses/MIT>

springboot-resttemplate/pom.xml

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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-resttemplate</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
12+
<name>springboot-resttemplate</name>
13+
<description>使用RestTemplate访问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+
<dependency>
44+
<groupId>org.apache.httpcomponents</groupId>
45+
<artifactId>httpclient</artifactId>
46+
<version>4.3.6</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-test</artifactId>
51+
<scope>test</scope>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.hamcrest</groupId>
55+
<artifactId>hamcrest-core</artifactId>
56+
<version>1.3</version>
57+
<scope>test</scope>
58+
</dependency>
59+
60+
</dependencies>
61+
62+
<build>
63+
<plugins>
64+
<plugin>
65+
<groupId>org.apache.maven.plugins</groupId>
66+
<artifactId>maven-compiler-plugin</artifactId>
67+
<version>3.6.1</version>
68+
<configuration>
69+
<!--<proc>none</proc>-->
70+
<source>1.8</source>
71+
<target>1.8</target>
72+
</configuration>
73+
</plugin>
74+
<plugin>
75+
<groupId>org.apache.maven.plugins</groupId>
76+
<artifactId>maven-surefire-plugin</artifactId>
77+
<version>2.20</version>
78+
<configuration>
79+
<skip>true</skip>
80+
</configuration>
81+
</plugin>
82+
<plugin>
83+
<groupId>org.springframework.boot</groupId>
84+
<artifactId>spring-boot-maven-plugin</artifactId>
85+
<executions>
86+
</executions>
87+
</plugin>
88+
</plugins>
89+
90+
<resources>
91+
<resource>
92+
<directory>src/main/resources</directory>
93+
</resource>
94+
<resource>
95+
<directory>src/main/java</directory>
96+
<includes>
97+
<include>**/*.xml</include>
98+
</includes>
99+
</resource>
100+
</resources>
101+
</build>
102+
103+
</project>

springboot-resttemplate/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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.xncoding.pos.config;
2+
3+
import org.apache.http.client.HttpClient;
4+
import org.apache.http.client.config.RequestConfig;
5+
import org.apache.http.config.Registry;
6+
import org.apache.http.config.RegistryBuilder;
7+
import org.apache.http.conn.socket.ConnectionSocketFactory;
8+
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
9+
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
10+
import org.apache.http.impl.client.HttpClientBuilder;
11+
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
12+
import org.springframework.boot.web.client.RestTemplateBuilder;
13+
import org.springframework.context.annotation.Bean;
14+
import org.springframework.context.annotation.Configuration;
15+
import org.springframework.http.client.ClientHttpRequestFactory;
16+
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
17+
import org.springframework.web.client.RestTemplate;
18+
19+
/**
20+
* RestTemplate客户端连接池配置
21+
*
22+
* @author XiongNeng
23+
* @version 1.0
24+
* @since 2018/1/24
25+
*/
26+
@Configuration
27+
public class RestClientConfig {
28+
@Bean
29+
public ClientHttpRequestFactory httpRequestFactory() {
30+
return new HttpComponentsClientHttpRequestFactory(httpClient());
31+
}
32+
33+
@Bean
34+
public RestTemplate restTemplate(RestTemplateBuilder builder) {
35+
return new RestTemplate(httpRequestFactory());
36+
}
37+
38+
@Bean
39+
public HttpClient httpClient() {
40+
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
41+
.register("http", PlainConnectionSocketFactory.getSocketFactory())
42+
.register("https", SSLConnectionSocketFactory.getSocketFactory())
43+
.build();
44+
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
45+
connectionManager.setMaxTotal(5);
46+
connectionManager.setDefaultMaxPerRoute(5);
47+
48+
RequestConfig requestConfig = RequestConfig.custom()
49+
.setSocketTimeout(8000)
50+
.setConnectTimeout(8000)
51+
.setConnectionRequestTimeout(8000)
52+
.build();
53+
54+
return HttpClientBuilder.create()
55+
.setDefaultRequestConfig(requestConfig)
56+
.setConnectionManager(connectionManager)
57+
.build();
58+
}
59+
}
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+
}

0 commit comments

Comments
 (0)