Apache CXF: Writing a service with Spring
一、设置构建路径(必要的jar包)
commons-logging-1.1.jar
geronimo-activation_1.1_spec-1.0-M1.jar (or Sun's Activation jar)
geronimo-annotation_1.0_spec-1.1.jar (JSR 250)
geronimo-javamail_1.4_spec-1.0-M1.jar (or Sun's JavaMail jar)
geronimo-servlet_2.5_spec-1.1-M1.jar (or Sun's Servlet jar)
geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181)
jaxb-api-2.0.jar
jaxb-impl-2.0.5.jar
jaxws-api-2.0.jar
neethi-2.0.jar
saaj-api-1.3.jar
saaj-impl-1.3.jar
stax-api-1.0.1.jar
wsdl4j-1.6.1.jar
wstx-asl-3.2.1.jar
XmlSchema-1.2.jar
xml-resolver-1.2.jar
aopalliance-1.0.jar
spring-core-2.0.4.jar
spring-beans-2.0.4.jar
spring-context-2.0.4.jar
spring-web-2.0.4.jar
cxf-2.0-incubator.jar
geronimo-activation_1.1_spec-1.0-M1.jar (or Sun's Activation jar)
geronimo-annotation_1.0_spec-1.1.jar (JSR 250)
geronimo-javamail_1.4_spec-1.0-M1.jar (or Sun's JavaMail jar)
geronimo-servlet_2.5_spec-1.1-M1.jar (or Sun's Servlet jar)
geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181)
jaxb-api-2.0.jar
jaxb-impl-2.0.5.jar
jaxws-api-2.0.jar
neethi-2.0.jar
saaj-api-1.3.jar
saaj-impl-1.3.jar
stax-api-1.0.1.jar
wsdl4j-1.6.1.jar
wstx-asl-3.2.1.jar
XmlSchema-1.2.jar
xml-resolver-1.2.jar
aopalliance-1.0.jar
spring-core-2.0.4.jar
spring-beans-2.0.4.jar
spring-context-2.0.4.jar
spring-web-2.0.4.jar
cxf-2.0-incubator.jar
二、编写服务类
a). 接口类HelloService.java
package
demo.spring;
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebService;
import demo.spring.model.UserInfoBean;
@WebService
public interface HelloService {
public String sayHello(@WebParam(name="name")String text);
public UserInfoBean getUserInfo(@WebParam(name="username")String name);
public List<UserInfoBean> getUsers();
}
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebService;
import demo.spring.model.UserInfoBean;
@WebService
public interface HelloService {
public String sayHello(@WebParam(name="name")String text);
public UserInfoBean getUserInfo(@WebParam(name="username")String name);
public List<UserInfoBean> getUsers();
}
b).实现类HelloServiceImpl.java
package
demo.spring;
import java.util.ArrayList;
import java.util.List;
import javax.jws.WebService;
import demo.cxf.server.HelloService;
import demo.spring.model.UserInfoBean;
@WebService(endpointInterface = " demo.spring.HelloService " )
public class HelloServiceImpl implements HelloService {
public String sayHello(String text) {
return "Hello,".concat(text);
}
public UserInfoBean getUserInfo(String username){
UserInfoBean user = new UserInfoBean();
user.setRealname("捷克");
user.setUsername(username);
user.setEmail("[email protected]");
return user;
}
public List<UserInfoBean> getUsers(){
List<UserInfoBean> users = new ArrayList<UserInfoBean>();
users.add(getUserInfo("john"));
return users;
}
}
import java.util.ArrayList;
import java.util.List;
import javax.jws.WebService;
import demo.cxf.server.HelloService;
import demo.spring.model.UserInfoBean;
@WebService(endpointInterface = " demo.spring.HelloService " )
public class HelloServiceImpl implements HelloService {
public String sayHello(String text) {
return "Hello,".concat(text);
}
public UserInfoBean getUserInfo(String username){
UserInfoBean user = new UserInfoBean();
user.setRealname("捷克");
user.setUsername(username);
user.setEmail("[email protected]");
return user;
}
public List<UserInfoBean> getUsers(){
List<UserInfoBean> users = new ArrayList<UserInfoBean>();
users.add(getUserInfo("john"));
return users;
}
}
三、声明服务Bean
在WEB-INF目录下创建成一个beans.xml文件,以声明endpoint bean:
<?
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/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" />
< jaxws:endpoint id ="helloService" implementor ="demo.spring.HelloServiceImpl" address ="/HelloService" />
</ beans >
< 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/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" />
< jaxws:endpoint id ="helloService" implementor ="demo.spring.HelloServiceImpl" address ="/HelloService" />
</ beans >
若使用Spring定义的bean,也可这样声明:
<
bean
id
="helloService"
class
="demo.spring.HelloServiceImpl"
/>
< jaxws:endpoint id ="helloServiceImpl" implementor ="#helloService" address ="/HelloService" />
< jaxws:endpoint id ="helloServiceImpl" implementor ="#helloService" address ="/HelloService" />
四、定义Servlet
修改WEB-INF/目录下的web.xml文件,添加如下定义:
<
context-param
>
< param-name > contextConfigLocation </ param-name >
< param-value > WEB-INF/beans.xml </ param-value >
</ context-param >
< listener >
< listener-class >
org.springframework.web.context.ContextLoaderListener
</ listener-class >
</ listener >
< servlet >
< servlet-name > CXFServlet </ servlet-name >
< display-name > CXF Servlet </ display-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 >
< param-name > contextConfigLocation </ param-name >
< param-value > WEB-INF/beans.xml </ param-value >
</ context-param >
< listener >
< listener-class >
org.springframework.web.context.ContextLoaderListener
</ listener-class >
</ listener >
< servlet >
< servlet-name > CXFServlet </ servlet-name >
< display-name > CXF Servlet </ display-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 >
五、客户端代码
client_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.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd" >
< bean id ="client" class ="demo.spring.HelloService"
factory-bean ="clientFactory" factory-method ="create" />
< bean id ="clientFactory" class ="org.apache.cxf.jaxws.JaxWsProxyFactoryBean" >
< property name ="serviceClass" value ="demo.spring.HelloService" />
< property name ="address" value ="http://localhost:8080/cxf/HelloService" />
</ bean >
</ beans >
< 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.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd" >
< bean id ="client" class ="demo.spring.HelloService"
factory-bean ="clientFactory" factory-method ="create" />
< bean id ="clientFactory" class ="org.apache.cxf.jaxws.JaxWsProxyFactoryBean" >
< property name ="serviceClass" value ="demo.spring.HelloService" />
< property name ="address" value ="http://localhost:8080/cxf/HelloService" />
</ bean >
</ beans >
client.java
ClassPathXmlApplicationContext context
= new ClassPathXmlApplicationContext( new String[] {"demo/spring/client/client_beans.xml"} );
HelloService client = (HelloService)context.getBean( " client " );
String response = client.sayHello( " Joe " );
System.out.println( " Response: " + response);
UserInfoBean user = client.getUserInfo( " jack " );
System.out.println(user.getUsername());
List < UserInfoBean > users = client.getUsers();
for (UserInfoBean u : users) {
System.out.println(u.getUsername());
}
System.exit( 0 );
= new ClassPathXmlApplicationContext( new String[] {"demo/spring/client/client_beans.xml"} );
HelloService client = (HelloService)context.getBean( " client " );
String response = client.sayHello( " Joe " );
System.out.println( " Response: " + response);
UserInfoBean user = client.getUserInfo( " jack " );
System.out.println(user.getUsername());
List < UserInfoBean > users = client.getUsers();
for (UserInfoBean u : users) {
System.out.println(u.getUsername());
}
System.exit( 0 );