昨天用Eclipse生成了WebService的服务端和客户端,今天尝试不用Eclipse的插件生成,自己写一个服务端,可能因为昨天碰到的不少问题都得到了解决,今天写起来还算比较顺利,现在把过程贴出来,以后可能会用得着。
1、创建一个web工程,DynamicWeb Project (File->New->Dynamic Web Project),取名叫“ws-server-hand”,这个就不多说了。
2、编写提供服务的接口和实现类,这个步骤和用插件生成相同,注意标注不要遗漏。
AddService.java
package com.ws.server;
import javax.jws.WebService;
@WebService(targetNamespace="http://server.ws.com/")
public interface AddService {
public int add(int a, int b);
}
AddServcieImpl.java
package com.ws.server.impl;
import javax.jws.WebService;
import com.ws.server.AddService;
@WebService(endpointInterface = "com.ws.server.AddService")
public class AddServcieImpl implements AddService {
@Override
public int add(int a, int b) {
return a + b;
}
}
3、导入CXF框架所需要的jar包,可以去
官方网站下载,下载解压后,把apache-cxf-3.1.6\lib目录下的jar包引用到java项目中。
4、在WEB-INF目录下创建cxf-beans.xml文件,因为CXF内部是整合Spring的,为了让Spring能够注入提供服务的类,需要添加一个"jaxws:endpoint"标签,在这个标签里指定提供服务的实现类。
cxf-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-2.5.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:endpoint xmlns:tns="http://server.ws.com/" id="addservice"
implementor="com.ws.server.impl.AddServcieImpl" address="/AddService">
</jaxws:endpoint>
</beans>
5、修改web.xml,让web.xml能够读到cxf-beans.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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>ws-server</display-name>
<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>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/cxf-beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
6、所有的编码都已经完成,现在只需要启动服务器,访问
http://localhost:8080/ws-server-hand/services/测试一下是否能发布服务。
访问结果:
打开WSDL连接,显示如下内容,说明发布成功。
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.server.ws.com/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http"
xmlns:ns1="http://server.ws.com/" name="AddServcieImplService"
targetNamespace="http://impl.server.ws.com/">
<wsdl:import
location="http://localhost:8080/ws-server-hand/services/AddService?wsdl=AddService.wsdl"
namespace="http://server.ws.com/"></wsdl:import>
<wsdl:binding name="AddServcieImplServiceSoapBinding"
type="ns1:AddService">
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="add">
<soap:operation soapAction="" style="document" />
<wsdl:input name="add">
<soap:body use="literal" />
</wsdl:input>
<wsdl:output name="addResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="AddServcieImplService">
<wsdl:port binding="tns:AddServcieImplServiceSoapBinding"
name="AddServcieImplPort">
<soap:address
location="http://localhost:8080/ws-server-hand/services/AddService" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
昨天在搞CXF的时候发现一个不错的网站http://www.javatips.net/,里面有好多关于Java框架的操作手册,简单易懂,收藏一下,以供以后参考学习。