本文主要是介绍spring整合cxf 使用jax-ws规范实现webservice功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一.新建服务端
新建项目:spring-jax-ws-server
1.pom文件的编写:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd"><parent><artifactId>web-service-demo</artifactId><groupId>com.meboth.bonc.ljf</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><groupId>com.meboth.bonc.ljf.spring.jax</groupId><artifactId>spring-jax-ws-server</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><name>spring-jax-ws-server Maven Webapp</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><!-- 要进行jaxws 服务开发 --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.1.2</version></dependency><!-- 内置jetty web服务器 --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-jetty</artifactId><version>3.1.2</version></dependency><!-- 日志实现 --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.25</version></dependency><!--spring 需要的jar包 --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.4.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.2.4.RELEASE</version></dependency><!-- 解决jar包冲突--><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency></dependencies><build><finalName>spring-jax-ws-server</finalName><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><!-- maven的jdk编译插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.3</version><configuration><source>1.7</source><target>1.7</target><encoding>UTF-8</encoding><showWarnings>true</showWarnings></configuration></plugin><!-- 运行tomcat7方法:tomcat7:run --><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.2</version><configuration><!-- 指定端口 --><port>8080</port><!-- 请求路径 --><path>/</path></configuration></plugin></plugins></pluginManagement></build>
</project>
2.接口实现类
package com.ljf.spring.jax.ws.server.service;import javax.jws.WebService;@WebService
public interface BeiJingUserService {//问好public String sayHelloToBeijingName(String name);
}package com.ljf.spring.jax.ws.server.serviceImpl;import com.ljf.spring.jax.ws.server.service.BeiJingUserService;public class BeiJingUserServiceImpl implements BeiJingUserService {@Overridepublic String sayHelloToBeijingName(String name) {return name+":nihao ,today is 端午节!!!!";}
}
3.resources文件下的applicationContext资源文件
<?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:cxf="http://cxf.apache.org/core"xmlns:jaxws="http://cxf.apache.org/jaxws"xmlns:jaxrs="http://cxf.apache.org/jaxrs"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/corehttp://cxf.apache.org/schemas/core.xsdhttp://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsdhttp://cxf.apache.org/jaxrshttp://cxf.apache.org/schemas/jaxrs.xsd
"><!--Spring整合ApacheCXF,发布jaxws服务:1. 服务地址2. 服务bean 完整服务地址:http://localhost:8080/ws/userService--><jaxws:server address="/beijinguser"> <!--可以任意起名 --><jaxws:serviceBean><bean class="com.ljf.spring.jax.ws.server.serviceImpl.BeiJingUserServiceImpl"></bean></jaxws:serviceBean></jaxws:server>
</beans>
4.web.xml文件
<!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:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- webservice服务端,发布服务需要配置CXFServlet --><!-- 这里配置的servlet路径,最为最终服务路径的一部分: --><!-- 服务访问路径:http://localhost:8080/web.xml配置路径/spring配置的路径 --><servlet><servlet-name>cxfServlet</servlet-name><servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class></servlet><servlet-mapping><servlet-name>cxfServlet</servlet-name><url-pattern>/ws/*</url-pattern></servlet-mapping>
</web-app>
5.index.jsp页面
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
6.启动tomcat服务:
二.新建客户端
新建项目:spring-jax-ws-client
1.pom文件:
<?xml version="1.0" encoding="UTF-8"?><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/xsd/maven-4.0.0.xsd"><parent><artifactId>web-service-demo</artifactId><groupId>com.meboth.bonc.ljf</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><groupId>com.meboth.bonc.ljf.spring.jax</groupId><artifactId>spring-jax-ws-client</artifactId><packaging>war</packaging><name>spring-jax-ws-client Maven Webapp</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.7</maven.compiler.source><maven.compiler.target>1.7</maven.compiler.target></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><!-- 要进行jaxws 服务开发 --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.1.2</version></dependency><!-- 内置jetty web服务器 --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-jetty</artifactId><version>3.1.2</version></dependency><!-- 日志实现 --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.25</version></dependency><!--spring 需要的jar包 --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.4.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.2.4.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.2.4.RELEASE</version></dependency><!-- 解决jar包冲突--><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>RELEASE</version></dependency></dependencies><build><finalName>spring-jax-ws-client</finalName><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><!-- maven的jdk编译插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.3</version><configuration><source>1.7</source><target>1.7</target><encoding>UTF-8</encoding><showWarnings>true</showWarnings></configuration></plugin><!-- 运行tomcat7方法:tomcat7:run --><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.2</version><configuration><!-- 指定端口 --><port>8080</port><!-- 请求路径 --><path>/</path></configuration></plugin></plugins></pluginManagement></build>
</project>
2.接口
package com.ljf.spring.jax.ws.client.service;import javax.jws.WebService;@WebService(targetNamespace = "http://service.server.ws.jax.spring.ljf.com/")
public interface BeiJingClientUserService {//注意要和服务端的方法名保持一致,不一致的话,报错public String sayHelloToBeijingName(String name);
}
3.资源配置类:applicationContext的内容
<?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:cxf="http://cxf.apache.org/core"xmlns:jaxws="http://cxf.apache.org/jaxws"xmlns:jaxrs="http://cxf.apache.org/jaxrs"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/corehttp://cxf.apache.org/schemas/core.xsdhttp://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsdhttp://cxf.apache.org/jaxrshttp://cxf.apache.org/schemas/jaxrs.xsd
"><!--Spring整合ApacheCXF,客户端配置关键点:通过Spring整合ApacheCXF,创建客户端的代理对象,远程访问服务端。jaxws:clientid 应用中注入的接口的代理对象的名称address 服务端访问地址serviceClass 指定接口路径,会根据该类型生成代理对象--><jaxws:clientid="bcuserClient"address="http://localhost:8080/ws/beijinguser"serviceClass="com.ljf.spring.jax.ws.client.service.BeiJingClientUserService"></jaxws:client>
</beans>
4.启动类:
package com.ljf.spring.jax.ws.client;import com.ljf.spring.jax.ws.client.service.BeiJingClientUserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;import javax.annotation.Resource;public class SpringJaxClient {// 注入远程访问服务端的接口的代理对象。public static void main(String args[]){ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });BeiJingClientUserService userService = (BeiJingClientUserService) context.getBean("bcuserClient");// org.apache.cxf.jaxws.JaxWsClientProxy@2826f61System.out.println(userService);// class com.sun.proxy.$Proxy45System.out.println(userService.getClass());// 远程调用服务接口String content = userService.sayHelloToBeijingName("球球");System.out.println(content);}
}
注意这里的web.xml和index.jsp页面,可以忽略。
5.调用(确保服务启动,且是正常的)
这篇关于spring整合cxf 使用jax-ws规范实现webservice功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!