JAX-WS + Spring integration example
来源:互联网 发布:java api 数量 编辑:程序博客网 时间:2024/05/29 17:28
(http://www.mkyong.com/webservices/jax-ws/jax-ws-spring-integration-example/)
Here’s a guide to show you how to integrate Spring with JAX-WS, as mention in this link : http://jax-ws-commons.java.net/spring/. Upon finishing this tutorial, you will create a simple HelloWorld web service (JAX-WS), and DI a bean into the web service via Spring.
1. Project Folder
See the final project folder structure.
2. Project Dependencies
Use Maven to get all the library dependencies. The key to integrate Spring with JAX-WS is via jaxws-spring.jar.
File : 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.mkyong</groupId> <artifactId>WebServicesExample</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>WebServicesExample Maven Webapp</name> <url>http://maven.apache.org</url> <repositories> <repository> <id>java.net</id> <url>http://download.java.net/maven/2</url> </repository> </repositories> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency><!-- Spring framework --> <dependency><groupId>org.springframework</groupId><artifactId>spring</artifactId><version>2.5.6</version></dependency> <!-- JAX-WS --> <dependency> <groupId>com.sun.xml.ws</groupId> <artifactId>jaxws-rt</artifactId> <version>2.2.3</version></dependency> <!-- Library from java.net, integrate Spring with JAX-WS --> <dependency><groupId>org.jvnet.jax-ws-commons.spring</groupId><artifactId>jaxws-spring</artifactId><version>1.8</version><exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </exclusion> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </exclusion> <exclusion> <groupId>com.sun.xml.stream.buffer</groupId> <artifactId>streambuffer</artifactId> </exclusion> <exclusion> <groupId>org.jvnet.staxex</groupId> <artifactId>stax-ex</artifactId> </exclusion></exclusions></dependency> </dependencies> <build> <finalName>web services</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.1</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build></project>
3. JAX-WS Hello World
A simple JAX-WS example, and dependency inject (DI) “HelloWorldBo” via Spring.
File : HelloWorldWS.java
package com.mkyong.ws;import javax.jws.WebMethod;import javax.jws.WebService;import com.mkyong.bo.HelloWorldBo;@WebServicepublic class HelloWorldWS{//DI via SpringHelloWorldBo helloWorldBo;@WebMethod(exclude=true)public void setHelloWorldBo(HelloWorldBo helloWorldBo) {this.helloWorldBo = helloWorldBo;}@WebMethod(operationName="getHelloWorld")public String getHelloWorld() {return helloWorldBo.getHelloWorld();} }
4. Beans
Here’s the HelloWorldBo class, with a getHelloWorld()
method to return a simple string.
File : HelloWorldBo.java
package com.mkyong.bo;public interface HelloWorldBo{String getHelloWorld();}
File : HelloWorldBoImpl.java
package com.mkyong.bo.impl;import com.mkyong.bo.HelloWorldBo;public class HelloWorldBoImpl implements HelloWorldBo{public String getHelloWorld(){return "JAX-WS + Spring!";}}
5. Spring Beans Configuration
Spring beans configuration file to bind URL pattern “/hello” to “com.mkyong.ws.HelloWorldWS” web service class.
File : applicationContext.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:ws="http://jax-ws.dev.java.net/spring/core" xmlns:wss="http://jax-ws.dev.java.net/spring/servlet" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://jax-ws.dev.java.net/spring/core http://jax-ws.dev.java.net/spring/core.xsd http://jax-ws.dev.java.net/spring/servlet http://jax-ws.dev.java.net/spring/servlet.xsd"> <wss:binding url="/hello"> <wss:service> <ws:service bean="#helloWs"/> </wss:service> </wss:binding> <!-- Web service methods --> <bean id="helloWs" class="com.mkyong.ws.HelloWorldWS"> <property name="helloWorldBo" ref="HelloWorldBo" /> </bean> <bean id="HelloWorldBo" class="com.mkyong.bo.impl.HelloWorldBoImpl" /> </beans>
6. web.xml
In web.xml, declares “com.sun.xml.ws.transport.http.servlet.WSSpringServlet
“, and link it to “/hello
“.
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><display-name>Spring + JAX-WS</display-name><servlet> <servlet-name>jaxws-servlet</servlet-name> <servlet-class> com.sun.xml.ws.transport.http.servlet.WSSpringServlet </servlet-class> </servlet><servlet-mapping> <servlet-name>jaxws-servlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <!-- Register Spring Listener --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> </web-app>
7. Demo
Start the project, and access the deployed web service via URL “/hello“, for examplehttp://localhost:8080/WebServicesExample/hello?wsdl
Reference
- JAX-WS + Java Web Application Integration Example
- http://jax-ws-commons.java.net/spring/
- http://weblogs.java.net/blog/kohsuke/archive/2007/01/spring_support.html
- JAX-WS + Spring integration example
- JAX-WS Services Integration
- Apache CXF JAX-WS example
- JAX-WS Hello World Example
- JAX-WS整合Spring
- Spring整合JAX-WS
- Jax-Ws集成spring
- Wicket + Spring integration example
- TestNG + Spring Integration Example
- jax-ws spring client 详解
- JSF 2.0 + Spring integration example
- Struts + Spring + Hibernate integration example
- Spring MVC + Log4j Integration Example
- JAX-WS Hello World Example – RPC Style
- JAX-WS+spring开发webservice配置
- JAX-WS 和 Spring 整合开发步骤:
- JAX-WS Spring集成jax方式的webservice
- JAX-WS
- 使用MongoDB实现分片集
- Windows安装mysql5.7.14
- Ref : Troubleshooting Guide for HotSpot VM
- myeclipse安装反编译插件jad
- 华为刷题
- JAX-WS + Spring integration example
- 收录一些JS方面问题解决
- 线性结构---堆栈
- Html5新增标签
- 【IOS学习】iOS本地数据存取,看这里就够了
- Netty学习笔记—IO模型
- 【Android】如何加速 AndroidStudio 的编译效率
- AndroidStudio2.1.2版本使用jdk1.7完美创建工程(不用jdk1.8)
- Java在linux系统的部署(3): 系统环境的安装——安装VirtualBox安装增强功能