CXF使用笔记
第一次用CXF的服务端,写个简单的笔记。
●一、引用的包
直接上maven的pom,里面嵌套引用了好多jar,看起来不是那么爽。
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>2.6.0</version></dependency><dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>2.6.0</version></dependency>
@WebServicepublic class TestService{ public String doSomething(String msg){ System.out.println("有人来访问了,消息:" + msg); return "谢谢惠顾,您的消息是:" + msg; }}<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <description>WebService配置</description> <jaxws:endpoint implementor="TestService" address="/test"/></beans>
<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>/ws/*</url-pattern> </servlet-mapping>
CXF主目录/bin/wsdl2java -p test -d d: http://host:port/app/ws/test?wsdl
TestService.javaDoSomething.javaDoSomethingResponse.javaObjectFactory.java
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="client" factory-method="create"/> <bean id="clientFactory" value="test.TestService"/> <!-- 这个类是刚才生成的 --> <property name="address" value="http://host:port/app/ws/test"/> <!-- 这个路径是服务器端配置的 --></bean></beans>
public class ClientTest {public static void main(String args[]) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"}); TestService client = context.getBean(TestService.class); String res = client.doSomething("Hi, boy. I come!"); System.out.println(res); System.exit(0); }}<!-- WSS4J --><dependency> <groupId>org.apache.ws.security</groupId> <artifactId>wss4j</artifactId> <version>1.6.5</version> <scope>runtime</scope></dependency><dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-ws-security</artifactId> <version>2.6.0</version></dependency>
import javax.security.auth.callback.Callback;import javax.security.auth.callback.CallbackHandler;public class PasswordCallback implements CallbackHandler {@Overridepublic void handle(Callback[] callbacks) throws Exception{for (Callback cb : callbacks){WSPasswordCallback pc = (WSPasswordCallback)cb; if ("user123".equals(pc.getIdentifier())){ pc.setPassword("pwd123"); }}}}<jaxws:endpoint implementor="TestService" address="/test"/>
<jaxws:endpoint implementor="TestService" address="/test"><jaxws:inInterceptors><ref bean="wss4JInInterceptor"/></jaxws:inInterceptors></jaxws:endpoint><bean id="wss4JInInterceptor" value="UsernameToken"/><entry key="passwordType" value="PasswordDigest"/><entry key="passwordCallbackRef"><bean name="code">public class ClientPasswordCallback implements CallbackHandler {@Overridepublic void handle(Callback[] callbacks) throws Exception{for (Callback cb : callbacks){WSPasswordCallback pc = (WSPasswordCallback)cb; pc.setPassword("pwd123");}}}<bean id="wss4JOutInterceptor" value="UsernameToken" /> <!-- 要和服务端的配置一致 --><entry key="passwordType" value="PasswordDigest" /> <!-- 要和服务端的配置一致 --><entry key="user" value="user123" /> <!-- 用户名在这里设置 --><entry key="passwordCallbackRef"><bean name="code"><bean id="clientFactory" value="test.TestService"/><property name="address" value="http://host:port/app/ws/test"/><property name="outInterceptors"><list><ref bean="wss4JOutInterceptor"/></list></property></bean>