标签:
新建一个maven项目(or下载cxf所需jar包),pom.xml如下
1.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.webservice</groupId> <artifactId>WebserviceProject</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>WebserviceProject Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-api</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-bindings-soap</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>2.5.0</version> </dependency> </dependencies> <build> <finalName>WebserviceProject</finalName> </build> </project>
2.新建一个配置文件application.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:context="http://www.springframework.org/schema/context" 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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd" > <bean id="greetingServiceImpl" class="com.webservice.server.GreetingServiceImpl"/> <jaxws:endpoint id="greetingService" implementor="#greetingServiceImpl" address="/Greeting" /> </beans>
3.配置web.xml文件,加载xml和配置url
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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> <!--==这个设置很重要,那么我们的webservice的地址就是http://localhost:8080/webserviceProject/webservice/Greeting===--> <url-pattern>/webservice/*</url-pattern> </servlet-mapping> </web-app>
4 新建server实体类和接口
Greeting.java
package com.webservice.server; import javax.jws.WebService; @SuppressWarnings("restriction") @WebService public interface Greeting { public String greeting(String userName); }
GreetingServiceImpl.java
package com.webservice.server; import java.util.Calendar; import javax.jws.WebService; @SuppressWarnings("restriction") @WebService(endpointInterface = "com.webservice.server.Greeting") public class GreetingServiceImpl implements Greeting { public String greeting(String userName) { return "Hello " + userName + ", currentTime is " + Calendar.getInstance().getTime(); } }
5.server端已经完成,接下来创建一个client端类Test
GreetingServiceClient.java
package com.webservice.client; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import com.webservice.server.Greeting; public class GreetingServiceClient { public static void main(String[] args) { //创建WebService客户端代理工厂 JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); //注册WebService接口 factory.setServiceClass(Greeting.class); //设置WebService地址 factory.setAddress("http://localhost:8080/WebserviceProject/webservice/Greeting"); Greeting greetingService = (Greeting)factory.create(); System.out.println("invoke webservice..."); System.out.println("message context is:"+greetingService.greeting("gary")); } }
标签:
原文地址:http://www.cnblogs.com/jianlun/p/4668105.html