java调⽤webservice接⼝
1.直接AXIS调⽤远程的webservice
注意:不同版本的Axis相差很⼤,⼤家最好以apache⽹站上的例⼦为准,这⾥仅仅⽤于说明其基本⽤法。
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class WebServiceClient {
public static void main(String[] args) {
try {
String endpoint = "172.19.0.153:8080/scs-web/webservice/SignService";
// 直接引⽤远程的wsdl⽂件
// 以下都是套路
Service service = new Service();
call.setOperationName("signContract");// WSDL⾥⾯描述的接⼝名称
Call call = (Call) ateCall();
call.setTargetEndpointAddress(endpoint);
call.addParameter("channel",org.ding.XMLType.XSD_l.rpc.ParameterMode.IN);// 接⼝的参数
call.addParameter("templateId",org.ding.XMLType.XSD_l.rpc.ParameterMode.IN);// 接⼝的参数
call.addParameter("strJson",org.ding.XMLType.XSD_l.rpc.ParameterMode.IN);// 接⼝的参数
call.addParameter("isSeal",org.ding.XMLType.XSD_l.rpc.ParameterMode.IN);// 接⼝的参数
call.addParameter("callBackUrl",org.ding.XMLType.XSD_l.rpc.ParameterMode.IN);// 接⼝的参数
call.setReturnType(org.ding.XMLType.XSD_STRING);// 设置返回类型
String templateId = "3021";
String channel = "1007";
String strJson = "{\"certApplys\":[{\"useridno\":\"N256613448988875\",\"address\":\"⼭东省德州临⾢县恒源\",\"engName\":\"test\",\"telno\":\"158********\",\"email\":\"test@credithc\",\"username\":\"CHENG AI-HSIANG\",\"idTypeCode\":\"Z\" String isSeal = "1";
String callBackUrl = "?";
String result = (String) call.invoke(new Object[] {channel,templateId,isSeal,strJson,callBackUrl});
// 给⽅法传递参数,并且调⽤⽅法
System.out.println("result is :" + result);
} catch (Exception e) {
}
}
}
2.使⽤Http⽅式调⽤远程的webservice
package com.webservice;
import java.io.InputStream;
import java.io.OutputStream;
import java.HttpURLConnection;
import java.URL;
import java.URLConnection;
public class WebServiceClient2 {
public static void main(String[] args) {
try {
// 1 指定WebService服务的请求地址:
String wsUrl = "172.19.0.153:8080/scs-web/webservice/SignService";
// 2 创建URL:
URL url = new URL(wsUrl);
// 3 建⽴连接,并将连接强转为Http连接
URLConnection conn = url.openConnection();
HttpURLConnection con = (HttpURLConnection) conn;
/
/ 4,设置请求⽅式和请求头:
con.setDoInput(true); // 是否有⼊参
con.setDoOutput(true); // 是否有出参
con.setRequestMethod("POST"); // 设置请求⽅式
con.setRequestProperty("content-type", "text/xml;charset=UTF-8");
// 5,⼿动构造请求体
String channel = "1007";
String templateId = "3021";
String isSeal = "1";
String strJson = "{\"certApplys\":[{\"useridno\":\"N256613448988875\",\"address\":\"⼭东省德州临⾢县恒源\",\"engName\":\"test\",\"telno\":\"158********\",\"email\":\"test@credithc\",\"username\":\"CHENG
AI-HSIANG\",\"idTypeCode\":\"Z\" String callBackUrl = "?";
String  requestBody = "<soapenv:Envelope xmlns:soapenv=\"/soap/envelope/\"";
requestBody += " xmlns:xsd=\"/2001/XMLSchema\"";
requestBody += " xmlns:xsi=\"/2001/XMLSchema-instance\">";
requestBody += "<soapenv:Body>";
requestBody += "<q0:signContract xmlns:q0=\"impl.dithc/\">";
requestBody += "<channel>" + channel + "</channel>";
requestBody += "<templateId>" + templateId + "</templateId> ";
requestBody += "<isSeal>" + isSeal + "</isSeal> ";
requestBody += "<strJson>" + strJson + "</strJson> ";
requestBody += "<callBackUrl>" + callBackUrl + "</callBackUrl> ";
requestBody += "</q0:signContract>";
requestBody += "</soapenv:Body>";
调用webservice服务requestBody += "</soapenv:Envelope>";
// 6,通过流的⽅式将请求体发送出去:
OutputStream out = OutputStream();
out.Bytes());
out.close();
// 7,服务端返回正常:
int code = ResponseCode();
if (code == 200) {// 服务端返回正常
InputStream is = InputStream();
byte[] b = new byte[1024];
StringBuffer sb = new StringBuffer();
int len = 0;
while ((len = is.read(b)) != -1) {
String str = new String(b, 0, len, "UTF-8");
sb.append(str);
}
System.out.String());
is.close();
}
con.disconnect();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}blog.csdn/java_cainiao2016/article/details/80032377
3.springboot 动态调⽤
sign.webservice;
sign.service.RedisService;
import f.endpoint.Client;
import dpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
l.namespace.QName;
import java.util.UUID;
@WebService(name = "reSignService")
@Component
public class ReSignService {
@Autowired
private RedisService redisService;
@WebMethod
@WebResult
public String reSign(@WebParam(name = "channel") String channel,
@WebParam(name = "templateId") String templateId,
@WebParam(name = "isSeal") int isSeal,
@WebParam(name = "strJson") String strJson,
@WebParam(name = "callBackUrl") String callBackUrl) {
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
redisService.set(uuid, callBackUrl);
callBackUrl = "172.19.0.153:8081/re/sign/callback" + "?id=" + uuid;
// 创建动态客户端
JaxWsDynamicClientFactory dcf = wInstance();
Client client = ateClient("172.19.0.153:8080/scs-web/webservice/SignService?wsdl");        Object[] objects = new Object[0];
try {
QName opName = new QName("dithc/", "signContract");
objects = client.invoke(opName, channel, templateId, isSeal, strJson, callBackUrl);
System.out.println("返回数据:" + objects[0]);
} catch (java.lang.Exception e) {
e.printStackTrace();
}
return "请求成功";
}
}
fig;
sign.webservice.ReSignService;
import f.Bus;
import f.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import t.annotation.Bean;
import t.annotation.Configuration;
l.ws.Endpoint;
@Configuration
public class CxfConfig {
@Autowired
private Bus bus;
@Autowired
private ReSignService reSignService;
@Bean
public Endpoint endpoint(){
EndpointImpl endpoint = new EndpointImpl(bus,reSignService);
endpoint.publish("/reSignService");
return endpoint;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。