1、需在web.xml中配置CXFServlet

 <servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/service/*</url-pattern>
  </servlet-mapping>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
            classpath:spring/*.xml
        </param-value>
  </context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

2、在src/main/resources/spring 路径下添加 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:p="http://www.springframework.org/schema/p"  
    xmlns:jaxws="http://cxf.apache.org/jaxws"  
    xmlns:cxf="http://cxf.apache.org/core"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-2.5.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" />  
 
    <bean id="sendMail" class="cn.sccl.webservice.impl.SendMailRs"/>  
    <jaxws:endpoint id="helloWorld" implementor="#sendMail" address="/sendMail" />  
</beans>

其中 classpath*表示不光找java对应的classes中的文件 还可以找jar包中具有的该文件

3java类编写webService接口

package cn.sccl.webservice;

import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;


import net.sf.json.JSONObject;

import javax.jws.soap.SOAPBinding.Style;
@WebService(targetNamespace="http://impl.webservice.sccl.cn/",name="SendMailRsService")
@SOAPBinding(style = Style.DOCUMENT)
public interface SendMailRsService {


	JSONObject sendMail(@WebParam(name="tos")String tos,@WebParam(name="subject")String subject,@WebParam(name="content")String content);
	
	String sayHello(@WebParam(name="text")String text);
}

此处xml的namespaceURi命名为"http://impl.webservice.sccl.cn/" name设置为"SendMailRsService"

其实现类

package cn.sccl.webservice.impl;


import javax.jws.WebParam;
import javax.jws.WebService;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import cn.sccl.utils.sendMailByQQ.SendMail;
import cn.sccl.webservice.SendMailRsService;
import net.sf.json.JSONObject;
@WebService(endpointInterface="cn.sccl.webservice.SendMailRsService")
public class SendMailRs implements SendMailRsService{
	
	
	public JSONObject sendMail(String tos,String subject,String content){
		System.out.println(tos);
		System.out.println(subject);
		System.out.println(content);
		System.out.println("进来了");
		boolean f = SendMail.sendMessage(
				SendMail.SMTP_HOST,
				SendMail.SMTP_USER, 
				SendMail.SMTP_PWD,
				tos.split(";"), 
				subject, content, "text/html;charset=UTF-8");
		JSONObject json=new JSONObject();
		json.put("code", f?1:0);
		return json;
	}

	@Override
	public String sayHello(@WebParam(name="text")String text) {
		JSONObject json=new JSONObject();
		json.put("success", 1);
		System.out.println(text);
		return text;
	}


	
}

编写完成后发布webService

访问 http://localhost:8090/xx/service/sendMail?wsdl

<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.webservice.sccl.cn/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="SendMailRsService"targetNamespace="http://impl.webservice.sccl.cn/">

<wsdl:types>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://impl.webservice.sccl.cn/" elementFormDefault="unqualified" targetNamespace="http://impl.webservice.sccl.cn/" version="1.0">

<xs:element name="sayHello" type="tns:sayHello"/>

<xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>

<xs:element name="sendMail" type="tns:sendMail"/>

<xs:element name="sendMailResponse" type="tns:sendMailResponse"/>

<xs:complexType name="sendMail">

<xs:sequence>

<xs:element minOccurs="0" name="tos" type="xs:string"/>

<xs:element minOccurs="0" name="subject" type="xs:string"/>

<xs:element minOccurs="0" name="content" type="xs:string"/>

</xs:sequence>

</xs:complexType>

<xs:complexType name="sendMailResponse">

<xs:sequence>

<xs:element name="_return">

<xs:complexType>

<xs:sequence>

<xs:element maxOccurs="unbounded" minOccurs="0" name="entry">

<xs:complexType>

<xs:sequence>

<xs:element minOccurs="0" name="key" type="xs:anyType"/>

<xs:element minOccurs="0" name="value" type="xs:anyType"/>

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:sequence>

</xs:complexType>

<xs:complexType final="extension restriction" name="jsonObject">

<xs:complexContent>

<xs:extension base="tns:abstractJSON">

<xs:sequence/>

</xs:extension>

</xs:complexContent>

</xs:complexType>

<xs:complexType abstract="true" name="abstractJSON">

<xs:sequence/>

</xs:complexType>

<xs:complexType name="sayHello">

<xs:sequence>

<xs:element minOccurs="0" name="text" type="xs:string"/>

</xs:sequence>

</xs:complexType>

<xs:complexType name="sayHelloResponse">

<xs:sequence>

<xs:element minOccurs="0" name="return" type="xs:string"/>

</xs:sequence>

</xs:complexType>

</xs:schema>

</wsdl:types>

<wsdl:message name="sendMailResponse">

<wsdl:part element="tns:sendMailResponse" name="parameters"/>

</wsdl:message>

<wsdl:message name="sendMail">

<wsdl:part element="tns:sendMail" name="parameters"/>

</wsdl:message>

<wsdl:message name="sayHelloResponse">

<wsdl:part element="tns:sayHelloResponse" name="parameters"/>

</wsdl:message>

<wsdl:message name="sayHello">

<wsdl:part element="tns:sayHello" name="parameters"/>

</wsdl:message>

<wsdl:portType name="SendMailRsService">

<wsdl:operation name="sendMail">

<wsdl:input message="tns:sendMail" name="sendMail"/>

<wsdl:output message="tns:sendMailResponse" name="sendMailResponse"/>

</wsdl:operation>

<wsdl:operation name="sayHello">

<wsdl:input message="tns:sayHello" name="sayHello"/>

<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse"/>

</wsdl:operation>

</wsdl:portType>

<wsdl:binding name="SendMailRsServiceSoapBinding" type="tns:SendMailRsService">

<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>

<wsdl:operation name="sendMail">

<soap:operation soapAction="" style="document"/>

<wsdl:input name="sendMail">

<soap:body use="literal"/>

</wsdl:input>

<wsdl:output name="sendMailResponse">

<soap:body use="literal"/>

</wsdl:output>

</wsdl:operation>

<wsdl:operation name="sayHello">

<soap:operation soapAction="" style="document"/>

<wsdl:input name="sayHello">

<soap:body use="literal"/>

</wsdl:input>

<wsdl:output name="sayHelloResponse">

<soap:body use="literal"/>

</wsdl:output>

</wsdl:operation>

</wsdl:binding>

<wsdl:service name="SendMailRsService">

<wsdl:port binding="tns:SendMailRsServiceSoapBinding" name="SendMailRsPort">

<soap:address location="http://localhost:8090/iotportal/service/sendMail"/>

</wsdl:port>

</wsdl:service>

</wsdl:definitions>

4访问WebService的方法

1:axis call方法

package iotportal;


import java.rmi.RemoteException;

import javax.ws.rs.core.Response;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.encoding.XMLType;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
import org.w3c.dom.Element;

import net.sf.json.JSONObject;





public class TestWebService {
	public static void main(String[] args) throws ServiceException, RemoteException  {
		 
		//调用WebService的方法 1
		String endpoint = "http://localhost:8090/xx/service/sendMail?wsdl";   
         //直接引用远程的wsdl文件   
        //以下都是套路    
         Service service = new Service();   
         Call call = (Call) service.createCall();   
         call.setTargetEndpointAddress(endpoint);   
         QName qn=new QName("http://impl.webservice.sccl.cn/", "sayHello");
         call.setOperationName(qn);
         call.addParameter("text", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
         call.setReturnType(XMLType.SOAP_STRING);
         String response=(String) call.invoke(new Object[]{"18112998241@189.cn"});
         System.out.println(response);
         //调用WebService的方法2
	}
}

2:javax.xml.ws.Service

package iotportal;

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import cn.sccl.webservice.SendMailRsService;



public class TestWebService1 {
	public static void main(String[] args) throws MalformedURLException {
		URL url=new URL("http://localhost:8090/iotportal/service/sendMail?wsdl");
		Service service=Service.create(url,new QName("http://impl.webservice.sccl.cn/","SendMailRsService"));
		SendMailRsService sendMailService=service.getPort(new QName("http://impl.webservice.sccl.cn/","SendMailRsPort"),SendMailRsService.class);
		System.out.println(sendMailService.sendMail("18112998241@189.cn", "123", "123"));	
	}
}

 

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐