我有一个简单的Spring集成项目,可在其中连接到简单的soap服务
http://www.w3schools.com/webservic/tempconvert.asmx转换温度。

这是定义肥皂服务链的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对象的响应?

如果有人可以发布一些代码示例。

最佳答案

尝试这个

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());
    }
}

09-10 06:57