spring-ws中使用xmlbeans记录

选择用spring-ws框架+xmlbeans.


以查询股票的webservice为例子

http://www.webxml.com.cn/WebServices/ChinaStockWebService.asmx?wsdl


1.官网下载xmlbeans ,在bin目录使用以下命令

sudo ./scomp -out /tmp/chinaStock.jar /tmp/ChinaStockWebService.wsdl

p.s. 第一次使用了
sudo ./scomp -srconly -src /tmp/src /tmp/ChinaStockWebService.wsdl
命令,结果生成的java文件不包含很多的命名空间。浪费了个把小时。



2. 将chinaStock.jar 放到项目lib下, 在spring配置文件里增加如下内容:

	<!--stock webservice configuration -->

	<bean id="chinaStockWebserviceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
		<property name="defaultUri"
			value="http://www.webxml.com.cn/WebServices/ChinaStockWebService.asmx" />
		<property name="marshaller" ref="marshaller" />
		<property name="unmarshaller" ref="marshaller" />
	</bean>

	<bean
		class="org.springframework.ws.server.endpoint.adapter.GenericMarshallingMethodEndpointAdapter">
		<property name="marshaller" ref="marshaller" />
		<property name="unmarshaller" ref="marshaller" />
	</bean>

	<bean id="marshaller" class="org.springframework.oxm.xmlbeans.XmlBeansMarshaller">
	</bean>

2.1 或者放入maven repository:

 mvn install:install-file -Dfile=/tmp/chinaStock.jar -DgroupId=cn.webxml -DartifactId=chinaStock -Dversion=1.0.0 -Dpackaging=jar

2.2 pom.xml

               <!-- chinastock.jar-->
                <dependency>
                    <groupId>cn.webxml</groupId>
                    <artifactId>chinaStock</artifactId>
                    <version>1.0.0</version>
                </dependency>




3.配置log4j.properties,以便打印所有的soap message:

log4j.logger.org.springframework.ws.client.MessageTracing.sent=TRACE
log4j.logger.org.springframework.ws.client.MessageTracing.received=DEBUG

4. 写个测试类,调用 getStockInfoByCode方法:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"file:/home/***/src/main/resources/springmvc-webserviceTest.xml"})
public class ChinaStockWebServiceTest {
	
	@Autowired
    private WebServiceTemplate chinaStockWebserviceTemplate;
	
	@Test
	public void testGetStockDetailList(){
		System.out.println("22");
		GetStockInfoByCodeDocument request = GetStockInfoByCodeDocument.Factory.newInstance();
		request.addNewGetStockInfoByCode().setTheStockCode("sh600270");
		GetStockInfoByCodeResponseDocument response = (GetStockInfoByCodeResponseDocument)chinaStockWebserviceTemplate.marshalSendAndReceive(request,new WebServiceMessageCallback() {
		    public void doWithMessage(WebServiceMessage message) {
		        ((SoapMessage)message).setSoapAction("http://WebXml.com.cn/getStockInfoByCode");
		    }
		});
		System.out.println(response);
		System.out.println("good");
	}

}



这里有个很重要的 webServiceMessageCallback(), 如果没有callback,就没法设置soapAction, webservice调用会出错(我卡在这里2小时). google了一篇文章,说的很详细.

点击打开链接

然后run,顺利通过,控制台输出:

22
2014-01-26 17:20:01,949 TRACE [org.springframework.ws.client.MessageTracing.sent] - Sent request [<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><web:getStockInfoByCode xmlns:web="http://WebXml.com.cn/"><web:theStockCode>sh600270</web:theStockCode></web:getStockInfoByCode></SOAP-ENV:Body></SOAP-ENV:Envelope>]
2014-01-26 17:20:02,667 DEBUG [org.springframework.ws.client.MessageTracing.received] - Received response [SaajSoapMessage {http://WebXml.com.cn/}getStockInfoByCodeResponse] for request [SaajSoapMessage {http://WebXml.com.cn/}getStockInfoByCode]
<getStockInfoByCodeResponse xmlns="http://WebXml.com.cn/">
  <getStockInfoByCodeResult>
    <string>sh600270</string>
    <string>外运发展</string>
    <string>2014-01-24 15:04:08</string>
    <string>9.68</string>
    <string>9.70</string>
    <string>9.67</string>
    <string>-0.02</string>
    <string>9.60</string>
    <string>9.82</string>
    <string>-0.21%</string>
    <string>58789.12</string>
    <string>5721.6886</string>
    <string>9.67</string>
    <string>9.68</string>
    <string>-32.01%</string>
    <string>9.67 / 67.06</string>
    <string>9.66 / 218.00</string>
    <string>9.65 / 269.00</string>
    <string>9.64 / 177.00</string>
    <string>9.63 / 108.00</string>
    <string>9.68 / 9.00</string>
    <string>9.69 / 39.00</string>
    <string>9.70 / 719.00</string>
    <string>9.71 / 129.00</string>
    <string>9.72 / 733.00</string>
  </getStockInfoByCodeResult>
</getStockInfoByCodeResponse>
good



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值