springboot接口开发小结
最近使用Springboot进行相关业务的开发,主要用它去提供GET和POST接口给外部进行调用,写了一个demo用于相关技术的验证。
项目基于Springboot1.4.3.RELEASE版本进行开发,接口生产消费的对象为JSON。
一.数据库表设计
首先进行数据库表设计,比较简单,
merchantId merchant_ip_list test_num
AA0b019201507241509254272 192.168.82.145,192.168.82.111 100.89
AA0b0192015072415092542712 192.168.82.145,192.168.82.111 100.89
AA0aaaaaaaaaaaaa123123123213213 192.168.82.145,192.168.82.111 100.89
主要存储三个字段,商户id,商户的ip列表,测试金额。
对应的建表SQL为
create table ESALES_MERCHANT_IP_LIST
(
MERCHANT_ID VARCHAR2(40) not null,
MERCHANT_IP_LIST VARCHAR2(512) not null,
TEST_NUM NUMBER(5,2)
)
二.具体业务开发
2.1 pom.xml
由于测试使用了oracle数据库,因此需要将oracle的jdbc驱动包安装到maven库中,使用如下命令安装
mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6
-Dversion=14.0.0 -Dpackaging=jar -Dfile=C:\Users\wuwl\Desktop\ojdbc14.jar
为方便测试,这里使用JDBCTemplate进行数据库的访问。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hispeed</groupId>
<artifactId>test-maven</artifactId>
<version>1.0.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>GBK</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<!-- spring-boot-start-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- common-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<!-- Oracle数据库jdbc -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>14.0.0</version>
</dependency>
<!-- JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- fastJson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.31</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2 编写Controller暴露访问接口RestControllerDemo.java
此处主要定义两个接口:
查询接口:GET data/search?p=xxxxxxxxxx
新增接口:POST data/insert (content-type=application/json)
@RestController
public class RestControllerDemo {
Logger logger = LoggerFactory.getLogger(RestControllerDemo.class);
@Autowired
DemoDao demoDao;
/**
* @param p "AA0b0192015072415092542712"
* @param request
* @return
*/
@RequestMapping(value = "data/search", method = RequestMethod.GET)
public ResponseParam getData(@RequestParam("p") String p, HttpServletRequest request) {
ResponseParam responseParam = new ResponseParam();
MerchantIpQueryVobj merchantIpQueryVobj = demoDao.getBean(p);
merchantIpQueryVobj.setMerchantIp(request.getRemoteHost());
responseParam.setData(merchantIpQueryVobj);
responseParam.setReturnCode("0000");
responseParam.setStatus("发送成功");
return responseParam;
}
@RequestMapping(value = "data/insert", method = RequestMethod.POST)
public ResponseParam insertData(@RequestBody MerchantIpQueryVobj merchantIpQueryVobj) {
ResponseParam responseParam = new ResponseParam();
int count = this.demoDao.insertData(merchantIpQueryVobj);
if (count > 0) {
responseParam.setReturnCode("0000");
responseParam.setStatus("发送成功");
responseParam.setData((String)"success");
logger.info(merchantIpQueryVobj.toString());
return responseParam;
}
responseParam.setData(merchantIpQueryVobj);
responseParam.setReturnCode("0001");
responseParam.setStatus("发送失败");
return responseParam;
}
}
class ResponseParam {
private String status;
private Object data;
private String returnCode;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public String getReturnCode() {
return returnCode;
}
public void setReturnCode(String returnCode) {
this.returnCode = returnCode;
}
}
2.2.1 查询接口/data/search?p
通过@RequestParam(“p”)获取请求的参数p,将p作为查询条件调用DAO方法进行数据的查询,
并将查询结果设置到一个内部类ResponseParam中,以JSON方式返回。
接口测试:GET访问http://localhost:8080/data/search?p=AA0b0192015072415092542712
{
status: "发送成功"
data: {
merchantId: "AA0b0192015072415092542712"
merchantIp: "0:0:0:0:0:0:0:1"
merchantIpList: "192.168.82.145,192.168.82.111"
money: 100.89
}
returnCode: "0000"
}
2.2.2 新增接口data/insert
由于Controller的注解为@RestController,此处就相当于在方法体上添加了@ResponseBody注解。因此此处未添加。
外部访问该接口需要设置Header中的Content-type为application/json。
在方法的参数定义中,为对象添加@RequestBody注解将外部传输的JSON映射为实体Bean。即MerchantIpQueryVobj,
执行DAO方法进行新增操作,如果返回的条数大于0表示插入成功。否则为插入失败。
接口测试:POST方式访问http://localhost:8080/data/insert
请求体添加如下内容
{
"merchantId": "AA0aaaaacccaaa213",
"merchantIp": "0:0:0:0:0:0:0:1",
"merchantIpList": "192.168.82.145,192.168.82.111",
"money": 100.89
}
点击提交,查看数据库中数据为
AA0aaaaacccaaa213 192.168.82.145,192.168.82.111 100.89 AAE7sIAAEAAB4f2AAA
表示新增成功。
2.3 数据库访问DAO类DemoDao.java
...省略部分代码
@Autowired
JdbcTemplate jdbcTemplate;
/**
* 数据查询方法
*/
public MerchantIpQueryVobj getBean(String id) {
String sql = "select t.merchant_id, t.merchant_ip_list, t.test_num from esales_merchant_ip_list t where t.merchant_id = ?";
final MerchantIpQueryVobj merchantIpQueryVobj = new MerchantIpQueryVobj();
this.jdbcTemplate.query(sql, new Object[]{id}, new RowCallbackHandler() {
public void processRow(ResultSet rs) throws SQLException {
merchantIpQueryVobj.setMerchantId(rs.getString("merchant_id"));
merchantIpQueryVobj.setMerchantIpList(rs.getString("merchant_ip_list"));
merchantIpQueryVobj.setMoney(rs.getFloat("test_num"));
}
});
System.out.println(merchantIpQueryVobj.toString());
return merchantIpQueryVobj;
}
/**
* 数据插入
*/
public int insertData(final MerchantIpQueryVobj merchantIpQueryVobj) {
String sql = "insert into esales_merchant_ip_list(merchant_id, merchant_ip_list, test_num) values(?,?,?)";
int count = this.jdbcTemplate.update(sql, new PreparedStatementSetter() {
public void setValues(PreparedStatement ps) throws SQLException {
ps.setString(1, merchantIpQueryVobj.getMerchantId());
ps.setString(2, merchantIpQueryVobj.getMerchantIpList());
ps.setFloat(3, merchantIpQueryVobj.getMoney());
}
});
return count;
}
主要使用了JdbcTemplate进行数据库访问,前提条件是需要在properties配置中添加数据库的相关配置如下
# 开发端口
server.port=8080
# datasource配置
spring.jpa.database=oracle
spring.jpd.show-sql=true
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@xxx.xx.xxx.xxx:1521:testdb
spring.datasource.username=xxxxxxxx
spring.datasource.password=oooooooo
spring.jpa.hibernate.ddl-auto=update
小结
Springboot的开箱即用的特性十分适合快速开发迭代的场景,使开发能够在短时间内即可将业务开发出来,而且对于Mock工程的
开发也是具有很积极的意义,对于需要联调环境的开发过程是很有益处的。
本文就如何使用Springboot进行接口服务的快速开发进行了较为详细的描述,并为之后的微服务化奠定了一定的基础。