`
nlslzf
  • 浏览: 1049016 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring – Exposing a bean as both REST (xml, json, …) and SOAP WebServices

    博客分类:
  • cxf
阅读更多
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

    标题中的“Exposing Windows Workflow as Web Services”是指将Windows Workflow Foundation (WF)的工作流实例作为Web服务公开,以便外部系统可以调用和交互。Windows Workflow Foundation是.NET Framework的一部分...

    spring-boot-reference.pdf

    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....

    exposing workflow as web service

    标题“exposing workflow as web service”涉及到的核心概念是工作流(Workflow)与Web服务(Web Service)的结合。工作流是一种管理系统中业务过程的方式,它规范了任务的分配、执行和监控,通常用于自动化和协调...

    Manning.Spring.in.Action.4th.Edition.2014.11.epub

    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. ...

    Spring中文帮助文档

    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 ...

    Spring API

    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 ...

    mastering-spring-cloud2018

    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 ...

    Cloud Computing Principles and Paradigms

    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, ...

    Just a Dummy Python API created using Flask and exposing metrics

    Just a Dummy Python API created using Flask and exposing metrics

    Mastering.Modern.Web.Penetration.Testing

    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 ...

    Javaone_Based Framework for Integrating Request_Response XML Services.pdf

    标题中的“Javaone_Based Framework for Integrating Request_Response XML Services”表明,本文档是关于一个基于Javaone的框架,用于集成请求响应XML服务。这一框架的设计目的是为了方便地将XML Web服务层集成到...

    Professional C# 3rd Edition

    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 ...

    Seamless R and C++ Integration

    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 ...

    Spring-Data-REST-Demo

    3. **HATEOAS (Hypermedia as the Engine of Application State)**: Spring Data REST 遵循 HATEOAS 原则,返回的响应包含链接,这些链接指示了资源的可用操作和其他相关资源。这使得客户端可以根据链接动态地探索...

    Getting MEAN with Mongo, Express, Angular, and Node(Manning,2015)

    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 ...

    Modern.Python.Cookbook.epub

    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 ...

    Go.Programming.Blueprints.2nd

    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 ...

Global site tag (gtag.js) - Google Analytics