【新建规则项目HelloWorldRule】——>点击【下一步】
【规则项目引用】——>点击【完成】
【新建主流、hello规则】
【请求客户端结构】
ClientMultiThreadedExecution.java:
package httpclient;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import org.apache.http4.HttpEntity;
import org.apache.http4.HttpResponse;
import org.apache.http4.client.methods.HttpPost;
import org.apache.http4.client.utils.URIBuilder;
import org.apache.http4.entity.StringEntity;
import org.apache.http4.util.EntityUtils;
public class ClientMultiThreadedExecution {
/**
* send http post request
*
* @param url:http url
* @param params:String数组 格式为{"param1=value1","param2=value2"}
* @param content:发送内容为String不限制长度
* @return com.firsttech.underwriting.dto.UnderwriteResultDTO序列化后的XML
* @exception
*/
public static String sendHttpPost(String url,String[] params,String content) throws Exception{
URIBuilder builder = new URIBuilder();
try {
URL u = new URL(url);
builder.setScheme(u.getProtocol()).setHost(u.getHost()).setPort(u.getPort()).setPath(u.getPath());
for(String param:params){
String[] p = param.split("=");
builder.setParameter(p[0],p[1]);
}
} catch (RuntimeException e) {
throw new IllegalArgumentException("URL格式错误",e);
}
return sendHttpPost(builder,content);
}
public static String sendHttpPost(URIBuilder builder,String content) throws URISyntaxException, IOException{
HttpPost httppost = new HttpPost(builder.build());
if(content!=null){
StringEntity entity = new StringEntity(content,"UTF-8");
httppost.setEntity(entity);
}
try {
HttpResponse response = HttpConnectionManager.getHttpClient().execute(httppost);
HttpEntity result = response.getEntity();
return EntityUtils.toString(result);
} catch (Exception ex) {
ex.printStackTrace();
httppost.abort();
return null;
}
}
}
ObjectXmlUtils.java:
package utils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.xml.sax.SAXException;
import com.wutka.jox.JOXBeanInputStream;
import com.wutka.jox.JOXBeanOutputStream;
public class ObjectXmlUtils {
public static String javaBean2Xml(Object o){
OutputStream xmlData = null;
xmlData = new ByteArrayOutputStream();
JOXBeanOutputStream joxOut = new JOXBeanOutputStream(xmlData,"GBK");
try {
joxOut.writeObject(o.getClass().getName(), o);
} catch (IOException e) {
System.out.println(e.getMessage());
System.out.println(e.getStackTrace());
e.printStackTrace();
}
return xmlData.toString();
}
@SuppressWarnings("rawtypes")
public static Object xml2javaBean(String xml,Class className){
try {
InputStream in = new ByteArrayInputStream(xml.getBytes());
JOXBeanInputStream joxIn = new JOXBeanInputStream(in);
Object o = (Object) joxIn.readObject(className);
return o;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
FileLoader.java:
package com.test.utils;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
public class FileLoader {
/**
* 读取文件到字符串
*
* @throws UnsupportedEncodingException
*/
public static String readFile(String fileName)
throws UnsupportedEncodingException {
String fileContent = "";
StringBuffer filebuff = new StringBuffer();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(
new FileInputStream(fileName), "GBK"));
String tempString = null;
while ((tempString = reader.readLine()) != null) {
filebuff.append(tempString + "\n");
}
fileContent = filebuff.toString();
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
}
return fileContent;
}
}
helloWorld.xml:
<?xml version="1.0" encoding="GBK"?>
<HelloWorldRequestBOM java-class="com.hello.bom.HelloWorldRequestBOM">
<age java-class="java.lang.Integer">17</age>
<name>小明</name>
<requestType>110</requestType>
<result>结果</result>
</HelloWorldRequestBOM>
TestHelloWorldService.java:
package com.test.client;
import httpclient.ClientMultiThreadedExecution;
import com.test.utils.FileLoader;
public class TestHelloWorldService {
private final static String url = "http://192.168.17.1:8080/HelloWorldWLS/helloWorldService.do";
public static void main(String[] args) throws Exception {
String[] params = { "method=helloWorldRule" };
String content = FileLoader.readFile("resources/helloWorld.xml");
String resultXML = ClientMultiThreadedExecution.sendHttpPost(url,params, content);
System.out.println("请求:");
System.out.println(content);
System.out.println();
System.out.println("响应:");
System.out.println(resultXML);
}
}