文章目录
前言
(MuleSoft自学分享-11.Mule基本组件)中使用了get电商商城定制开发请求通过后端接口/getEmployee获取到了全部的employee数据。这次我们要使用Post请求,请求体为员工编号,返回员工编号为"12345678"的员工数据。
{ "number": "12345678"}
- 1
- 2
- 3
Post请求
-
使用Sub ,Request,Transform Message和Logger四种组件进行配置,方式和(MuleSoft自学分享-11.Mule基本组件)中相同。最后将Request中Method设置为Post。
-
进入employeeapi,将post:\ …中的logger替换为Flow Reference,并将Flow Name设置为employeeapi-sub中的employeeapi-post_Flow
-
后端EmployeeController.java修改为
package com.example.demo.controller;import java.util.ArrayList;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import com.example.demo.entity.EmployeeEntity;import com.example.demo.entity.NumberEntity;@RestControllerpublic class EmployeeController { EmployeeEntity employeeEntity1 = new EmployeeEntity("12345678", "zhangsan", "20", "man", "afl", "mulesoft", "san.b.zhang@acc.com", "12345678901", "liaoning", "dalian"); EmployeeEntity employeeEntity2 = new EmployeeEntity("12345679", "lisan", "20", "man", "afl", "se", "san.a.li@acc.com", "12345678902", "liaoning", "shenyang"); EmployeeEntity employeeEntity3 = new EmployeeEntity("12345680", "wangwu", "20", "woman", "sap", "sa", "wu.a.wang@acc.com", "12345678903", "sichuan", "chengdu"); @RequestMapping(value = "/getEmployee", method = {RequestMethod.GET}) public ArrayList<EmployeeEntity> getEmployee() { ArrayList<EmployeeEntity> employeeEntities = new ArrayList<EmployeeEntity>(); employeeEntities.add(employeeEntity1); employeeEntities.add(employeeEntity2); employeeEntities.add(employeeEntity3); return employeeEntities; } @RequestMapping(value = "/getEmployee", method = {RequestMethod.POST}) public ArrayList<EmployeeEntity> postEmployee(@RequestBody NumberEntity Body) { ArrayList<EmployeeEntity> employeeEntities = new ArrayList<EmployeeEntity>(); ArrayList<EmployeeEntity> employeePostEntities = new ArrayList<EmployeeEntity>(); employeeEntities.add(employeeEntity1); employeeEntities.add(employeeEntity2); employeeEntities.add(employeeEntity3); for(int i = 0; i < employeeEntities.size();i++) { if(employeeEntities.get(i).getId().equals(Body.getNumber())) { employeePostEntities.add(employeeEntities.get(i)); } } return employeePostEntities; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 后端增加NumberEntity.java
package com.example.demo.entity;public class NumberEntity { String number; public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 启动前后端后用PostMan进行请求,在请求体中添加{“number”: “12345678”},将PostMan中请求方式设置为POST。
{ "number": "12345678"}
- 1
- 2
- 3
获得员工号为"12345678"的员工数据。