介面開發-restful

勤快的懶羊羊發表於2022-06-14

資料庫表設計

 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 }

測試

 

相關文章