<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>2.7.1</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>2.7.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.1.2.RELEASE</version> </dependency>
package com.cj.webservice; import javax.jws.WebService; @WebService public interface HelloWorld { String sayHi(String text); }
注:这里只是作为简单的sample开发示例展示,暴露的接口较为简单。
package com.cj.webservice;
import javax.jws.WebService;
@WebService(endpointInterface = "demo.hw.server.HelloWorld",
serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {
@Override
public String sayHi(String text) {
System.out.println("sayHi called");
return "Hello" + text ;
}
}
<?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:soap="http://cxf.apache.org/bindings/soap" xsi:schemaLocation= " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <jaxws:server id="jaxwsService" serviceClass="demo.hw.server.HelloWorld" address="/hello_world"> <jaxws:serviceBean> <bean class="demo.hw.server.HelloWorldImpl" /> </jaxws:serviceBean> </jaxws:server> <!-- 也可以通过下面定义的方式注册 --> <!-- <jaxws:endpoint id="jaxwsService" address="/hello_world" implementor="demo.hw.server.HelloWorldImpl"> </jaxws:endpoint> --> </beans>
<?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" > <display-name>cxf</display-name> <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>/services/*</url-pattern> </servlet-mapping> </web-app>
<?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:soap= "http://cxf.apache.org/bindings/soap" xsi:schemaLocation= " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.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:server id ="jaxwsService" serviceClass="demo.hw.server.HelloWorld" address= "/hello_world"> <jaxws:serviceBean > <bean class ="demo.hw.server.HelloWorldImpl" /> </jaxws:serviceBean> </jaxws:server > <!-- <jaxws:endpoint id="jaxwsService" address="/hello_world" implementor="demo.hw.server.HelloWorldImpl"> </jaxws:endpoint> --> </beans>
<?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" > <display-name>cxf</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/service.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>/services/*</url-pattern> </servlet-mapping> </web-app>
package demo.hw.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import demo.hw.server.HelloWorld;
import demo.hw.server.User;
import demo.hw.server.UserImpl;
public final class Client {
public static void main(String args[]) throws Exception {
String endpointAddress = "http://localhost:8080/java_first_jaxws/services/hello_world";
JaxWsProxyFactoryBean clientFactory = new JaxWsProxyFactoryBean();
clientFactory.setAddress(endpointAddress);
clientFactory.setServiceClass(HelloWorld.class);
HelloWorld hw = (HelloWorld)clientFactory.create();
System.out.println(hw.sayHi("World"));
}
}
<jaxws:client id= "client" serviceClass="demo.hw.server.HelloWorld"
address="http://127.0.0.1:8080/java_first_jaxws/services/hello_world" />
<bean id="client" class="demo.hw.server.HelloWorld" factory-bean="clientFactory" factory-method="create" /> <bean id ="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name ="serviceClass" value= "demo.hw.server.HelloWorld" /> <property name ="address" value= "http://127.0.0.1:8080/java_first_jaxws/services/hello_world" /> </bean>
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:conf/spring/cxf/cxf-client.xml" ); HelloWorld hw = (HelloWorld)ac.getBean("client"); System.out.println(hw.sayHi("chenjie"));