? 优质资源分享 ?
学习路线指引(点击解锁) | 知识定位 | 人群定位 |
---|---|---|
? Python实战微信订餐小程序 ? | 进阶级 | 本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯云部署上线,打造一个全栈订餐系统。 |
?Python量化交易实战? | 入门级 | 手把手带你打造一个易扩展、更安全、效率更高的量化交易系统 |
数据库表设计
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 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 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 </spanxml 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 }
测试
转载请注明:xuhss » 接口开发-restful