栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Spring Web Service客户端教程或所需示例

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Web Service客户端教程或所需示例

在我之前的项目中,我用Spring 2.5.6,maven2,xmlbeans实现了一个Web服务客户端。

  • xmlbeans负责un / marshal
  • maven2用于项目mgmt / building等。

我在此处粘贴一些代码,希望对您有所帮助。

xmlbeans maven插件conf :(在pom.xml中)

<build>        <finalName>projectname</finalName>        <resources>        <resource> <directory>src/main/resources</directory> <filtering>true</filtering>        </resource>        <resource> <directory>target/generated-classes/xmlbeans </directory>        </resource>    </resources>        <!-- xmlbeans maven plugin for the client side -->        <plugin> <groupId>org.prehaus.mojo</groupId> <artifactId>xmlbeans-maven-plugin</artifactId> <version>2.3.2</version> <executions>     <execution>         <goals>  <goal>xmlbeans</goal>         </goals>     </execution> </executions> <inherited>true</inherited> <configuration>     <schemaDirectory>src/main/resources/</schemaDirectory> </configuration>        </plugin><plugin> <groupId>org.prehaus.mojo</groupId> <artifactId>build-helper-maven-plugin </artifactId> <version>1.1</version> <executions>     <execution>         <id>add-source</id>         <phase>generate-sources</phase>         <goals>  <goal>add-source</goal>         </goals>         <configuration>  <sources>      <source> target/generated-sources/xmlbeans</source>  </sources>         </configuration>     </execution> </executions>        </plugin>    </plugins></build>

因此,从上述配置文件中,您需要将模式文件(独立的或在WSDL文件中,需要将其提取并另存为模式文件。)放在src / main /
resources下。当您使用Maven构建项目时,pojos将由xmlbeans生成。生成的源代码将位于target / generated-
sources / xmlbeans下。

然后我们来参加Spring conf。我只是将WS相关上下文放在这里:

    <bean id="messageFactory" >        <property name="payloadCaching" value="true"/>    </bean>    <bean id="abstractClient" abstract="true">        <constructor-arg ref="messageFactory"/>    </bean>    <bean id="marshaller" /> <bean id="myWebServiceClient" parent="abstractClient" >        <property name="defaultUri" value="http://your.webservice.url"/>        <property name="marshaller" ref="marshaller"/>        <property name="unmarshaller" ref="marshaller"/>    </bean>

最后,看看ws-client java类

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

}

我希望示例代码对您有所帮助。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/454251.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号