Spring Web Service Client Tutorial or Example Required

后端 未结 2 1208
[愿得一人]
[愿得一人] 2020-12-25 10:03

I need to jump into the Spring Web Service Project, in that I required to implement the Spring Web Service\'s Client Only..

So, I have already gone through with Spri

2条回答
  •  猫巷女王i
    2020-12-25 10:58

    in my previous project, I implemented a Webservice client with Spring 2.5.6, maven2, xmlbeans.

    • xmlbeans is responsible for un/marshal
    • maven2 is for project mgmt/building etc.

    I paste some codes here and hope they are helpful.

    xmlbeans maven plugin conf: (in pom.xml)

    
            projectname
    
            
    
            
    
                src/main/resources
    
                true
    
            
    
            
    
                target/generated-classes/xmlbeans
    
                
    
            
    
        
    
    
            
    
            
    
                org.codehaus.mojo
    
                xmlbeans-maven-plugin
    
                2.3.2
    
                
    
                    
    
                        
    
                            xmlbeans
    
                        
    
                    
    
                
    
                true
    
                
    
                    src/main/resources/
    
                
    
            
    
    
                org.codehaus.mojo
    
                build-helper-maven-plugin
    
                
    
                1.1
    
                
    
                    
    
                        add-source
    
                        generate-sources
    
                        
    
                            add-source
    
                        
    
                        
    
                            
    
                                 target/generated-sources/xmlbeans
    
                            
    
                        
    
                    
    
    
    
                
    
            
        
    
    

    So from the above conf, you need to put the schema file (either standalone or in your WSDL file, you need to extract them and save as a schema file.) under src/main/resources. when you build the project with maven, the pojos are gonna be generated by xmlbeans. The generated sourcecodes will be under target/generated-sources/xmlbeans.

    then we come to Spring conf. I just put the WS relevant context here:

        
    
            
    
        
    
    
        
            
        
    
        
    
     
    
            
    
            
    
            
    
        
    

    finally, take a look the ws-client java class

    public class MyWsClient extends WebServiceGatewaySupport {
     //if you need some Dao, Services, just @Autowired here.
    
        public MyWsClient(WebServiceMessageFactory messageFactory) {
            super(messageFactory);
        }
    
        // here is the operation defined in your wsdl
        public Object someOperation(Object parameter){
    
          //instantiate the xmlbeans generated class, infact, the instance would be the document (marshaled) you are gonna send to the WS
    
          SomePojo requestDoc = SomePojo.Factory.newInstance(); // the factory and other methods are prepared by xmlbeans
          ResponsePojo responseDoc = (ResponsePojo)getWebServiceTemplate().marshalSendAndReceive(requestDoc); // here invoking the WS
    
    
    //then you can get the returned object from the responseDoc.
    
       }
    

    }

    I hope the example codes are helpful.

提交回复
热议问题