接口开发-restful
数据库表设计
1 --员工表
2 create table Employee
3 (
4 id NUMBER primary key,
5 employeeID NUMBER not null,
6 employeeName VARCHAR2(100) not null,
7 employeeSex VARCHAR2(100) not null,
8 employeeEmail VARCHAR2(100) not null,
9 employeeDepartmentID VARCHAR2(10) not null
10 )
11 create unique index Employee_UINDEX on Employee (employeeID) ---员工id唯一
12
15 drop sequence Sequence_Employee_id
16 --创建一个序列
17 create sequence Sequence_Employee_id
18 start with 1 --起始值是1000
19 increment by 1 --每次增量1
20 maxvalue 99999 --最大增量9999
实体类
1 package com.example.demo.api.restful.entity;
2
3 /**
4 * 员工主数据
5 *
6 * @author liuwenlong
7 * @create 2022-06-13 22:10:22
8 */
9 @SuppressWarnings("all")
10 public class Employee {
11
12 /**
13 * 员工ID
14 */
15 private Integer employeeID;
16
17 /**
18 * 员工姓名
19 */
20 private String employeeName;
21
22 /**
23 * 员工性别
24 */
25 private String employeeSex;
26
27 /**
28 * 员工邮箱
29 */
30 private String employeeEmail;
31
32 /**
33 * 员工部门ID
34 */
35 private Integer employeeDepartmentID;
36
37
38 public Employee() {
39 }
40
41 public Integer getEmployeeID() {
42 return employeeID;
43 }
44
45 public void setEmployeeID(Integer employeeID) {
46 this.employeeID = employeeID;
47 }
48
49 public String getEmployeeName() {
50 return employeeName;
51 }
52
53 public void setEmployeeName(String employeeName) {
54 this.employeeName = employeeName;
55 }
56
57 public String getEmployeeSex() {
58 return employeeSex;
59 }
60
61 public void setEmployeeSex(String employeeSex) {
62 this.employeeSex = employeeSex;
63 }
64
65 public String getEmployeeEmail() {
66 return employeeEmail;
67 }
68
69 public void setEmployeeEmail(String employeeEmail) {
70 this.employeeEmail = employeeEmail;
71 }
72
73 public Integer getEmployeeDepartmentID() {
74 return employeeDepartmentID;
75 }
76
77 public void setEmployeeDepartmentID(Integer employeeDepartmentID) {
78 this.employeeDepartmentID = employeeDepartmentID;
79 }
80
81 @Override
82 public String toString() {
83 return "Employee{" +
84 "employeeID=" + employeeID +
85 ", employeeName='" + employeeName + '\'' +
86 ", employeeSex='" + employeeSex + '\'' +
87 ", employeeEmail='" + employeeEmail + '\'' +
88 ", employeeDepartmentID=" + employeeDepartmentID +
89 '}';
90 }
91 }
控制类Controller
1 package com.example.demo.api.restful.controller;
2
3 import com.example.demo.api.restful.service.IRestfulService;
4 import net.sf.json.JSONObject;
5 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.web.bind.annotation.*;
7
8
9 /**
10 * restful接口
11 *
12 * @author liuwenlong
13 * @create 2022-06-13 14:49:56
14 */
15 @RestController
16 @SuppressWarnings("all")
17 @RequestMapping(value = "/restful/api")
18 public class RestfulController {
19
20 @Autowired
21 IRestfulService iRestfulService;
22
23 /**
24 * 员工主数据接入接口
25 *
26 * @param body
27 * @return
28 */
29 @RequestMapping(value = "employeeMasterData", method = RequestMethod.POST)
30 public JSONObject employeeMasterData(@RequestBody JSONObject body) {
31 return iRestfulService.employeeMasterData(body);
32 }
33 }
接口(Service)
1 package com.example.demo.api.restful.service;
2
3 import net.sf.json.JSONObject;
4 import org.springframework.web.bind.annotation.RequestBody;
5
6 /**
7 * @author liuwenlong
8 * @create 2022-06-13 22:47:37
9 */
10 @SuppressWarnings("all")
11 public interface IRestfulService {
12 /**
13 * 人员主数据接口接入
14 *
15 * @param body
16 * @return
17 */
18 JSONObject employeeMasterData(@RequestBody JSONObject body);
19 }
业务实现层(Impl)
1 package com.example.demo.api.restful.service.impl;
2
3 import com.example.demo.api.restful.dao.IRestfulMapper;
4 import com.example.demo.api.restful.entity.Employee;
5 import com.example.demo.api.entity.HEAD;
6 import com.example.demo.api.entity.LIST;
7 import com.example.demo.api.entity.Response;
8 import com.example.demo.api.restful.service.IRestfulService;
9 import net.sf.json.JSONArray;
10 import net.sf.json.JSONObject;
11 import org.springframework.beans.factory.annotation.Autowired;
12 import org.springframework.stereotype.Service;
13
14 import java.util.LinkedList;
15 import java.util.List;
16
17 /**
18 * @author liuwenlong
19 * @create 2022-06-13 22:48:52
20 */
21 @SuppressWarnings("all")
22 @Service
23 public class RestfulServiceImpl implements IRestfulService {
24
25 @Autowired
26 IRestfulMapper iRestfulMapper;
27
28
29 /**
30 * 人员主数据接口接入
31 *
32 * @param body
33 * @return
34 */
35 @Override
36 public JSONObject employeeMasterData(JSONObject body) {
37 try {
38 //JSONObject jsonObject = JSONObject.fromObject(body);//将json字符串转为json对象
39 String BIZTRANSACTIONID = body.getJSONObject("HEAD").getString("BIZTRANSACTIONID");
40 JSONArray requestList = body.getJSONArray("LIST");//得到上游请求来的LIST数组
41 List<Employee> employeeList = new LinkedList<>();//存放得到的人员信息
42
43 int successCount = 0;//多少条数据
44 int result = 0;//成功失败标识(默认)
45 String comments = "成功";//成功标识(默认)
46 String errorInfo = "";//失败标识
47 String errorCode = "";//失败代码
48
49 //组装反馈HEAD
50 HEAD responseHead = new HEAD();
51 //组装反馈LIST
52 List<LIST> responseLIST = new LinkedList<>();
53
54
55 for (int i = 0; i < requestList.size(); i++) {
56 int employeeID = Integer.parseInt(requestList.getJSONObject(i).getString("employeeID"));
57 String employeeName = requestList.getJSONObject(i).getString("employeeName");
58 String employeeSex = requestList.getJSONObject(i).getString("employeeSex");
59 String employeeEmail = requestList.getJSONObject(i).getString("employeeEmail");
60 int employeeDepartmentID = Integer.parseInt(requestList.getJSONObject(i).getString("employeeDepartmentID"));
61 Employee employee = new Employee();
62 employee.setEmployeeID(employeeID);
63 employee.setEmployeeName(employeeName);
64 employee.setEmployeeSex(employeeSex);
65 employee.setEmployeeEmail(employeeEmail);
66 employee.setEmployeeDepartmentID(employeeDepartmentID);
67
68 LIST errorList = new LIST();
69 try {
70 int insertDBresult = iRestfulMapper.employeeMasterData(employee);//插入数据库
71 successCount++;
72 errorList.setId(employeeID);
73 errorList.setMessage("success");
74 } catch (Exception e) {
75 System.out.println(e.getMessage());
76 errorInfo = "错误详情请参考list里对应错误提示";
77 result = 1;
78 errorList.setId(employeeID);
79 errorList.setMessage(e.getCause().toString());
80 }
81 responseLIST.add(errorList);
82 }
83
84 if (result == 1) {
85 result = 1;
86 comments = "接收失败";
87 }
88
89 //组装反馈HEAD
90 responseHead.setBIZTRANSACTIONID(BIZTRANSACTIONID);
91 responseHead.setRESULT(result);
92 responseHead.setCOMMENTS(comments);
93 responseHead.setERRORCODE(errorCode);
94 responseHead.setERRORINFO(errorInfo);
95 responseHead.setSUCCESSCOUNT(successCount);
96
97 //组装完整反馈信息
98 Response responseInfo = new Response();
99 responseInfo.setHEAD(responseHead);
100 responseInfo.setLIST(responseLIST);
101
102 return JSONObject.fromObject(responseInfo);
103
104 } catch (Exception e) {
105 String error = "{\"error\":\"" + e.getMessage().replace("\"", "\\\"") + "\"}";
106 return JSONObject.fromObject(error);
107 }
108 }
109 }
DAO
1 package com.example.demo.api.restful.dao;
2
3 import com.example.demo.api.restful.entity.Employee;
4
5 /**
6 * @author liuwenlong
7 * @create 2022-06-13 22:54:11
8 */
9 @SuppressWarnings("all")
10 public interface IRestfulMapper {
11
12 /**
13 * 人员主数据接口接入
14 *
15 * @param body
16 * @return
17 */
18 int employeeMasterData(Employee employee);
19 }
Mapper
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
3 <mapper namespace="com.example.demo.api.restful.dao.IRestfulMapper">
4
5 <!--人员主数据接入-->
6 <insert id="employeeMasterData" parameterType="com.example.demo.api.restful.entity.Employee">
7 insert into Employee(id,employeeID,employeeName,employeeSex,employeeEmail,employeeDepartmentID)
8 values(
9 Sequence_Employee_id.NEXTVAL,
10 #{employeeID},
11 #{employeeName},
12 #{employeeSex},
13 #{employeeEmail},
14 #{employeeDepartmentID}
15 )
16 </insert>
17 </mapper>
数据库连接
1 #连接Oracle数据库
2 spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
3 spring.datasource.url=jdbc:oracle:thin:@localhost:1521/orcl
4 spring.datasource.username=system
5 spring.datasource.password=123456
6
7 #加载Mybatis的xml 在:resources下
8 mybatis.mapper-locations=classpath*:/com/example/demo/api/restful/dao/*.xml
请求参数说明
1 接口名称:人员主数据接入接口
2 请求地址:http://localhost:8001/restful/api/employeeMasterData
3 请求方式:post
4 传输类型:application/json
请求报文示例
1 {
2 "HEAD": {
3 "BIZTRANSACTIONID": "PMS_ES_001_20210304110203",
4 "COUNT": "1",
5 "CONSUMER": "ES",
6 "SRVLEVEL": "1",
7 "ACCOUNT": "",
8 "PASSWORD": "",
9 "TRANSID": ""
10 },
11 "LIST": [{
12 "employeeID": 10001,
13 "employeeName": "张东",
14 "employeeSex": "男",
15 "employeeEmail": "zhangd@123.com",
16 "employeeDepartmentID": 1
17 }, {
18 "employeeID": 10002,
19 "employeeName": "肖瑾",
20 "employeeSex": "女",
21 "employeeEmail": "xiaoj@123.com",
22 "employeeDepartmentID": 2
23 }, {
24 "employeeID": 10003,
25 "employeeName": "孙凯凯",
26 "employeeSex": "男",
27 "employeeEmail": "sunkk@123.com",
28 "employeeDepartmentID": 3
29 }
30 ]
31 }
响应报文示例
1 响应成功报文示例:
2 {
3 "HEAD": {
4 "BIZTRANSACTIONID": "PMS_ES_001_20210304110203",
5 "COMMENTS": "成功",
6 "ERRORCODE": "",
7 "ERRORINFO": "",
8 "RESULT": 0,
9 "SUCCESSCOUNT": 3
10 },
11 "LIST": [
12 {
13 "id": 11001,
14 "message": "success"
15 },
16 {
17 "id": 11002,
18 "message": "success"
19 },
20 {
21 "id": 11003,
22 "message": "success"
23 }
24 ]
25 }
26
27 响应失败报文样例
28
29 {
30 "HEAD": {
31 "BIZTRANSACTIONID": "PMS_ES_001_20210304110203",
32 "COMMENTS": "接收失败",
33 "ERRORCODE": "",
34 "ERRORINFO": "错误详情请参考list里对应错误提示",
35 "RESULT": 1,
36 "SUCCESSCOUNT": 1
37 },
38 "LIST": [
39 {
40 "id": 11004,
41 "message": "success"
42 },
43 {
44 "id": 11002,
45 "message": "java.sql.SQLIntegrityConstraintViolationException: ORA-00001: 违反唯一约束条件 (SYSTEM.EMPLOYEE_UINDEX)\n"
46 },
47 {
48 "id": 11003,
49 "message": "java.sql.SQLIntegrityConstraintViolationException: ORA-00001: 违反唯一约束条件 (SYSTEM.EMPLOYEE_UINDEX)\n"
50 }
51 ]
52 }
测试
接口开发-restful的更多相关文章
- 用cxf开发restful风格的WebService
我们都知道cxf还可以开发restful风格的webService,下面是利用maven+spring4+cxf搭建webService服务端和客户端Demo 1.pom.xml <projec ...
- 开发RESTful WebService
RESTful风格的webservice越来越流行了,sun也推出了RESTful WebService的官方规范:JAX-RS,全称:Java API for RESTful WebService. ...
- flask开发restful api系列(8)-再谈项目结构
上一章,我们讲到,怎么用蓝图建造一个好的项目,今天我们继续深入.上一章中,我们所有的接口都写在view.py中,如果几十个,还稍微好管理一点,假如上百个,上千个,怎么找?所有接口堆在一起就显得杂乱无章 ...
- flask开发restful api
flask开发restful api 如果有几个原因可以让你爱上flask这个极其灵活的库,我想蓝图绝对应该算上一个,部署蓝图以后,你会发现整个程序结构非常清晰,模块之间相互不影响.蓝图对restfu ...
- API接口开发 配置、实现、测试
Yii2 基于RESTful架构的 advanced版API接口开发 配置.实现.测试 环境配置: 开启服务器伪静态 本处以apache为例,查看apache的conf目录下httpd.conf,找到 ...
- Python自动化开发 - RESTful API
本节内容 1. RESTful 简介 2. RESTful 设计指南 3. Django REST Framework 最佳实践 4. 理论拓展与开放平台 5. API文档化与测试 一 R ...
- web基础----->jersey整合jetty开发restful应用(一)
这里介绍一个jersey与jetty整合开发restful应用的知识.将过去和羁绊全部丢弃,不要吝惜那为了梦想流下的泪水. jersey与jetty的整合 一.创建一个maven项目,pom.xml的 ...
- 使用Spring boot开发RestFul 风格项目PUT/DELETE方法不起作用
在使用Spring boot 开发restful 风格的项目,put.delete方法不起作用,解决办法. 实体类Student @Data public class Student { privat ...
- 使用CXF开发RESTFul服务
相信大家在阅读CXF官方文档(http://cxf.apache.org/docs/index.html)时,总是一知半解.这里向大家推荐一本PacktPub.Apache.CXF.Web.Servi ...
随机推荐
- linux中find与三剑客之grep用法
find用法 find一般用来用来查找文件名称 根据文件的名称或者属性查找文件. 语法格式: find [查找范围] [参数] 参数: -name : 按照文件的名字查找文件 * :通配符 -inam ...
- springboot读取配置文件赋值给静态变量
1.实现InitializingBean接口,重写afterPropertiesSet方法,将@Value赋值给成员变量的属性赋值给静态变量,示例如下: /** * @Classname FileUt ...
- (2020行人再识别综述)Person Re-Identification using Deep Learning Networks: A Systematic Review
目录 1.引言 2.研究方法 2.1本次综述的贡献 2.2综述方法 2.3与现有综述的比较 3.行人再识别基准数据集 3.1基于图像的再识别数据集 3.2基于视频的再识别数据集 4.基于图像的深度再识 ...
- 攻防世界-MISC:神奇的Modbus
这是攻防世界高手进阶区的第三题,题目如下: 点击下载附件一,得到一个流量包,题目中提到的modbus,百度百科的解释如下: 用wireshark打开流量包,搜索modbus 然后鼠标右键选择追踪流,再 ...
- 【mq】从零开始实现 mq-05-实现优雅停机
前景回顾 [mq]从零开始实现 mq-01-生产者.消费者启动 [mq]从零开始实现 mq-02-如何实现生产者调用消费者? [mq]从零开始实现 mq-03-引入 broker 中间人 [mq]从零 ...
- drools中Fact的equality modes
一.equality modes介绍 在drools中存在如下2种equality modes. 1.identity模式 identity:这是默认的情况.drools引擎使用IdentityHas ...
- 项目实战:Qt+OpenCV大家来找茬(Qt抓图,穿透应用,识别左右图区别,框选区别,微调位置)
前言 本项目的出现理由只是笔者的一个念头,于是利用专业Qt和Opencv相关的知识开发一个辅助工具,本文章仅用于Qt和Opencv结合的学习. Demo演示效果 运行包下载地 ...
- 组织:EFF
电子前沿基金会(Electronic Frontier Foundation), 简称EFF,是一个非营利性的国际法律组织.该组织成立于1990年,创始人包括Mitch Kapor(Lotus公司的总 ...
- 6. ZigZag Conversion - LeetCode
Question 6. ZigZag Conversion Solution 题目大意:将字符串按Z字型排列,然后再一行一行按字符输出 思路:按题目中的第一个例子,画出如下图,通过n的不同值,可以找出 ...
- HIVE 数据分析
题目要求: 具体操作: ①hive路径下建表:sale create table sale (day_id String, sale_nbr String, buy_nbr String, cnt S ...