Announcement Announcement Module
Collapse
No announcement yet.
WebServiceTemplate returns null Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • WebServiceTemplate returns null

    I'm rather new to Spring and started using Spring-WS, thus I might be missing some basic Spring knowledge, but here goes.

    I'm create a simple webservice, which works like a charm, but when it comes to the client I'm in trouble.

    Code is always nice:
    PHP Code:
    ...
    public 
    CustomerInfo getCustomer(String id) {
        
    Document doc createRequest(id);
                    
        
    JDOMSource reqSource = new JDOMSource(doc);
        
    JDOMResult result = new JDOMResult();
        
    webServiceTemplate.sendSourceAndReceiveToResult(reqSourceresult);
        ...
    }

    ...

    private 
    WebServiceTemplate webServiceTemplate;
    public 
    void setWebServiceTemplate(WebServiceTemplate webServiceTemplate) {    
        
    this.webServiceTemplate webServiceTemplate;

    Debugging gave me that the setWebServiceTemplate method is called during startup and is not null, but for some reason webServiceTemplate.sendSourceAndReceiveToResult(re qSource, result) results in a NullPointerException.

    I'm somewhat lost here, any hints or clues as to where I should go from here?

  • #2
    Oh, forgot - using a main method to simply test it:
    PHP Code:
    ...
    TemplateBasedSalesOverviewClient t = new TemplateBasedSalesOverviewClient();
    System.out.println(t.getCustomer("test"));
    ... 

    Comment


    • #3
      If I don't use the webservicetemplate but instead uses the wsdl it works like a charm...
      PHP Code:
          public static void main(String[] args) throws Exception{

              String xmlRequest = 
                  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                  "<GetCustomerRequest xmlns=\"http://www.example.com/so/schemas\">" +
                      "<cpr>123</cpr>" +
                  "</GetCustomerRequest>";

              String wsdlUrl = "http://localhost/so-ws/services/so.wsdl";

              StreamSource requestMessage = new StreamSource(new StringReader(xmlRequest));
              StreamResult responseMessage = new StreamResult(System.out);
              
              WebServiceTemplate template = new WebServiceTemplate();
              template.sendSourceAndReceiveToResult(wsdlUrl, requestMessage, responseMessage);        
          }
      Now, why doesn't the webservicetemplate work? Am I missing something. This is my spring conf:
      PHP Code:
          <bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
              <
      property name="messageFactory">
                  <
      bean class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory"/>
              </
      property>
              <
      property name="defaultUri" value="http://localhost/so-ws/services"/>
              <
      property name="messageSender" ref="messageSender"/>
          </
      bean>
          
          <
      bean id="templateBasedClient" class="so.ws.client.TemplateBasedSalesOverviewClient">
              <
      property name="webServiceTemplate" ref="webServiceTemplate"/>
          </
      bean>
          
          <
      bean id="messageSender" class="org.springframework.ws.transport.http.HttpUrlConnectionMessageSender"/> 

      Comment


      • #4
        Fixed the problem by adding a class that can load the config using a singleton:
        PHP Code:
        import java.io.File;

        import org.springframework.context.ApplicationContext;
        import org.springframework.context.support.FileSystemXmlApplicationContext;


        public class 
        SpringContext {
            private final static 
        ApplicationContext context = new FileSystemXmlApplicationContext();

            static {
                
        FileSystemXmlApplicationContext ctx = (FileSystemXmlApplicationContextcontext;
                
        ctx.setConfigLocation("file:" getConfigRoot() + "spring" File.separatorChar "applicationContext.xml");
                
        ctx.refresh();
            }
            
            public static 
        Object getBean(String name) {
                return 
        context.getBean(name);
            }
            
            
            private static 
        String configRoot null;
            private static 
        String getConfigRoot() {
                if (
        configRoot == null) {
                    
        configRoot System.getProperty("config.root"System.getProperty("java.io.tmpdir") + "\\remove.me");
                    
        configRoot configRoot.substring(0configRoot.lastIndexOf(File.separatorChar));
                    
        configRoot += File.separator;
                }
                return 
        configRoot;
            }

        Comment

        Working...
        X