wsdl文档结构图
JDK方式开发
server端代码
package server; import javax.jws.WebMethod; import javax.jws.WebService; /** * * * SEI:Service Endpoint Interface 发布的服务接口 * */ @WebService public interface HelloWS { @WebMethod public String sayHello(String name); }
package server; import javax.jws.WebService; /** * * * SEI的实现 * */ @WebService public class HelloWSImpl implements HelloWS{ @Override public String sayHello(String name) { System.out.println("server sayHello()"+name); return "Hello"+name; } }
package server; import javax.xml.ws.Endpoint; public class ServerPublish { public static void main(String[] args) { String address="http://localhost:8090/sayhello"; //对应的wsdl文档地址http://localhost:8090/sayhello?wsdl Endpoint.publish(address, new HelloWSImpl()); System.out.println("发布服务成功"); } }
根据wsdl文件生成客户端代码
远程 wsimport -keep .net代码写的需要修改里面的 <s:sequence> <s:element ref="s:schema"/> <s:any/> </s:sequence> 为 <s:sequence> <s:any minOccurs="2" maxOccurs="2" /> </s:sequence> 本地 wsimport -keep WeatherWS.wsdl
使用以下代码发送请求
package weather; import java.util.List; import cn.com.webxml.ArrayOfString; import cn.com.webxml.WeatherWS; import cn.com.webxml.WeatherWSSoap; public class ClientTest { public static void main(String[] args) { WeatherWS weatherWS =new WeatherWS(); WeatherWSSoap weatherWSSoap =weatherWS.getWeatherWSSoap(); ArrayOfString arrayOfString= weatherWSSoap.getWeather("上海", null); List<String> list=arrayOfString.getString(); System.out.println(list); for(String s:list){ System.out.println(s+"***"); } } }
2.CXF方式
CXF支持map,jdk自带不支持map数据结构
使用wsdl2java +wsdl文件地址 生成客户端调用代码
cxf3.1.4添加日志服务器和客户端拦截器
服务器
package server; import java.util.List; import org.apache.cxf.frontend.ServerFactoryBean; import org.apache.cxf.interceptor.Interceptor; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.message.Message; public class ServerPublish { public static void main(String[] args) { String address="http://localhost:8090/sayhello"; HelloWSImpl hw = new HelloWSImpl(); ServerFactoryBean sfb = new ServerFactoryBean(); sfb.setServiceClass(HelloWS.class); sfb.setServiceBean(hw); sfb.setAddress(address); //服务器端的日志入拦截器 List<Interceptor<? extends Message>> inInterceptors =sfb.getInInterceptors(); inInterceptors.add(new LoggingInInterceptor() ); //服务器端的日志出拦截器 List<Interceptor<? extends Message>> outInterceptors =sfb.getOutInterceptors(); outInterceptors.add(new LoggingOutInterceptor()); sfb.create(); System.out.println("发布服务成功"); } }
客户端
package client; import java.util.List; import org.apache.cxf.endpoint.Client; import org.apache.cxf.frontend.ClientProxy; import org.apache.cxf.interceptor.Interceptor; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.message.Message; import server.HelloWS; import server.HelloWSPortType; public class client_test { public static void main(String[] args) { HelloWS helloWS = new HelloWS(); HelloWSPortType helloWSPortType = helloWS.getHelloWSPort(); Client client=ClientProxy.getClient(helloWSPortType); //客户端的日志入拦截器 List<Interceptor<? extends Message>> inInterceptors =client.getInInterceptors(); inInterceptors.add(new LoggingInInterceptor() ); //客户端的日志出拦截器 List<Interceptor<? extends Message>> outInterceptors =client.getOutInterceptors(); outInterceptors.add(new LoggingOutInterceptor()); String s=helloWSPortType.sayHello(" yuanhai"); System.out.println(s); } }
cxf添加自定义拦截器实现密码用户名验证
客户端拦截器
package client; import java.util.List; import javax.xml.namespace.QName; import org.apache.cxf.binding.soap.SoapMessage; import org.apache.cxf.headers.Header; import org.apache.cxf.helpers.DOMUtils; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.phase.AbstractPhaseInterceptor; import org.apache.cxf.phase.Phase; import org.w3c.dom.Document; import org.w3c.dom.Element; public class AddUserInterceptor extends AbstractPhaseInterceptor<SoapMessage>{ private String name; private String password; public AddUserInterceptor(String name, String password) { super(Phase.PRE_PROTOCOL);//准备协议化时拦截 this.name=name; this.password=password; } /* 在soap中加个head <Envelope> <head> <atguigu> <name>yuanhai</name> <password>123456<password> </atguigu> <atguigu2> <name>yuanhai</name> <password>123456<password> </atguigu2> </head> <Body> <sayHello> <arg0>yuanhai</arg0> </sayHello> </Body> </Envelope> */ @Override public void handleMessage(SoapMessage msg) throws Fault { List<Header> headers= msg.getHeaders(); Document document=DOMUtils.createDocument(); Element rootEle=document.createElement("atguigu"); Element nameEle=document.createElement("name"); nameEle.setTextContent(name); Element passwordEle=document.createElement("password"); passwordEle.setTextContent(password); rootEle.appendChild(nameEle); rootEle.appendChild(passwordEle); headers.add(new Header(new QName("atguigu"), rootEle));//QName必须跟rootEle的标签一样 System.out.println("client handleMessage()..."); } }
调用代码
package client; import java.util.List; import org.apache.cxf.endpoint.Client; import org.apache.cxf.frontend.ClientProxy; import org.apache.cxf.interceptor.Interceptor; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.message.Message; import server.HelloWS; import server.HelloWSPortType; public class client_test { public static void main(String[] args) { HelloWS helloWS = new HelloWS(); HelloWSPortType helloWSPortType = helloWS.getHelloWSPort(); Client client=ClientProxy.getClient(helloWSPortType); //客户端的日志入拦截器 List<Interceptor<? extends Message>> inInterceptors =client.getInInterceptors(); inInterceptors.add(new LoggingInInterceptor() ); //客户端的日志出拦截器 List<Interceptor<? extends Message>> outInterceptors =client.getOutInterceptors(); outInterceptors.add(new LoggingOutInterceptor()); outInterceptors.add(new AddUserInterceptor("yuanhai","123456")); String s=helloWSPortType.sayHello(" yuanhai"); System.out.println(s); } }
服务端拦截器
package server; import javax.xml.namespace.QName; import org.apache.cxf.binding.soap.SoapMessage; import org.apache.cxf.headers.Header; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.phase.AbstractPhaseInterceptor; import org.apache.cxf.phase.Phase; import org.w3c.dom.Element; /* * 检查用户的拦截器 */ public class CheckUserInterceptor extends AbstractPhaseInterceptor<SoapMessage> { public CheckUserInterceptor() { super(Phase.PRE_PROTOCOL); } @Override public void handleMessage(SoapMessage msg) throws Fault { Header header= msg.getHeader(new QName("atguigu")); if(header!=null){ Element atguiguEle =(Element) header.getObject(); String name=atguiguEle.getElementsByTagName("name").item(0).getTextContent(); String password=atguiguEle.getElementsByTagName("password").item(0).getTextContent(); if("yuanhai".equals(name)&&"123456".equals(password)){ System.out.println("Server 通过拦截器..."); return; } } //不能通过 System.out.println("Server 未通过拦截器..."); throw new Fault(new RuntimeException("请求需要一个正确的用户名和密码")); } }
调用代码
package server; import java.util.List; import org.apache.cxf.frontend.ServerFactoryBean; import org.apache.cxf.interceptor.Interceptor; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.message.Message; public class ServerPublish { public static void main(String[] args) { String address="http://localhost:8090/sayhello"; HelloWSImpl hw = new HelloWSImpl(); ServerFactoryBean sfb = new ServerFactoryBean(); sfb.setServiceClass(HelloWS.class); sfb.setServiceBean(hw); sfb.setAddress(address); //服务器端的日志入拦截器 List<Interceptor<? extends Message>> inInterceptors =sfb.getInInterceptors(); inInterceptors.add(new LoggingInInterceptor() ); inInterceptors.add(new CheckUserInterceptor()); //服务器端的日志出拦截器 List<Interceptor<? extends Message>> outInterceptors =sfb.getOutInterceptors(); outInterceptors.add(new LoggingOutInterceptor()); sfb.create(); System.out.println("发布服务成功"); } }
3.cxf与spring集成
server段代码
实体类order
package bean; public class Order { public Order(int id, String name, double price) { this.id = id; this.name = name; this.price = price; } private int id; private String name; private double price; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }
webservice接口
package ws; import javax.jws.WebMethod; import javax.jws.WebService; import bean.Order; @WebService public interface OrderWS { @WebMethod public Order getOrderById(int id); }
webservice实现类
package ws; import bean.Order; public class OrderWsImpl implements OrderWS { public OrderWsImpl(){ System.out.println("OrderWsImpl()"); } @Override public Order getOrderById(int id) { System.out.println("server getOrderById()"+id); return new Order(id, "飞机", 100000); } }
spring配置文件beans.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- 引入cxf的一些核心配置--> <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="orderWS" implementor="ws.OrderWsImpl" address="/orderws" > <jaxws:inInterceptors> <bean class="ws.CheckUserInterceptor"/> </jaxws:inInterceptors> </jaxws:endpoint> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://java.sun.com/xml/ns/javaee" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:beans.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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>/*</url-pattern> </servlet-mapping> </web-app>
客户端
spring配置文件client-beans.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <jaxws:client id="orderClient" serviceClass="ws.OrderWS" address="http://localhost:8080/webservice_cxf_spring_server/orderws"> <jaxws:outInterceptors> <bean class="test.AddUserInterceptor"> <constructor-arg name="name" value="yuanhai"/> <constructor-arg name="password" value="123456"/> </bean> </jaxws:outInterceptors> </jaxws:client> </beans>
客户端调用代码
package test; import org.springframework.context.support.ClassPathXmlApplicationContext; import ws.Order; import ws.OrderWS; public class client_test { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "client-beans.xml" }); OrderWS orderWs = (OrderWS) context.getBean("orderClient"); Order order = orderWs.getOrderById(24); System.out.println(order); } }