By
zhpooer
更新日期:
入门
Apache CXF = Celtix + Xfire, 开源 web Service 框架, 支持多种协议,
soap1.2,1.1, XML/HTTP, RESTful, CORBA
Cxf基于SOA总线结果, 依靠 spring 完成模块的集成
demo
服务端
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
32
| @WebService(targetNamespace="http://itcast.cn",
serviceName="WeatherService")
@BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
public interface IWeatherService {
@WebMethod(operationName="queryWeatherByCityName")
public @WebResult(name="weatherResult") String queryWeatherByCityName(@WebParam("cityname") String city){}
}
public class WeatherService implements IWeatherService {
public String queryWeatherByCityName(String cityName){
return "晴朗";
}
}
publci static void main(String[] args) {
JaxWsServerFactoryBean jaxWsServerFactoryBean = new JaxWsServerFactoryBean();
jaxWsServerFactoryBean.setAddress("http://ip:port/weather");
jaxWsServerFactoryBean.sestServiceClass(WeaterServiceInterface.class);
jaxWsServerFactoryBean.setServiceBean(new WeatherServiceImpl.class);
jaxWsServerFactoryBean.getInInterceptpor().add(new LoggingInInterceptor());
jaxWsServerFactoryBean.getOutInerceptpor().add(new LoggingOutInterceptor());
jaxWsServerFactoryBean.create();
}
|
客户端
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class WeatherClient {
public static void main(String [] args) {
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
jaxWsProxyFactoryBean.setAddress("http://ip:port/weather");
jaxWsProxyFactoryBean.setServiceClass(WeatherService.class);
WeatherServiceInterface weatherServiceInterface = jaxWsProxyFactoryBean.create();
String result = weatherServiceInterface.queryWeather("郑州");
}
}
|
Cxf 与 spring 集成
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"?>
<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.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<jaxws:server id="weather" address="/weather"
serviceClass="cn.itcast.ws.cxf.server.WeatherInterface">
<jaxws:serviceBean>
<bean class="cn.itcast.ws.cxf.server.WeatherInterfaceImpl"></bean>
</jaxws:serviceBean>
<jaxws:inInterceptors>
<bean class="LoggingInIntercetpor"> </bean>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="LoggingOutIntercetpor"> </bean>
</jaxws:outInterceptors>
</jaxws:server>
</beans>
|
1
2
3
4
5
6
7
8
9
10
11
12
|
<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>/ws/*</url-pattern>
</servlet-mapping>
|
客户端调用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| <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.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<jaxws:client id="weatherClient" address="http://ip:port/ws/weather"
serviceClass="WeatherServiceInterface">
</jaxws:client>
</beans>
|
1
2
3
4
5
| public static void main (){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
WeatherServiceInterface weatherClient = applicationContext.getBean("weatherClient");
String result = weatherClient.query("郑州");
}
|
身份过滤
Apache WSS4J(WebService Security For Java)实现了JAVA 语言的 WS-Security,
CXF 中使用拦截器机制完成 WSS4J 功能的支持 WSS4JInInterceptor 和 WSS4JOutInterceptor
WSS4JInInterceptor
输入拦截器,用于服务端校验密码
WSS4JOutInterceptor
输出拦截器,用于客户端发送密码
服务端
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
<bean id="wss4jInInterceptor" class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor">
<constructor-arg>
<map>
<entry key="action" value="UsernameToken" />
<entry key="passwordType" value="PasswordText" />
<entry key="passwordCallbackClass" value="cn.itcast.ws.cxf.server.interceptor.ServerPasswordCallbackHandler" />
</map>
</constructor-arg>
</bean>
</jaxws:inInterceptors>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| public class ServerPasswordCallbackHandler implements CallbackHandler {
public void handle(Callback[] callbacks) {
WSPasswordCallback wsPwCallback = callbacks[0];
String userid_fromclient = wsPwCallback.getIdentifier();
if(userid_fromclient.equals(userid)) {
wsPasswordCallback.setPassword(passwd);
} else {
throw new IOException("用户身份不合法");
}
}
}
|
客户端
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<jaxws:client id="weatherServicePort"
address="http://localhost:8080/14webservice_cxf_spring/ws/weather?wsdl" serviceClass="webservice.itcast.cn.WeatherServicePort">
<jaxws:outInterceptors>
<ref bean="wss4jOutInterceptor" />
</jaxws:outInterceptors>
</jaxws:client>
<bean id="wss4jOutInterceptor" class="org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor">
<constructor-arg>
<map>
<entry key="action" value="UsernameToken" />
<entry key="user" value="mrt" />
<entry key="passwordType" value="PasswordDigest" />
<entry key="passwordCallbackClass"
value="cn.itcast.ws.cxf.client.interceptor.ClientPasswordCallbackHandler" />
</map>
</constructor-arg>
</bean>
|
1
2
3
4
5
6
7
| public class ClientPasswordCallbackHandler implements CallbackHandler {
public void handle(Callback[] callbacks) {
WSPasswordCallback wspassCallback = (WSPasswordCallback) callbacks[0];
wspassCallback.setPassword(PASSWORD);
wspassCallback.setIdentifier(USER);
}
}
|
便民查询网站
使用springmvc+CXF实现便民网站,同时对外提供webservice服务。
调用别人webservice,将自己的服务发布成webservice
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
| public class WeatherClient {
public List<String> queryWeatherByCityName(String cityname) {
WeatherWebService weatherWebService = new WeatherWebService();
WeatherWebServiceSoap weatherWebServiceSoap = weatherWebService.getWeatherWebServiceSoap();
ArrayOfString arrayOfString = weatherWebServiceSoap.getWeatherbyCityName(cityname);
List<String> resultlist = arrayOfString.getString();
return resultlist;
}
}
public class Weather {
private String result;
private String img;
private String img2;
}
public interface IWeatherService {
public List<Weather> queryWeatherByCityName(String cityname);
}
public class WeatherServiceImpl implements IWeatherService {
@Autowired
private WeatherClient weatherClient;
public List<Weather> queryWeatherByCityName(String cityname){
List<String> resutsList = weatherClient.queryWeatherByCityName(cityname);
List<Weather> list = new ArrayList<Weather>();
WeatherModel weatherModel = new WeatherModel();
return list;
}
}
@Controller
public class WeatherAction {
@Autowired
WeatherServiceInterface weatherServiceInterface;
@RequestMapping("/queryWeather")
public String queryWeather(String cityName, Model model) {
List<Weather> list = weatherServiceInterface.queryWeatherByCityName(cityName);
model.addAttribute("weatherresult", list);
return "queryweather";
}
}
|
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
32
33
| <?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.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<bean id="weatherService" class="cn.itcast.ws.cxf.service.impl.WeatherServiceInterfaceImpl" />
<bean id="webWeatherClient" class="cn.itcast.ws.cxf.client.WeatherClient" />
<jaxws:server id="weatherServer" address="/weather" serviceClass="cn.itcast.ws.cxf.service.WeatherServiceInterface">
<jaxws:serviceBean>
<ref bean="weatherService"/>
</jaxws:serviceBean>
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
</jaxws:outInterceptors>
</jaxws:server>
</beans>
|
springmvc-servlet.xml
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
32
33
34
35
36
37
38
39
40
| <?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">
<context:component-scan base-package="cn.itcast.ws.cxf.action" />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
|
web.xml
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
| <?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>07193webservice_cxf_spring</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CharacterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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>
</web-app>
|