一、概述
Http组作是SMB中的核心组件之一,在第七节中讲解了如何简洁的进行web程序部署和运行,这只是它的功能之一。在本节中,我们将介绍Http组件的重要属性。
二、请求头Request
1、支持方法
支持POST、GET、PUT、DELETE、OPTIONS等方法,支持较广的应用场景。
2、请求头
在实际开发中,需要通过请求header来提交一些重要的参数,接下来将展示在Http组件中如何获取。我们建立了一个简单的消息流:
在java计算组件中进行header获取:
package sashulin.apps;
import sashulin.Models.MessageModel;
import sashulin.applications.FlowApi;
import org.json.JSONArray;
import org.json.JSONObject;
import java.sql.*;
import java.util.Map;
public class httpFlow_JavaCompute1 {private String routeLabels = "";public String execute(MessageModel messageModel,String message) throws Exception{//获取请求头信息for(Object key : messageModel.flowRequestHeader.keySet()) {System.out.println("key:" + key.toString() + ",vaule:" + messageModel.flowRequestHeader.get(key));}System.out.println("参数:");//获取提交的url中的参数for(Object key : messageModel.params.keySet()) {System.out.println("key:" + key.toString() + ",vaule:" + messageModel.params.get(key));}System.out.println(message);JSONObject n = null;JSONObject input = new JSONObject();input.put("arg0","11122");String res = FlowApi.execute(this,"WebServiceOut1",n,input.toString());res = res.trim();return res;}public String getRouteLabels(){return routeLabels;}}
在代码中我们可以看到:
messageModel.flowRequestHeader中包含了全部的请求头,通过遍历,将所有kv打印出来。
结果:
key:Sec-ch-ua-mobile,vaule:?0
key:Sec-fetch-site,vaule:none
key:Sec-fetch-dest,vaule:document
key:Sec-ch-ua,vaule:"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"
key:User-agent,vaule:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
key:Accept,vaule:text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
key:Host,vaule:127.0.0.1:9003
key:Upgrade-insecure-requests,vaule:1
key:Sec-fetch-mode,vaule:navigate
key:Accept-language,vaule:zh-CN,zh;q=0.9
key:Connection,vaule:keep-alive
key:remoteAddress,vaule:localhost
key:Accept-encoding,vaule:gzip, deflate, br
key:method,vaule:GET
key:Sec-fetch-user,vaule:?1
key:Sec-ch-ua-platform,vaule:"macOS"
三、请求参数
不管是GET还是POST,在URL中带有参数都会在messageModel.params中获取到。
//获取提交的url中的参数
for(Object key : messageModel.params.keySet()) {System.out.println("key:" + key.toString() + ",vaule:" +
messageModel.params.get(key));
}
打印结果:
参数:
key:name,vaule:roy
key:company,vaule:yiji
四、返回Response
1、场景设计
1)、在消息流中调用另一个api,这个Api叫test,在提交给这个Api的时候要增加一个header,这个header名叫myHeaderName,值为hello Http
2)、调用完test这个Api后,要获取这个Api返回header中的某个数据。并打印这个api所有的header
3)、在当前消息流中增加Response Header,键值为:aaa:123333
流程设计如图:
httpOut1是调用test api的组件。
调用test api前的代码是:
package sashulin.apps;
import sashulin.Models.MessageModel;
import sashulin.applications.FlowApi;
import org.json.JSONArray;
import org.json.JSONObject;
import java.sql.*;
public class httpFlow_JavaCompute3 {private String routeLabels = "";public String execute(MessageModel messageModel,String message){messageModel.flowResponseHeader.clear();messageModel.setHttpOutRequestHeader("HttpOut1","myHeaderName","hello Http.");return message;}public String getRouteLabels(){return routeLabels;}}
最终返回的response的代码是:
package sashulin.apps;
import sashulin.Models.MessageModel;
import sashulin.applications.FlowApi;
import org.json.JSONArray;
import org.json.JSONObject;
import java.sql.*;
import java.util.HashMap;
public class httpFlow_JavaCompute4 {private String routeLabels = "";public String execute(MessageModel messageModel,String message){String headerValue = messageModel.getHttpOutResponseHeader("HttpOut1","Access-control-allow-headers");System.out.println("获取某个头Access-control-allow-headers:"+headerValue);System.out.println("获取返回头:");HashMap<String,Object> myHeaders = messageModel.getHttpOutResponseHeaders("HttpOut1");for(Object key : myHeaders.keySet()) {System.out.println("key:" + key.toString() + ",vaule:" + myHeaders.get(key));}//增加返回参数messageModel.flowResponseHeader.put("aaa","123333");return "Compute1:"+message;}public String getRouteLabels(){return routeLabels;}}
2、测试结果
总结:这种场景一般用于ESB平台,ESB中代理其他系统的接口,进行接口转调,然后再返回给请求方。
五、url中使用变量
将HttpUrlPattern组件的属性设置为带变量的url,如图:
编译、运行结果:
六、url中使用 *
运行结果: