catalogue
pom.xml introduces jar package dependency
Add the bean file ApplicationContext CXF. For the webService service interface xml
Write a java class for the webService service
The client invokes the webService service
JaxWsDynamicClientFactory call based on dynamic proxy factory class
Calling web service service based on HTTP client
Basic concepts of Web Service
Web Service is also called XML Web Service. Web Service is a lightweight and independent communication technology that can receive requests from other systems on the Internet or Intranet. Yes: software services provided on the web through SOAP are described using WSDL files and registered through UDDI.
XML: (Extensible Markup Language) extensible markup language. Short term oriented temporary data processing and World Wide Web oriented are the basis of Soap.
Soap: (Simple Object Access Protocol) simple object access protocol. Is the communication protocol of XML Web Service. When the user finds your WSDL description document through UDDI, he can call one or more operations in the web service you established through soap. Soap is a specification for calling methods in the form of XML documents. It can support different underlying interfaces, such as HTTP(S) or SMTP.
WSDL: (Web service description language) a WSDL file is an XML document that describes a set of SOAP messages and how they are exchanged. In most cases, it is automatically generated and used by software.
UDDI (Universal Description, Discovery, and Integration) is a new project mainly aimed at Web service providers and consumers. Before users can call Web services, they must determine which methods are included in the service, find the interface definition to be called, and prepare software on the server. UDDI is a mechanism to guide the system to find corresponding services according to the description documents. UDDI uses SOAP message mechanism (standard XML/HTTP) to publish, edit, browse and find registration information. It uses XML format to encapsulate various types of data, and sends it to the registry or the registry returns the required data.
Call principle
Environment configuration
pom.xml introduces jar package dependency
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-core</artifactId> <version>3.1.11</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.11</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.11</version> </dependency> <!-- join cxf-restful Dependent package --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxrs</artifactId> <version>3.1.11</version> </dependency>
web.xml settings Servlet
<servlet> <servlet-name>CXFService</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFService</servlet-name> <url-pattern>/webService/*</url-pattern> </servlet-mapping>
Note: don't make a mistake in adding the location, as shown in the figure below:
Add the bean file ApplicationContext CXF. For the webService service interface xml
Note the file location: Here I am web The xml scanning path for xml configuration is
So my file is in Src / main / resources / spring / ApplicationContext CXF XML, the file contents are as follows
<?xml version="1.1" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:soap="http://cxf.apache.org/bindings/soap" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd "> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <bean id="CommonWebServiceServerService" class="com.xxx.common.webService.impl.CommonWebServiceServerServiceImpl" /> <jaxws:endpoint implementor = "#CommonWebServiceServerService" address="/webServiceServer" publish="true" /> </beans>
Provide a web service server interface (you can skip it here if the project does not need to provide external services)
Write a java class for the webService service
Write an interface first
package com.xxx.common.webService; public interface CommonWebServiceServerService { String commonMethod( String xmlData); }
Then write its implementation class:
package com.xxx.common.webService.impl; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @WebService(targetNamespace = "http://webService.common.xxx.com") public class CommonWebServiceServerServiceImpl implements CommonWebServiceServerService{ private static final Logger logger = LoggerFactory.getLogger(CommonWebServiceServerServiceImpl.class); @WebMethod @WebResult(targetNamespace = "http://webService.common.xxx.com") public String commonMethod(@WebParam(targetNamespace = "http://webService.common.xxx.com") String xmlData){ long start = System.currentTimeMillis(); logger.info("Receiving third parties( webservice)message:{}", xmlData); String retXml = ""; try { //Write your specific business logic here to process and return the client response } catch (Exception e) { logger.error("Receiving third parties( webservice)Message processing exception:{}", e); } finally { logger.info("Dealing with third parties( webservice)The time taken for the request is:{}millisecond", System.currentTimeMillis()-start); } return retXml; } }
Note: the namespace targetNamespace is the full path of the package name inversion where your interface is located
The client invokes the webService service
There are many ways to call the client. I think it should be used according to my actual situation
JaxWsDynamicClientFactory call based on dynamic proxy factory class
catalogue
pom.xml introduces jar package dependency
Add the bean file ApplicationContext CXF. For the webService service interface xml
Write a java class for the webService service
The client invokes the webService service
JaxWsDynamicClientFactory call based on dynamic proxy factory class
Calling web service service based on HTTP client
Calling web service service based on HTTP client
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.InputStreamRequestEntity; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.RequestEntity; public class HttpClientTest { static String wsdl = "http://ip:port/webService/webServiceServer?wsdl"; static String ns = "http://webService.common.xxx.com "; / / namespace static String method = "commonMethod";//Method name /** * Access services * * @param wsdl wsdl address * @param ns Namespace * @param method Method name * @param req_xml Request parameters * @return * @throws Exception */ public synchronized static String accessService(String wsdl, String ns, String method,String reqXml) throws Exception { String soapResponseData = ""; StringBuffer soapRequestData = new StringBuffer(""); soapRequestData.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\""+ns+"\">"); soapRequestData.append("<soapenv:Header/>"); soapRequestData.append("<soapenv:Body>"); soapRequestData.append("<ser:" + method + ">"); soapRequestData.append("<arg0>"+reqXml+"</arg0>"); soapRequestData.append("</ser:" + method + ">"); soapRequestData.append("</soapenv:Body>" + "</soapenv:Envelope>"); PostMethod postMethod = new PostMethod(wsdl); // Then add the Soap request data to the PostMethod byte[] b = null; InputStream is = null; try { b = soapRequestData.toString().getBytes("utf-8"); is = new ByteArrayInputStream(b, 0, b.length); RequestEntity re = new InputStreamRequestEntity(is, b.length, "application/soap+xml; charset=UTF-8"); postMethod.setRequestEntity(re); HttpClient httpClient = new HttpClient(); int status = httpClient.executeMethod(postMethod); if (status == 200) { System.out.println("Successfully called webService Interface return content:"+postMethod.getResponseBodyAsString()); soapResponseData = getMesage(postMethod.getResponseBodyAsString()); }else { System.out.println("call webService Interface failure:STATUS:"+status); System.out.println("httpPost Mode call webservice Service exception:"+postMethod.getResponseBodyAsString()); } } catch (Exception e) { e.printStackTrace(); } finally { if (is != null) { is.close(); } } return soapResponseData; } public static String getMesage(String soapAttachment) { if (soapAttachment != null && soapAttachment.length() > 0) { int begin = soapAttachment.indexOf("<return>"); begin = soapAttachment.indexOf(">", begin); int end = soapAttachment.indexOf("</return>"); String str = soapAttachment.substring(begin + 1, end); str=str.replace("<", "<"); str=str.replace(""", "\""); str=str.replace(">", ">"); return str; } else { return ""; } } }
The above is a tool class. If it is inappropriate, you can modify it according to your own request and the calling method
String response = accessService(wsdl, ns, method,reqXml);
There is also a way for axis2 to call webService
Interested can study on their own, Xiaobian is once used in the project once invoked the interface of the bank, is a variety of ways, if the above two ways have problems, then consider third kinds. Because cxf needs the same version of the server and client. Therefore, sometimes there will be problems when the package versions are inconsistent.