java是一个循环往复螺旋式上升的学习过程,因此,这对我们的耐力和动力要求很高。今天就主要来为大家介绍一下在java中如何用SOAP调用webservice,以及怎么编写它的接口。
想用soap调用webservice之前,我们需要下载jar,SOAP使用HTTP传送 XML。虽然HTTP 不是有效率的通讯协议,而且XML还需要额外的文件解析(parse),两者使得交易的速度大大低于其它方案。但是XML是一个开放、健全、有语义的讯息机制,而HTTP是一个广泛又能避免许多关于防火墙的问题,从而使SOAP得到了广泛的应用。但是考虑到效率的话,最好还是使用其他的方式更加好。
接下来为大家展示一个实际的代码:
import org.apache.soap.util.xml.*; import org.apache.soap.*; import org.apache.soap.rpc.*; import java.io.*; import java.net.*; import java.util.Vector; public class caService { public static String getService(String user) { URL url = null; try { url = new URL( "http://192.168.0.100:8080/ca3/services/caSynrochnized"); } catch (MalformedURLException mue) { return mue.getMessage(); } // This is the main SOAP object Call soapCall = new Call(); // Use SOAP encoding soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC); // This is the remote object we're asking for the price soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized"); // This is the name of the method on the above object soapCall.setMethodName("getUser"); // We need to send the ISBN number as an input parameter to the method Vector soapParams = new Vector(); // name, type, value, encoding style Parameter isbnParam = new Parameter("userName", String.class, user , null); soapParams.addElement(isbnParam); soapCall.setParams(soapParams); try { // Invoke the remote method on the object Response soapResponse = soapCall.invoke(url, ""); // Check to see if there is an error, return "N/A" if (soapResponse.generatedFault()) { Fault fault = soapResponse.getFault(); String f = fault.getFaultString(); return f; } else { // read result Parameter soapResult = soapResponse.getReturnValue(); // get a string from the result return soapResult.getValue() .toString(); } } catch (SOAPException se) { return se.getMessage(); } } }
接下来我们看一下如何在java中写webservice的接口,主要是为了部署到Tomcat下面的。首先⑴、创建一个web项目,然后添加jar包,代码如下:
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- 这里直接导入整个项目下的所有jar包,其中去除几个 --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>apache-cxf</artifactId> <version>3.2.6</version> <type>pom</type> <exclusions> <exclusion> <artifactId>cxf-services-wsn-api</artifactId> <groupId>org.apache.cxf.services.wsn</groupId> </exclusion> <exclusion> <artifactId>cxf-services-wsn-core</artifactId> <groupId>org.apache.cxf.services.wsn</groupId> </exclusion> <exclusion> <artifactId>cxf-services-ws-discovery-api</artifactId> <groupId>org.apache.cxf.services.ws-discovery</groupId> </exclusion> <exclusion> <artifactId>cxf-services-ws-discovery-service</artifactId> <groupId>org.apache.cxf.services.ws-discovery</groupId> </exclusion> </exclusions> </dependency> </dependencies>
⑵、然后再添加web.xml,代码如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <servlet> <description>Apache CXF Endpoint</description> <display-name>cxf</display-name> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> </web-app>
⑶、之后写一个接口,代码如下:
import javax.jws.WebService; @WebService public interface MsgService { public String getValue(String name); }
⑷、接着写接口的实现类,代码如下:
package com.msg.webservice.impl; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.jws.WebService; import com.msg.util.Log_Exception; import com.msg.util.UrlUtil; import com.msg.util.Util; import com.msg.webservice.MsgService; @WebService(endpointInterface = "com.msg.webservice.MsgService") public class MsgServiceImpl implements MsgService { public String getValue(String name) { return "我叫" + name; } }
⑸、再之后是cxf-servlet.xml,代码如下:
<?xml version="1.0" 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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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" /> //implementor:实现类的地址 address:调用时的地址 <jaxws:endpoint id="personQueryService" implementor="com.msg.webservice.impl.MsgServiceImpl" address="/msgservice" /> </beans>
⑹、client-beans.xml,代码展示:
<?xml version="1.0" 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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd"> //class:接口地址 <bean id="client" class="com.msg.webservice.MsgService" factory-bean="clientFactory" factory-method="create" /> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> //value:接口地址 <property name="serviceClass" value="com.msg.webservice.MsgService" /> //value:调用时的地址 <property name="address" value="http://192.168.1.10:9090/MSService/services/msgservice" /> </bean> </beans>
⑺、启动Tomcat;
⑻、图片展示:
⑼、调用接口。
以上就是有关在java中怎么用SOAP调用webservice以及写它的接口的具体操作方法。想要了解更多java经典例子,敬请关注奇Q工具网。
推荐阅读:
在java中如何用Apche CXF开发webservice,实际操作展示