如何策划肥皂对pojo的反应?

时间:2014-01-07 08:43:26

标签: java spring web-services soap-client

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

这是定义soap服务链的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>

这里是通过输入通道发送消息的demo类:

 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对象?

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

2 个答案:

答案 0 :(得分: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());
    }
}

答案 1 :(得分:0)

除了Evgeniy答案外,https://www.w3schools.com/xml/tempconvert.asmx上的实际ws端点还返回带有xmlns属性的XML结果:

<FahrenheitToCelsiusResponse xmlns="https://www.w3schools.com/xml/">
  <FahrenheitToCelsiusResult>...</FahrenheitToCelsiusResult>
</FahrenheitToCelsiusResponse>

我发现应该修改pojo(将名称空间属性添加到XmlElement批注中),如下所示:

  @XmlRootElement
  @XmlAccessorType(XmlAccessType.FIELD)
  class FahrenheitToCelsiusResponse {

    @XmlElement(
        name = "FahrenheitToCelsiusResult",
        namespace = "https://www.w3schools.com/xml/" // <-- important!
    )
    private double result;

    public double getResult() {
        return result;
    }
}

致谢