使用spring发送soap请求并定义xml

时间:2014-08-08 22:25:02

标签: java spring soap soap-client

我正在尝试使用spring连接到服务以运行定义为

的soap操作
https://services.domain.getuserbyid

这是我的代码,但它告诉我前缀" tem"不受约束。假设我的webServiceTemplate正在加载,任何想法我如何正确定义这个前缀所以我可以发送这个肥皂消息?如果需要更多信息,请告诉我。

import java.io.StringReader;

import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.client.core.WebServiceMessageCallback;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.soap.SoapMessage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.*;

@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class })
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"spring-ws.xml"})
public class TestWebServiceClient {

    private static final String ENVELOPE_PREFIX = "soap";
    private static final String MESSAGE =  
                "<tem:GetUserByID> " + 
                    "<tem:samAccountName>name</tem:samAccountName> " + 
                 "</tem:GetUserByID> " ; 

    @Autowired
    private WebServiceTemplate webServiceTemplate;

    @Test
    // send to the configured default URI
    public void simpleSendAndReceive() {
        StreamSource source = new StreamSource(new StringReader(MESSAGE));
        StreamResult result = new StreamResult(System.out);


        //webServiceTemplate.sendSourceAndReceiveToResult(source, result);

        webServiceTemplate.marshalSendAndReceive(webServiceTemplate.sendSourceAndReceiveToResult(source, result), new WebServiceMessageCallback() {  
                public void doWithMessage(WebServiceMessage message) {

                ((SoapMessage)message).setSoapAction("https://services.domain.getuserbyid");
            }});
    }

    public void setWebServiceTemplate(WebServiceTemplate webServiceTemplate) {
        this.webServiceTemplate = webServiceTemplate;
    }

    public WebServiceTemplate getWebServiceTemplate() {
        return this.webServiceTemplate;
    }
}

我的春季应用上下文文件:

<?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:oxm="http://www.springframework.org/schema/oxm"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
  xmlns:mongo="http://www.springframework.org/schema/data/mongo"
   xmlns:util="http://www.springframework.org/schema/util"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/data/mongo
    http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
    http://www.directwebremoting.org/schema/spring-dwr
    http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/oxm 
    http://www.springframework.org/schema/oxm/spring-oxm.xsd">

    <context:component-scan base-package="com.domain"/>
    <context:annotation-config />  

    <bean id="soapMessageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
        <property name="soapVersion">
            <util:constant static-field="org.springframework.ws.soap.SoapVersion.SOAP_12"/>
        </property>
    </bean>

    <bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
        <constructor-arg ref="soapMessageFactory"/>
        <property name="defaultUri" value="https://domain.com/Service.svc?WSDL"/>
    </bean>      

</beans>

1 个答案:

答案 0 :(得分:2)

想出来,需要将它添加到我的XML

<tem:GetUserByID xmlns:tem="http://domain/GetUserByIDResponse/">
      <tem:samAccountName>name</tem:samAccountName>
</tem:GetUserByID>