2014-01-07 135 views
0

我有一個簡單的彈簧集成項目,其中我連接到簡單的肥皂服務 http://www.w3schools.com/webservic/tempconvert.asmx來轉換溫度。如何將肥皂響應pojo?

下面是XML定義鏈皁服務:

<beans:beans 
     ... 
     <chain input-channel="fahrenheitChannel" output-channel="celsiusChannel"> 
      <ws:header-enricher> 
       <ws:soap-action value="http://www.w3schools.com/webservices/FahrenheitToCelsius"/> 
      </ws:header-enricher> 
      <ws:outbound-gateway uri="http://www.w3schools.com/webservices/tempconvert.asmx"/> 
     </chain> 

     <!-- The response from the service is logged to the console. --> 
     <stream:stdout-channel-adapter id="celsiusChannel"/> 

    </beans:beans> 

,這裏是演示類,通過輸入信道發送消息:

public class WebServiceDemoTestApp { 

    public static void main(String[] args) { 
     ClassPathXmlApplicationContext context = 
      new ClassPathXmlApplicationContext("/META-INF/spring/integration/temperatureConversion.xml"); 
     ChannelResolver channelResolver = new BeanFactoryChannelResolver(context); 

     // Compose the XML message according to the server's schema 
     String requestXml = 
       "<FahrenheitToCelsius xmlns=\"http://www.w3schools.com/webservices/\">" + 
       " <Fahrenheit>90.0</Fahrenheit>" + 
       "</FahrenheitToCelsius>"; 

     // Create the Message object 
     Message<String> message = MessageBuilder.withPayload(requestXml).build(); 

     // Send the Message to the handler's input channel 
     MessageChannel channel = channelResolver.resolveChannelName("fahrenheitChannel"); 
     channel.send(message); 
    } 

} 

它工作正常,我在這裏是響應:

<FahrenheitToCelsiusResponse xmlns="http://www.w3schools.com/webservices/"><FahrenheitToCelsiusResult>32.2222222222222</FahrenheitToCelsiusResult></FahrenheitToCelsiusResponse> 

現在,我該如何編寫XML簡單pojo對象的響應呢?

如果有人可以發佈一些代碼示例。

+0

http://blog.bdoughan.com/2012/08/handle-middle-of-xml-document-with-jaxb.html - FYR ... –

回答

3

試試這個

class FahrenheitToCelsiusResponse { 
    @XmlElement(name = "FahrenheitToCelsiusResult") 
    private double result; 

    public double getResult() { 
     return result; 
    } 
} 

public class X { 

    public static void main(String[] args) throws Exception { 
     String s = "<FahrenheitToCelsiusResponse><FahrenheitToCelsiusResult>32.2222222222222</FahrenheitToCelsiusResult></FahrenheitToCelsiusResponse>"; 
     FahrenheitToCelsiusResponse res = JAXB.unmarshal(new StringReader(s), FahrenheitToCelsiusResponse.class); 
     System.out.println(res.getResult()); 
    } 
}