构建后端服务
上次使用的数据是用Set Payload写入的,而在项目中我们往往会向后端发送请求来获取数据,把从后端获取的数据用Mule处理后再由Mule发送给前端。现在我们使用SpringBoot构建一个简单的后端。
- 使用Eclipse构建Spring Boot,创建两个文件EmployeeController.java和EmployeeEntity.java
- 在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;
@RestController
public class EmployeeController {
@RequestMapping(value = "/getEmployee", method = {RequestMethod.GET})
public ArrayList<EmployeeEntity> getEmployee() {
ArrayList<EmployeeEntity> employeeEntities = new ArrayList<EmployeeEntity>();
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");
employeeEntities.add(employeeEntity1);
employeeEntities.add(employeeEntity2);
employeeEntities.add(employeeEntity3);
return employeeEntities;
}
}
- 在EmployeeEntity.java编写以下代码
package com.example.demo.entity;
public class EmployeeEntity {
public EmployeeEntity(String id, String eName, String eAge, String eSex, String cGroup, String cTeam, String email,
String phone, String state, String city) {
super();
Id = id;
this.eName = eName;
this.eAge = eAge;
this.eSex = eSex;
this.cGroup = cGroup;
this.cTeam = cTeam;
Email = email;
Phone = phone;
State = state;
City = city;
}
public EmployeeEntity() {
super();
}
String Id;
String eName;
String eAge;
String eSex;
String cGroup;
String cTeam;
String Email;
String Phone;
String State;
String City;
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
public String geteName() {
return eName;
}
public void seteName(String eName) {
this.eName = eName;
}
public String geteAge() {
return eAge;
}
public void seteAge(String eAge) {
this.eAge = eAge;
}
public String geteSex() {
return eSex;
}
public void seteSex(String eSex) {
this.eSex = eSex;
}
public String getcGroup() {
return cGroup;
}
public void setcGroup(String cGroup) {
this.cGroup = cGroup;
}
public String getcTeam() {
return cTeam;
}
public void setcTeam(String cTeam) {
this.cTeam = cTeam;
}
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
public String getPhone() {
return Phone;
}
public void setPhone(String phone) {
Phone = phone;
}
public String getState() {
return State;
}
public void setState(String state) {
State = state;
}
public String getCity() {
return City;
}
public void setCity(String city) {
City = city;
}
}
- 启动后端服务,使用PostMan测试
使用Mule组件Request
- 回到之前的Mule APP(employee-api),删除employee-sub文件中的Set Payload组件,在Mule Palette中找到HTTP,将HTTP中的Request组件添加到Sub Flow中。
- 配置Request组件,点击Configuration后的小加号,弹出HTTP配置窗口在Host中填入IP地址(域名),在Port中填入端口号,在Path中填入请求路径。其它的配置:
名称 | 作用 |
---|
Name | 配置元素名 |
Base path | 基本路径 |
Protocol | 使用协议 |
Host | 域名 |
Port | 端口号 |
Max connections | 最大连接数 |
Connection idle timeout | 连接超时时间 |
名称 | 作用 |
---|
Method | 请求方式 |
Path | 请求路径 |
URL | 请求URL |
Body | 请求体 |
Headers | 请求头 |
Query Parameters | Query参数 |
URI Parameters | URI参数 |
- 启动Mule APP使用PostMan进行访问,取回数据。
- Mule APP后台Logger值:
INFO 2020-12-18 15:32:48,739 [[MuleRuntime].uber.11: [employee-api].get:\getEmpInf:employeeapi-config.CPU_LITE @5618bd3a] [processor: employeeapi-Sub_Flow/processors/1; event: 3a7add50-4103-11eb-abf8-ac675d3065a1] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: [{“eName”:“zhangsan”,“eAge”:“20”,“eSex”:“man”,“cGroup”:“afl”,“cTeam”:“mulesoft”,“id”:“12345678”,“state”:“liaoning”,“email”:“san.b.zhang@acc.com”,“city”:“dalian”,“phone”:“12345678901”},{“eName”:“lisan”,“eAge”:“20”,“eSex”:“man”,“cGroup”:“afl”,“cTeam”:“se”,“id”:“12345679”,“state”:“liaoning”,“email”:“san.a.li@acc.com”,“city”:“shenyang”,“phone”:“12345678902”},{“eName”:“wangwu”,“eAge”:“20”,“eSex”:“woman”,“cGroup”:“sap”,“cTeam”:“sa”,“id”:“12345680”,“state”:“sichuan”,“email”:“wu.a.wang@acc.com”,“city”:“chengdu”,“phone”:“12345678903”}]