http://raulraja.com/2009/06/25/spring-exposing-a-bean-as-rest-xml-json-and-soap-webservice/
Often times you have Spring services that you want to expose as web-services. In this small tutorial I’ll just show a few configuration files and code that demonstrate how a service and its implementation can accommodate both REST and SOAP access to the same backbone.
Thanks Justin for the REST part.
This tutorial is available for download with svn:
svn checkout http://raulrajatutorials.googlecode.com/svn/trunk/ raulrajatutorials-read-only
Comments in the code itself should be self-explanatory if you already have some java + spring experience.
I have not included the dependencies on CXF for simplicity but you can take a look at them and some for the other tutorials here.
And now to the point.
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: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/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/core
http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd"
default-autowire="byName">
<!-- Load CXF modules from cxf.jar -->
<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"/>
<import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"/>
<cxf:bus>
<cxf:features>
<cxf:logging/>
</cxf:features>
</cxf:bus>
<!-- The hello world service -->
<bean id="helloWorldService" class="com.raulraja.ws.impl.HelloWorldServiceImpl" autowire="autodetect"/>
<!-- Exposing the Helloworld service as a rest service -->
<jaxrs:server id="restServer" address="/rest/">
<jaxrs:serviceBeans>
<ref bean="helloWorldService"/>
</jaxrs:serviceBeans>
<jaxrs:extensionMappings>
<entry key="feed" value="application/atom+xml"/>
<entry key="json" value="application/json"/>
<entry key="xml" value="application/xml"/>
<entry key="html" value="text/html"/>
</jaxrs:extensionMappings>
</jaxrs:server>
<!-- Exposing the HelloWorld service as a SOAP service -->
<bean id="jaxbBean"
class="org.apache.cxf.jaxb.JAXBDataBinding"
scope="prototype"/>
<bean id="jaxws-and-aegis-service-factory"
class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean"
scope="prototype">
<property name="dataBinding" ref="jaxbBean"/>
<property name="serviceConfigurations">
<list>
<bean class="org.apache.cxf.jaxws.support.JaxWsServiceConfiguration"/>
<bean class="org.apache.cxf.aegis.databinding.AegisServiceConfiguration"/>
<bean class="org.apache.cxf.service.factory.DefaultServiceConfiguration"/>
</list>
</property>
</bean>
<jaxws:endpoint id="helloWorldServiceEndpoint"
serviceName="HelloWorld"
implementorClass="com.raulraja.ws.HelloWorldService"
implementor="#helloWorldService"
address="/helloWorldService">
<jaxws:serviceFactory>
<ref bean="jaxws-and-aegis-service-factory"/>
</jaxws:serviceFactory>
</jaxws:endpoint>
</beans>
HelloWorldService.java
package com.raulraja.ws;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.jws.WebService;
import javax.jws.WebMethod;
/**
* The hello world service interface exposed as soap and rest
*
* @ WebService is for soap
* @ Path is for the rest top path
*/
@Path("/helloWorldService/")
@WebService(serviceName = "HelloWorld", name = "HelloWorldService", targetNamespace = "http://ws.raulraja.com")
public interface HelloWorldService {
/**
* Simple methods that says hello
*
* @return hello world rest and soap!!!
* @ WebMethod is for soap
* @ GET is for REST
* @ Path is for the REST service path
*/
@WebMethod
@GET
@Path("/")
HelloWorld sayHello();
}
HelloWorldServiceImpl.java
package com.raulraja.ws.impl;
import com.raulraja.ws.HelloWorld;
import com.raulraja.ws.HelloWorldService;
/**
* Implementation of the hello world web service.
*/
public class HelloWorldServiceImpl implements HelloWorldService {
/**
* Simple methods that says hello
*
* @return hello world rest and soap!!!
*/
public HelloWorld sayHello() {
HelloWorld helloWorld = new HelloWorld();
helloWorld.setMessage("Hello world rest and soap!!!");
return helloWorld;
}
}
HelloWorld.java
package com.raulraja.ws;
import javax.xml.bind.annotation.XmlRootElement;
/**
* Hello world object that demonstrates the service
*/
@XmlRootElement
public class HelloWorld {
/**
* the message
*/
private String message;
/**
* @return the message
*/
public String getMessage() {
return message;
}
/**
* @param message sets the message
*/
public void setMessage(String message) {
this.message = message;
}
}
And here is some screen captures for the results…
Soap Services list
分享到:
相关推荐
Exposing Windows WorkFlow as Web services.zip
Registering Servlets, Filters, and Listeners as Spring Beans 27.4.2. Servlet Context Initialization Scanning for Servlets, Filters, and listeners 27.4.3. The ServletWebServerApplicationContext 27.4.4....
15.3. Exposing remote services with Hessian and Burlap 15.3.1. Exposing bean functionality with Hessian/Burlap 15.3.2. Accessing Hessian/Burlap services 15.4. Using Spring’s HttpInvoker 15.4.1. ...
exposing workflow as web service
6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.6.1. 理解AOP代理 6.7. 以编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ进行domain ...
6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.6.1. 理解AOP代理 6.7. 以编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ进行domain ...
used as an API gateway and proxy for Spring Cloud applications: Spring Cloud Netlix Zuul and Spring Cloud Gateway. You will learn how to integrate them with service discovery and create simple and ...
data/storage, and applications) as standards-based Web services and following a “utility” pricing model where customers are charged based on their utilization of computational resources, storage, ...
Creating RESTful Web Services API Documentation Using Swagger 2 together with Spring Boot Testing API with Swagger UI Spring Boot Actuator features Application information Health information Metrics ...
Chapter 21: Web Applications and Web Services 459 REST: The Architecture of the Web 460 Characteristics of REST 460 A Distributed Network of Interlinked Documents 461 A Client-Server Architecture ...
This book covers the latest technologies such as Advance XSS, XSRF, SQL Injection, Web API testing, XML attack vectors, OAuth 2.0 Security, and more involved in today's web applications Penetrate and ...
Implementing IDisposable and a Destructor 196 Unsafe Code 197 Pointers 198 Pointer Example: PointerPlayaround 207 Using Pointers to Optimize Performance 212 Summary 216 Chapter 8: Strings and Regular ...
The Rcpp-introduction vignette (also published as a JSS paper) provides a good entry point to Rcpp as do the Rcpp website, the Rcpp page and the Rcpp Gallery. Full documentation is provided by the ...
Designing systems as web services Social networking systems using peer-to-peer computing Principles of cloud computing using examples from open-source and commercial applications Using examples from ...
just as you'd do in a real project.Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications. About the Technology Traditional web dev stacks use ...
By exposing Python as a series of simple recipes, you can gain insight into specific language features in a particular context. Having a tangible context helps make the language or standard library ...
Chapter 6: Exposing Data and Functionality through a RESTful Data Web Service API Chapter 7: Random Recommendations Web Service Chapter 8: Filesystem Backup Chapter 9: Building a Q&A Application for ...
recognition as a key ingredient of modern industrial and business engineering. Our reliance on data—and the role that it can play in the discovery and confirmation of science, engineering, business, ...
and creating flat files, as well as providing guidance for working with flat files. It then covers envelope processing for XML documents, to handle message assembly and disassembly. Chapter 11, ...