MuleSoft知识总结-10.构建后端服务,使用Request组件(Request)

23 篇文章 6 订阅
16 篇文章 4 订阅

构建后端服务

上次使用的数据是用Set Payload写入的,而在项目中我们往往会向后端发送请求来获取数据,把从后端获取的数据用Mule处理后再由Mule发送给前端。现在我们使用SpringBoot构建一个简单的后端。

  1. 使用Eclipse构建Spring Boot,创建两个文件EmployeeController.java和EmployeeEntity.java
    在这里插入图片描述
  2. 在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;
	}
}
  1. 在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();
		// TODO Auto-generated constructor stub
	}

	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;
	}
}
  1. 启动后端服务,使用PostMan测试
    在这里插入图片描述

使用Mule组件Request

  1. 回到之前的Mule APP(employee-api),删除employee-sub文件中的Set Payload组件,在Mule Palette中找到HTTP,将HTTP中的Request组件添加到Sub Flow中。
    在这里插入图片描述
  2. 配置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 ParametersQuery参数
URI ParametersURI参数

在这里插入图片描述

  1. 启动Mule APP使用PostMan进行访问,取回数据。
    在这里插入图片描述
  2. 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”}]
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值