`
beike
  • 浏览: 362776 次
社区版块
存档分类
最新评论

[ZT]Spring Web Service

 
阅读更多

Wednesday, April 6, 2011

Spring Web Service - Part III ( Creating Web Services by java bean both for SOAP 1.1 & SOAP 1.2)

In the Last part of the servies, we will learn how to Develop web services that will support both SOAP 1.1 and SOAP 1.2 protocol, (Spring WS does not provide out of box support for this and we need to tweak the things little bit).
There are many ways through which you can generate the Web services, like
  • Manually creating the WSDL file and place it in WEB-INF directory of your web application.
  • Creating the Request XML XSD and let the framework to create the WSDL
The Second one is preferred approach as compared to the first one, as you does not have to code the WSDL directly either with tool or yourself. 
In order to create the following tutorial, i had taken help from a number of blogs, following are the their links, however I could not find a complete one, so summarizing all this in a single post.
Listed here are the steps:
  1. First we will create a xsd which represents the schema of the input xml, below is a snipped of it,  It only contains three fields that need to be passed to the Server, ( ID, Name, Email) and in response two things are being returned to the client ( CODE, DESCRIPTION)
    
        
            
                
                    
                    
                    
                
            
        
    
     
            
                
                    
                    
                    
                
            
        
    
        
            
                
                 
                 
                
            
        
    
        
            
                
                     
                     
                
            
        
    
        
            
                
                
            
        
    
        
      
       
        
        
        
       
      
        
    
        
         
       
        
        
       
      
        
    
    
    
    it Clearly Shows that the contents of Subscription Request and Subscription Response, as described above.
  2. Now we need to create the Java Objects corresponds to both Request and Response.
    SubscriptionRequest.java

    package com.test;
    
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    
    /**
     *
     * @author PankajB
     */
    @XmlRootElement(namespace="http://krams915.blogspot.com/ws/schema/oss",name="subscriptionRequest")
    public class SubscriptionRequest {
    
     private String id;
     private String name;
     private String email;
    
       @XmlElement(name="email",namespace="http://krams915.blogspot.com/ws/schema/oss")
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
       @XmlElement(name="id",namespace="http://krams915.blogspot.com/ws/schema/oss")
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        @XmlElement(name="name",namespace="http://krams915.blogspot.com/ws/schema/oss")
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
    
    }
    
     SubscriptionResponse.java
    package com.test;
    
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    
    /**
     *
     * @author PankajB
     */
    @XmlRootElement(namespace = "http://krams915.blogspot.com/ws/schema/oss", name = "subscriptionResponse")
    public class SubscriptionResponse {
    
        private String code;
        private String description;
    
        @XmlElement(name = "code", namespace = "http://krams915.blogspot.com/ws/schema/oss")
        public String getCode() {
            return code;
        }
    
        public void setCode(String code) {
            this.code = code;
        }
    
        @XmlElement(name = "description", namespace = "http://krams915.blogspot.com/ws/schema/oss")
        public String getDescription() {
            return description;
        }
    
        public void setDescription(String description) {
            this.description = description;
        }
    }
    
    

  3. Now we need to write our main POJO which which handle the request and generate a response. It pretty simply class, annotated with some special annotations.
    package com.test;
    
    import javax.xml.ws.soap.SOAPBinding;
    import org.springframework.ws.server.endpoint.annotation.Endpoint;
    import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
    import org.springframework.ws.server.endpoint.annotation.RequestPayload;
    import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
    
    /**
     *
     * @author PankajB
     */
    @Endpoint(value = SOAPBinding.SOAP12HTTP_BINDING)
    public class SubscriptionService {
    
        @PayloadRoot(localPart = "subscriptionRequest", namespace = "http://krams915.blogspot.com/ws/schema/oss")
        @ResponsePayload
        public SubscriptionResponse processSubscription(@RequestPayload SubscriptionRequest subscriptionRequest) {
    
    
            SubscriptionResponse response = new SubscriptionResponse();
            System.out.println("Coming Here.........  " + subscriptionRequest.getName());
            response.setCode("234");
            response.setCode("Successfully Executed the application.");
    
            return response;
    
        }
    }
    
    
    here it simply, receive the Payload ( what is being sent by the client in the XML and converted to our SubscriptionRequest Object and generates an SubscriptionResponse type of object, which will again be automatically going to be converted by the Spring Jaxb marshaller in the XML format.
    This Ends the first part of the story, now let's move to the configuration.
  4. First of all in the configuration files, let's visit web.xml
        
            spring-ws
            org.springframework.ws.transport.http.MessageDispatcherServlet
            
                transformWsdlLocationstrue
            1
        
        
            spring-ws
            /service/*
        
        
            
                30
            
        
        
            index.jsp
        
    
    
    
    This Simply tells the container that this servlet is going to receive any of the call that matches with the URI /service/*

  5. Now we have the spring-ws-servlet.xml (spring-ws comes from servlet name above in web.xml) which will define all the components related to Spring Web Service.
        
        
        
       
            
         
        
        
        
        
        
            
      
        
            
                
                    com.test.SubscriptionRequest
                    com.test.SubscriptionResponse
    
                
            
            
                
                    
                
            
        
    
        
            
        
    
     
        
    
     
        
            
            
        
    
     
        
                
        
       
    
    

    Most of the Things has been commenting in the XML itself, for better understanding.

  6. Since we need to support both SOAP 1.1 & SOAP 1.2 we need to write out own factory, that is going to be an implementation of SoapMessageFactory.
    /*
     *
     * This All has been done as per the things present at this link.
     * http://forum.springsource.org/showthread.php?t=56560
     *
     */
    
    package com.test.soap;
    
    import java.io.IOException;
    import java.io.InputStream;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.beans.factory.InitializingBean;
    import org.springframework.web.context.request.RequestAttributes;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.ws.WebServiceMessage;
    import org.springframework.ws.soap.SoapMessage;
    import org.springframework.ws.soap.SoapMessageFactory;
    import org.springframework.ws.soap.SoapVersion;
    import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
    import org.springframework.ws.transport.TransportInputStream;
    
    public class MyFactory implements SoapMessageFactory, InitializingBean {
            // This is the Request Context Attribute.
     private static final String REQUEST_CONTEXT_ATTRIBUTE = "MyFactory";
    
     private static final Log logger = LogFactory.getLog(MyFactory.class);
    
            // Two message factories for processing two differnet types of protocols.
     private SaajSoapMessageFactory soap11MessageFactory = new SaajSoapMessageFactory();
     private SaajSoapMessageFactory soap12MessageFactory = new SaajSoapMessageFactory();
    
            // This Object, will be responsible for choosing the Protocol on Runtime, it can be application/xml or text/xml (SOAP 1.2 & SOAP 1.1)
     private SoapProtocolChooser soapProtocolChooser = new MySoapProtocolChooser();
    
     private void setMessageFactoryForRequestContext(SaajSoapMessageFactory mf) {
      RequestAttributes attrs = RequestContextHolder.getRequestAttributes();
      attrs.setAttribute(REQUEST_CONTEXT_ATTRIBUTE, mf, RequestAttributes.SCOPE_REQUEST);
     }
    
     private SaajSoapMessageFactory getMessageFactoryForRequestContext() {
      RequestAttributes attrs = RequestContextHolder.getRequestAttributes();
      SaajSoapMessageFactory mf = (SaajSoapMessageFactory) attrs.getAttribute(REQUEST_CONTEXT_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
      return mf;
     }
    
            // Function called, when we are settign the SOPA Version
     public void setSoapVersion(SoapVersion version) {
                System.out.println("setSoapVersion called with: " + version + " -- ignoring");
     }
    
            // This Function, will set teh SOAP Proptocl chooser
     public void setSoapProtocolChooser(SoapProtocolChooser soapProtocolChooser) {
                System.out.println("Setting out the SOAP Protocol Chooser");
      this.soapProtocolChooser = soapProtocolChooser;
     }
    
            // Function will be invoked, when Spring will create the Bean.
     public void afterPropertiesSet() throws Exception {
      soap11MessageFactory.setSoapVersion(SoapVersion.SOAP_11);
      soap11MessageFactory.afterPropertiesSet();
      soap12MessageFactory.setSoapVersion(SoapVersion.SOAP_12);
      soap12MessageFactory.afterPropertiesSet();
                    System.out.println("Setting both the SOAP Version to 1.1 and 1.2");
     }
    
    
        // Function for creating the Web Service Message.
        public SoapMessage createWebServiceMessage() {
            return getMessageFactoryForRequestContext().createWebServiceMessage();
        }
    
        // Function for creating the Web Service Message from inputStream. 
        public SoapMessage createWebServiceMessage(InputStream inputStream) throws IOException {
            setMessageFactoryForRequestContext(soap12MessageFactory);
      if (inputStream instanceof TransportInputStream) {
                TransportInputStream transportInputStream = (TransportInputStream) inputStream;
             if (soapProtocolChooser.useSoap11(transportInputStream)) {
              setMessageFactoryForRequestContext(soap11MessageFactory);
             }
            }
      SaajSoapMessageFactory mf = getMessageFactoryForRequestContext();
      if (mf == soap11MessageFactory) {
       System.out.println("Final soapMessageFactory? " + soap11MessageFactory);
      } else {
       System.out.println("Final soapMessageFactory? " + soap12MessageFactory);
      }
      return mf.createWebServiceMessage(inputStream);
        }
    
        
    }
    
    This class is not alone itself, and works with the help of some associating classes & interfaces described below.
    SoapProtocolChooser.java Interface: This is the Interface that is being defined for Choosing a SOAP Protocol chooser class at run time.
    package com.test.soap;
    
    import java.io.IOException;
    
    import org.springframework.ws.transport.TransportInputStream;
    
    public interface SoapProtocolChooser {
     public boolean useSoap11(TransportInputStream transportInputStream) throws IOException;
    }
    
    MySoapProtocolChooser.java Implementation of Above Defined Interface:
    /**
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package com.test.soap;
    
    /**
     *
     * @author PankajB
     */
    import java.io.IOException;
    import java.util.Iterator;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.ws.transport.TransportInputStream;
    
    public class MySoapProtocolChooser implements SoapProtocolChooser {
    
        private static final Log logger = LogFactory.getLog(MySoapProtocolChooser.class);
        private static final Pattern userAgentPattern = Pattern.compile("html", Pattern.CASE_INSENSITIVE);
    
        public boolean useSoap11(TransportInputStream transportInputStream) throws IOException {
            for (Iterator headerNames = transportInputStream.getHeaderNames(); headerNames.hasNext();) {
                String headerName = (String) headerNames.next();
                logger.debug("found headerName: " + headerName);
                for (Iterator headerValues = transportInputStream.getHeaders(headerName); headerValues.hasNext();) {
                    String headerValue = (String) headerValues.next();
                    logger.debug("     headerValue? " + headerValue);
                    // Something weird with case names
                  /*  if (headerName.toLowerCase().contains("user-agent")) {
                    System.out.println("UserAgent  - " + headerValue);
                    Matcher m = userAgentPattern.matcher(headerValue);
                    if (m.find()) {
                    logger.debug("Found AXIS in header.  Using SOAP 1.1");
                    return true;
                    }
                    }*/
                    // This is the code written in order to support multiple Endpints by selection of SOAP
                    if (headerName.toLowerCase().contains("content-type")) {
                        logger.debug("Content Type  - " + headerValue);
    
                        if (headerValue.trim().toLowerCase().contains("text/xml")) {
                            logger.debug("Found text/xml in header.  Using SOAP 1.1");
                            return true;
                        }
    
                    }
                }
            }
            return false;
        }
    }
    
    
    The Above code is pretty self explanatory.
    This Class is being invoked by MyFactory class in method createWebServiceMessage(InputStream) to determine which SOAP Message Factory is going to be used, either 1.1 or 1.2
That's all we required, now we can simply deploy our application to the web container after creating a WAR file.
Now simply call the HTTP URI: http://localhost:8080/SpringWSGen/service/service/SubService/subscription.wsdl
( Note: service has been writted two times because:
      1. we have service/* servlet mapping, so first one belongs to it.
      2. we have service/SubService in the dynamic WSDL generation tag in sprng-ws-servlet.xml, so second one refers to it. Obviously one can go ahead with any mappings he want )
you will see the following WSDL Generated. now it can be invoked in any tool example SOAP UI to get to know which one is being called. As the WSDL File has the bindings for both the SOAP 1.1 & SOAP 1.2

  So that's all for the time beings. here can you create Spring Web Services through this. I will have some interesting URL's thta just add to the above functionaltiy, that i will add later on.
Thanks Spring, for creating such a light weight framework.
Please let me know, in case you want the complete source code. I will host it somewhere.

8 comments:

  1. This comment has been removed by the author.

    Reply
  2. If you create the xsd first (as spring-ws logic is contract first), then use the jaxb xjc tool to generate all beans (request/response and pojo). Do i still need to create the Jaxb marshaller configuration and mapping?

    Reply
  3. Also is it necessary to define the endpointMapping and soap11EndpointMapping? I understand you did it to enable your WS with SOAP 1.1/1.2
    Can i make the configuration simpler, just defining what is inside the soap11EndpointMapping?

    I m trying to make an example I found but based on spring-ws 1.5 to work with brand new way of the 2.0.2 release. I have SoapUI trying to send request soap message, but My WS cannot find any mapping for the endpoint. So I m just thinking reading your example. Is it because of my bean definition and the marshalling missing? Or is it because I have to enable SOAP 1.1 and 1.2 as Spring-WS is lightweight but won't do everything for you...

    Thanks in advance for your answers

    Reply
  4. 1. YES, you need to create the JAXMarshaller and define the mapping. The reason is because by this way you are informing spring, that this class will be used to map either the request/response xml.
    2. SOAP 1.1 or 1.2 is just a choice, based upon how the service is being created, or how you want it to be, because of the difference in the MIME type of both the types.
    3. Yes, i think you can make your configuration simpler by defining only soap11Endpointmapping
    4. If you want to dive in little details, here is the link. http://krams915.blogspot.com/

    Reply
  5. I already read this blog. But it was not using jaxb. It uses Castor as OXM API

    My problem is, may be it is just eclipse playing with me, if I define the same spring-ws-servlet as you did. I got validation problem. Any chance I can download the full project.

    Can you provide the jars dependency you have to run your WebService application. May be I m just missing something.

    Fairly new to SoapUI, so I just located the WSDL, and make it create the simple request from that. But when running it, My WebService application keeps on not finding the endpoint and just gave him a 404 http error.

    Any advice is welcome.

    Reply
  6. Okay, my bad, I just did not understand the namespace power: referencing in my spring config a spring-beans 3.0 whereas I m using Spring 3.1 can hurt you badly.

    You definitely help me.

    Reply
  7. Thanks very much, this was REALLY helpful!

    Reply
  8. Thanks for this, I was able to set up Spring Integration with Spring Web Services to use both SOAP 1.1 and SOAP 1.2. I did not need to use the marshaling, or auto set up the WSDL, the one I already had worked. In your WSDL file, what are the binding differences between SOAP 1.1 and 1.2?

    Reply

 

 

分享到:
评论

相关推荐

    zt411-zt421-ug-zhcn_ZT411/ZT421_斑马打印机手册_

    斑马打印机ZT411和ZT421是工业级条码和标签打印机,广泛应用于制造业、物流、零售业等对打印质量和效率有高要求的领域。这两款打印机以其出色的性能、耐用性和易用性赢得了用户的信赖。这份中文手册详细地介绍了ZT...

    ZT213/ZT213LEEA规格书V2.10-低功耗RS232多通道USB收发器/驱动器芯片手册

    《ZT213/ZT213LEEA:低功耗RS232多通道USB收发器/驱动器芯片详解》 ZT213/ZT213LEEA是一款专为低功耗应用设计的RS232多通道USB收发器和驱动器芯片,广泛应用于数据通信、工业控制、物联网设备等领域。本文将深入...

    Zebra ZT230 条码打印机驱动

    斑马(Zebra)ZT230条码打印机是一款广泛应用在工业环境中的高效设备,其驱动程序是确保打印机正常工作的重要组成部分。本驱动程序专为Zebra的ZT210、ZT220及ZT230系列打印机设计,提供了全面的功能支持,以实现高...

    ZT410打印机IP地址设置网络打印机

    ZT410打印机IP地址设置网络打印机ZT410打印机IP地址设置网络打印机ZT410打印机IP地址设置网络打印机ZT410打印机IP地址设置网络打印机ZT410打印机IP地址设置网络打印机

    ZT7548 Datasheet Rev.1.0.3.pdf

    ZT7548是一款第五代电容式触控屏幕控制器,支持30x18或18x30的通道配置,可以同时检测最多10个触点。该控制器能够与最多8个键与TSP(触控屏面板)或FPC(柔性印刷电路板)模式一起工作,在多点触控时无扫描率下降的...

    斑马zt410中文库

    斑马(Zebra)ZT410是一款先进的桌面级条码打印机,被广泛应用于物流、零售、医疗、制造业等多个行业。这款打印机以其高效、耐用和易于操作的特点深受用户喜爱。"斑马zt410中文库"指的是为ZT410打印机特别设计的中文...

    斑马打印机ZT210用户指南

    "斑马打印机ZT210用户指南" 本用户指南旨在为Zebra ZT210/ZT220/ZT230打印机的用户提供操作和维护指南,该设备是一种工业级的条码打印机,具有高速打印、高速处理和高质量打印输出等特点。 版权信息 本手册的版权...

    斑马ZT510打印机驱动文件

    斑马ZT510打印机驱动文件

    RS485通信芯片zt13085e的原理图库和PCB库

    RS485通信芯片ZT13085E是一种常用的工业级串行通信接口芯片,主要用于构建远程、多节点的通信网络。在工业自动化、楼宇自动化、物联网设备等领域有广泛应用。ZT13085E的设计目标是提供一种高效、可靠的数据传输方式...

    斑马打印机(ZT210).docx

    斑马打印机ZT210是一款专业的工业级条形码和标签打印机,广泛应用于物流、零售、医疗等行业的标签制作。以下是对如何设置和使用斑马ZT210打印机的详细步骤: 首先,我们需要安装电脑驱动。双击下载好的驱动程序文件...

    基于SpringBoot+Vue的大创管理系统springbootj2zt8.zip

    该项目利用了基于springboot + vue + mysql的开发...Java、Python、Node.js、Spring Boot、Django、Express、MySQL、PostgreSQL、MongoDB、React、Angular、Vue、Bootstrap、Material-UI、Redis、Docker、Kubernetes

    zt-exec-1.9-API文档-中文版.zip

    赠送jar包:zt-exec-1.9.jar; 赠送原API文档:zt-exec-1.9-javadoc.jar; 赠送源代码:zt-exec-1.9-sources.jar; 赠送Maven依赖信息文件:zt-exec-1.9.pom; 包含翻译后的API文档:zt-exec-1.9-javadoc-API文档-...

    斑马zt210打印机驱动 v5.1.07.5146 官方版

    斑马zt210是一款专为中国市场设计的工业条码打印机,非常适合不需要频繁更换标签的条码标签应用。这里给大家提供斑马zt210驱动下载,推荐有需要的用户下载安装。斑马zt210打印机优势:◆ 节省空间* 小巧紧凑和流线型...

    Android 请求 WebAPI的案例

    在Android开发中,与WebAPI进行交互是常见的需求,例如获取服务器数据、发送用户信息等。本案例"Android请求WebAPI"将详细讲解如何在Android应用中实现这一功能。WebAPI通常指的是基于HTTP协议的RESTful API,允许...

    证通ZT598金属键盘开发资料.rar

    证通ZT598是一款专门用于金融、安防等领域,具备高安全性和可靠性的金属键盘设备。这款设备的开发资料包含在"证通ZT598金属键盘开发资料.rar"压缩包中,主要适用于那些进行嵌入式系统开发的工程师。在深入探讨其开发...

    zebra ZT400系列打印机技术手册

    ### zebra ZT400系列打印机技术手册 #### 知识点概述: 1. **版权及法律声明**:Zebra ZT400系列打印机技术手册的版权及相关软件固件的所有权归属ZIH Corp.及其许可证持有者,未经授权复制会受到法律制裁。 2. **...

    ZT210 230加载介质和碳带

    《ZT210 230加载介质和碳带》 在条形码和标签打印领域,Zebra公司的ZT210和ZT230打印机是广泛应用的设备,以其可靠性和效率赢得了广大用户的青睐。这两个型号的打印机都属于入门级工业级条码打印机,适用于各种商业...

    斑马zt410打印机驱动 v5.1.7 官方最新版

    斑马zt410驱动是由斑马官方推出的打印机驱动程序,如果你的打印机与电脑的连接出现了异常而导致打印机无法正常的使用,下载此驱动能帮你很好的解决这个问题,欢迎购买了此型号打印机的朋友下载使用!斑马zt410打印机...

    zt-exec-1.9-API文档-中英对照版.zip

    赠送jar包:zt-exec-1.9.jar; 赠送原API文档:zt-exec-1.9-javadoc.jar; 赠送源代码:zt-exec-1.9-sources.jar; 赠送Maven依赖信息文件:zt-exec-1.9.pom; 包含翻译后的API文档:zt-exec-1.9-javadoc-API文档-...

Global site tag (gtag.js) - Google Analytics