服务端WebService接口使用Spring发布,客户端使用java测试。 服务端: 导入对应的jar包,编写服务接口并实现,和之前方式相同,然后在src下加个配置文件,引入cxf的核心配置。在web.xml引
服务端WebService接口使用Spring发布,客户端使用java测试。
服务端:
导入对应的jar包,编写服务接口并实现,和之前方式相同,然后在src下加个配置文件,引入cxf的核心配置。在web.xml引入这个配置文件,开启spring监听,引入CXFServlet处理请求。
package ws.spring.server; public class UserBean { private int id; private String name; public UserBean(int id, String name) { super(); this.id = id; this.name = name; } 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; } @Override public String toString() { return "UserBean [id=" + id + ", name=" + name + "]"; } }
package ws.spring.server; import javax.jws.WebMethod; import javax.jws.WebService; @WebService public interface UserWS { @WebMethod public UserBean getUserById(int id); }
package ws.spring.server; import javax.jws.WebService; @WebService public class UserWSImpl implements UserWS { public UserWSImpl(){ System.out.println("初始化 UserWSImpl"); } @Override public UserBean getUserById(int id) { System.out.println("server getUserById:"+id); return new UserBean(1, "张三"); } }
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.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/jaxws"> <!-- 引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="userWS" implementor="ws.spring.server.UserWSImpl" address="/userws"> </jaxws:endpoint> </beans>
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>ws_spring_server</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <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> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
客户端:
导入jar包,生成客户端代码,在src下引入配置文件,编写测试类。
生成的客户端代码:
GetUserById.java
GetUserByIdResponse.java
ObjectFactory.java
package-info.java
UserBean.java
UserWS.java
UserWSImplService.java
配置文件:
client-beans.xml
测试代码:
package ws.spring.server; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestClient { public static void main(String[] args) { ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext( new String[]{"client-beans.xml"}); UserWS userWS=(UserWS) context.getBean("userClient"); UserBean userBean=userWS.getUserById(2); System.out.println("Client:"+userBean); } }
有个问题:
如果在ws_spring_server项目里,加个jsp页面,当你用http://localhost:8080/ws_spring_server/index.jsp进行访问的时候,会报错:找不到index.jsp的Observer,
问题在于web.xml的配置:
<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>/*</url-pattern> </servlet-mapping>/*说明任何请求,都会使用CXFServlet处理,而我们访问index.jsp也使用这个Servlet进行了处理,这样显然不合理。把/*修改为:/WS/*,就可以了加以区分了。WS可以改为任意名称,如/WebService/*.
访问wsdl的时候也需要修改路径,前面加上对应的前缀:如果改为了/WS/*,访问wsdl路径变成了:
http://localhost:8080/ws_spring_server/WS/userws?wsdl。注意:beans.xml不需要做任何修改。
对应的客户端代码的配置需要跟/WS/*,修改为:
address= "http://localhost:8080/ws_spring_server/WS/userws">
前面也加上对应的WS.