接口发布
依赖包
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.3.1</version>
</dependency>
接口层
@WebService(name = "IdtpWebservice",targetNamespace = "http://webservice.brt.com")
public interface IdtpWebservice {
@WebMethod
String noParams(@WebParam(name = "test",targetNamespace = "http://webservice.brt.com")String transId);
}
实现层
@WebService(endpointInterface = "com.brt.govInfoManager.infoEditing.core.service.idtp.IdtpWebservice",
targetNamespace = "http://webservice.brt.com",
serviceName = "IdtpWebservice")
@Component
public class IdtpWebserviceImpl implements IdtpWebservice{
@Override
public String noParams( String transId) {
return "调用成功"+transId;
}
}
配置
默认地址:http://ip:port/services/【服务名】?wsdl
@Configuration
public class idtpConfig {
@Bean
public ServletRegistrationBean CXFServlet() {
return new ServletRegistrationBean(new CXFServlet(),"/webservice/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public IdtpWebservice webService() {
return new IdtpWebserviceImpl();
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), webService());
endpoint.publish("/IdtpWebservice");
return endpoint;
}
}
使用postman调用 web:命名空间, noParams:方法的名称 test:参数的名称
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2003/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2003/XMLSchema/"
xmlns:web="http://webservice.brt.com">
<soap:Body>
<web:noParams>
<web:test>1111</web:test>
</web:noParams>
</soap:Body>
</soap:Envelope>
接口调用
cxf方式调用
依赖和上面一样
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("服务地址:http://ip:port/服务名?wsdl");
QName qName = new QName("命名空间", "方法名称");
Object[] objects = null;
try {
objects = client.invoke(qName,
viewldtpFileSend.getTransId(),
viewldtpFileSend.getFileType(),
viewldtpFileSend.getFileName(),
viewldtpFileSend.getFile(),
viewldtpFileSend.getFragId(),
viewldtpFileSend.getExt1());
} catch (Exception e) {
e.printStackTrace();
return null;
}
httpClient方式调用
URL url = new URL(wsdl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
connection.setDoInput(true);
connection.setDoOutput(true);
String soapXML = this.getXML(viewldtpFormSend);
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes());
int responseCode = connection.getResponseCode();
StringBuilder sb = new StringBuilder();
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String temp = null;
while(null != (temp = br.readLine())){
sb.append(temp);
}
is.close();
isr.close();
br.close();
os.close();
this.logger.info("单导发送至方法:{},结果:{}",method,sb.toString());
if(200 == responseCode){
return true;
}
拼装XML数据:
public String getXML(ViewldtpFormSend viewldtpFormSend) {
String soapXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2003/XMLSchema-instance\" "
+"xmlns:web=\"http://webservice.brt.com\" "
+"xmlns:xsd=\"http://www.w3.org/2003/XMLSchema\" "
+"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+"<soap:Body>"
+"<web:ldtpFormSend>"
+"<web:transId>"+viewldtpFormSend.getTransId()+"</web:transId>"
+"<web:appId>"+viewldtpFormSend.getAppId()+"</web:appId>"
+"<web:formType>"+viewldtpFormSend.getFormType()+"</web:formType>"
+"<web:form>"+viewldtpFormSend.getForm()+"</web:form>"
+"<web:ext1>"+viewldtpFormSend.getExt1()+"</web:ext1>"
+"</web:ldtpFormSend>"
+"</soap:Body>"
+"</soap:Envelope>";
this.logger.info("ldtpFileSendXML"+soapXML);
return soapXML;
}
|