cxf发布webservice与java客户端解析webservice

一.服务端发布webservice(CXF,添加cxf所需jar):
1.接口类


[java] view plaincopyprint?
package com.god.ws 
@WebService(targetNamespace="http://webservice.myservice.god.com") 
public interface IWsPersonInfo { 
    //根据姓名和年龄查询人员信息  
    public String getPersonInfo(@WebParam(name = "name")String name,@WebParam(name="age")int age); 


package com.god.ws
@WebService(targetNamespace="http://webservice.myservice.god.com")
public interface IWsPersonInfo {
    //根据姓名和年龄查询人员信息
    public String getPersonInfo(@WebParam(name = "name")String name,@WebParam(name="age")int age);
}
2.实现类


[java] view plaincopyprint?
package com.god.ws.impl 
@WebService(endpointInterface = "com.god.ws",targetNamespace="http://webservice.myservice.god.com") 
public class WsPersonInfoImpl implements IWsPersonInfo { 
     
    public String getPersonInfo(String NAME,int age){ 
        StringBuilder strXml = new StringBuilder(); 
        strXml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 
            strXml.append(".......添加人员信息..........."); 
        return strXml.toString(); 
    } 


package com.god.ws.impl
@WebService(endpointInterface = "com.god.ws",targetNamespace="http://webservice.myservice.god.com")
public class WsPersonInfoImpl implements IWsPersonInfo {

public String getPersonInfo(String NAME,int age){
StringBuilder strXml = new StringBuilder();
strXml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        strXml.append(".......添加人员信息...........");
return strXml.toString();
}
}
3.spring配置文件


[html] view plaincopyprint?
<?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-extension-soap.xml"/> 
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> 
 
    <jaxws:endpoint id="infoEndpoint" implementor="com.god.ws.impl.WsPersonInfoImpl"  address="/PersonInfo"></jaxws:endpoint> 
</beans> 

<?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-extension-soap.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

    <jaxws:endpoint id="infoEndpoint" implementor="com.god.ws.impl.WsPersonInfoImpl"  address="/PersonInfo"></jaxws:endpoint>
</beans>
4.web.xml配置CXF


[html] view plaincopyprint?
<servlet> 
<servlet-name>CXFServlet</servlet-name> 
<servlet-class> 
    org.apache.cxf.transport.servlet.CXFServlet 
</servlet-class> 
<load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
<servlet-name>CXFServlet</servlet-name> 
<url-pattern>/webservices/*</url-pattern> 
</servlet-mapping> 

<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/webservices/*</url-pattern>
</servlet-mapping>
二.客户端解析webservice


[java] view plaincopyprint?
// 获取SOAP连接工厂  
SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance(); 
// 从SOAP连接工厂创建SOAP连接对象  
SOAPConnection connection = factory.createConnection(); 
// 获取消息工厂  
MessageFactory mFactory = MessageFactory.newInstance(); 
// 从消息工厂创建SOAP消息对象  
SOAPMessage message = mFactory.createMessage(); 
 
// 创建SOAPPart对象  
SOAPPart part = message.getSOAPPart(); 
// 创建SOAP信封对象  
SOAPEnvelope envelope = part.getEnvelope(); 
// 创建SOAPHeader对象  
SOAPHeader header = message.getSOAPHeader(); 
// 创建SOAPBody对象  
SOAPBody body = envelope.getBody(); 
 
 
// 创建XML的根元素 ,设置查询参数     
SOAPHeaderElement headerElementRoot1 = header.addHeaderElement(new QName("http://webservice.myservice.god.com", "name", "tns")); 
SOAPHeaderElement headerElementRoot2 = header.addHeaderElement(new QName("http://webservice.myservice.god.com", "age", "tns")); 
body.addBodyElement(new QName("http://webservice.myservice.god.com", "getPersonInfo", "tns")); 
headerElementRoot1.addTextNode("神仙"); 
headerElementRoot2.addTextNode("28"); 
 
// 访问Web服务地址  
SOAPMessage reMessage = connection.call(message, new URL("http://localhost:8080/项目名/webservices/PersonInfo?wsdl")); 
 
//strRootXml即为服务端传过来的xml数据  
String strRootXml = reMessage.getSOAPPart().getEnvelope().getBody().getTextContent(); 
connection.close(); 

你可能感兴趣的:(webservice)