前面一篇文章中,webservice的服务端与客户端都是单独启动,但是在现实项目中,服务端单独启动太没有实际意义了,还是要整合框架启动,所以今天将记录如何整合spring框架。
jar包下载地址如下:
http://yun.baidu.com/share/link?shareid=547689626&uk=2836507213
(一)、web.xml中添加spring与cxf的配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<?xml version= "1.0" encoding= "UTF-8" ?>
<web-app version= "2.5" xmlns= "http://java.sun.com/xml/ns/javaee" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http: //java.sun.com/xml/ns/javaee
http: //java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- spring配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener- class >org.springframework.web.context.ContextLoaderListener</listener- class >
</listener>
<!-- cxf配置 -->
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app> |
(二)、新建接口文件,实现类
接口,需要添加注解webservice
1
2
3
4
5
6
|
@WebService public interface IHello {
public String sayHi(String name);
public String printName(String name);
} |
实现接口类,指定endpointInterface与serviceName
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@WebService (endpointInterface= "com.xj.service.IHello" ,serviceName= "hello1Service" )
public class HelloImpl implements IHello{
@Override
public String sayHi(String name) {
System.out.println( "hi," +name);
return "hi," +name;
}
@Override
public String printName(String name) {
System.out.println( "my name is," +name);
return "my name is," +name;
}
} |
(三)、配置applicationContext.xml配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?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= "hello" class = "com.xj.service.HelloImpl" />
<jaxws:endpoint id= "testHello" implementor= "#hello" address= "/testHello" >
</jaxws:endpoint>
</beans> |
需要在文件中引入这三个文件,加入相关约束
<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标签,这与直接启动server时,endpoint的publish作用相同,发布这个服务,implementor指定了实现类,这里用的是引用的方式,可以直接如下
1
2
3
|
<!-- <bean id= "hello" class = "com.xj.service.HelloImpl" /> -->
<jaxws:endpoint id= "testHello" implementor= "com.xj.service.HelloImpl" address= "/testHello" >
</jaxws:endpoint>
|
(四)、项目部署并访问
将项目部署到tomcat中,启动无报错
访问 http://localhost/cxf/testHello?wsdl 可得到我们发布服务的wsdl,其中cxf为项目名称,testHello为我们endpoint中配置的路径
到此为止,服务端就发布完成了。
一般项目中大部分都是单独提供server端接口(即发布服务的wsdl地址),或者根据wsdl地址生成client端。
附加
(五)、启动client端
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?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">
<jaxws:client id= "hello" serviceClass= "com.xj.service.IHello" address= "http://localhost/cxf/testHello?wsdl" >
</jaxws:client>
</beans> |
解释:
jaxws:client 标志为服务的client端,其中address指定了需要访问的wsdl地址,id指定了一个唯一的bean标志。
1
2
3
4
5
6
|
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext( "applicationContextClient.xml" );
IHello hello = (IHello) ctx.getBean( "hello" );
System.out.println(hello.sayHi( "xiejun" ));
}
|
如上述,就可以调用服务的相关方法,但是这种调用方法需要项目中含有server端的基础类,如IHello等,显然不太现实。下一篇文章中讲继续记录 如何调用天气预报的接口,现在在那我们是没法知道server端的基础类的。
本文转自布拉君君 51CTO博客,原文链接:http://blog.51cto.com/5148737/1606499,如需转载请自行联系原作者