一、通过jdk发布一个ws 这是最简单的方法。
1.通过@WebService标记这个类可以发布为一个web服务。
2.通过Endpoint.publish的类以及他的静态方法将该类发不成一个web服务
3.通过http://192.168.12.65:6789/hello?wsdl 查看我们发布的服务的"说明书"。
4.通过wsimport -s +http://192.168.12.65:6789/hello?wsdl 命令生成客户端打代码调用WebService的方法
a.发布服务
package cn.maitian.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
import javax.xml.ws.WebServiceContext;
@WebService
public class HelloService{
public String sayHello(String name){
return "hello"+name;
}
@WebMethod(exclude=true)//发布的方法不包含改方法
public String sayHello2(String name){
return "hello"+name;
}
/**
*
* @Description: TODO
* @param @param args
* @return void
* @throws
* @author hzk
* @date 2015-11-30
*/
public static void main(String[] args) {
Endpoint.publish("http://192.168.12.65:6789/hello", new HelloService());
System.out.println("Server read....");
}
}
b.调用服务(wsimport -s +http://192.168.12.65:6789/hello?wsdl 命令生成客户端打代码调用WebService的方法)
public class App {
public static void main(String[] args) {
// TODO Auto-generated method stub
HelloServiceService hss=new HelloServiceService();
HelloService hs=hss.getHelloServicePort();
System.out.println(hs.sayHello("王宝强"));
System.out.println(hs.sayHello2("王宝强"));
}
}
二。CXF 框架同spring框架整合发布webService
1.引入cxf 的所有jar包。
2.编写接口和实现类代码 对外发布
a.接口代码
package cn.maitian.cxf;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;
@WebService
@BindingType(value=SOAPBinding.SOAP12HTTP_BINDING)
public interface IHelloService {
public String sayHello(String name);
public String testClient();
}
b.实现类代码
package cn.maitian.cxf;
public class HelloServiceImpl implements IHelloService {
public String sayHello(String name){
System.out.println("正在调用 sayHello()");
return "hello: "+name;
}
public String testClient(){
System.out.println("正在调用 testClient()");
return "OK";
}
}
c.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"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://cxf.apache.org/jaxrshttp://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/corehttp://cxf.apache.org/schemas/core.xsd">
<!-- 引入CXF Bean定义如下,早期的版本中使用 -->
<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:server id="hiService" serviceClass="cn.maitian.cxf.IHelloService" address="/hi">
<jaxws:serviceBean>
<!-- 服务的实现类 -->
<bean class="cn.maitian.cxf.HelloServiceImpl"></bean>
</jaxws:serviceBean>
<!-- 加入请求的消息拦截器 -->
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
</jaxws:inInterceptors>
<!-- 加入响应的消息拦截器 -->
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxws:outInterceptors>
</jaxws:server>
</beans>
d.web.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<!-- 配置CXF的核心Servlet -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:cxf-servlet.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/cxf/'*'</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3.客户端代码的编写
a.通过wsdl2java -d . url 的命令来生成客户端代码。但是客户端代码只应用一个接口文件即可完成程序调用
但是需要客户端引入所有的cxf的jar包。
package com.test.cxf.client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
//3.通过spring配置文件配置jaws 对象的属性,并且创建对象。获取对象后调用方法-2。
ApplicationContext a=new ClassPathXmlApplicationContext("com/test/cxf/client/CreatBean.xml");
IHelloService h = (IHelloService) a.getBean("hiService");
System.out.println(h.sayHello("hzk22"));
System.out.println(h.testClient());
}
}
b.客户端配置文件
<?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"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://cxf.apache.org/jaxrshttp://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/corehttp://cxf.apache.org/schemas/core.xsd">
<!-- 通过配置文件的方式生成对象,在java代码中获取给对象后调用服务-1 -->
<!-- <jaxws:client id="hiService" address="http://localhost:8088/CXFWEB/cxf/hi?wsdl" serviceClass="com.test.cxf.client.IHelloService"></jaxws:client> -->
<!-- 通过上述两个方法进行改造 -->
<jaxws:client id="hiService" address="http://localhost:8088/CXFWEB/cxf/hi?wsdl" serviceClass="com.test.cxf.client.IHelloService">
<!-- <jaxws:outInterceptors>
<ref bean="handleInterceptor"/>
</jaxws:outInterceptors> -->
</jaxws:client>
</beans>
注释:需要源码或者有问题的同学可以私信我。或者而留下邮箱地址。