2014-01-07 4 views
0

У меня есть простой проект интеграции Spring, в котором я подключаюсь к простому мыльному сервису 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()); 
    } 
} 
Смежные вопросы