gpt4 book ai didi

java - 如何编码 SOAP 对 pojo 的响应?

转载 作者:行者123 更新时间:2023-12-01 18:38:05 25 4
gpt4 key购买 nike

我有一个简单的 Spring 集成项目,在其中连接到简单的肥皂服务 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>

这是通过输入 channel 发送消息的演示类:

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

关于java - 如何编码 SOAP 对 pojo 的响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20967242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com