Reputation: 57
I have a simple Spring integration project in which I connect to simple soap service http://www.w3schools.com/webservic/tempconvert.asmx to convert temperature.
Here is xml that defines chain for soap service:
<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>
and here is demo class that sends message through input 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);
}
}
It works fine and I here is response:
<FahrenheitToCelsiusResponse xmlns="http://www.w3schools.com/webservices/"><FahrenheitToCelsiusResult>32.2222222222222</FahrenheitToCelsiusResult></FahrenheitToCelsiusResponse>
Now, how can I marshall that xml response to simple pojo object?
If someone could post some code example.
Upvotes: 0
Views: 5405
Reputation: 3788
I addition to Evgeniy answer, as far real ws endpoint on https://www.w3schools.com/xml/tempconvert.asmx returns XML result with xmlns attribute:
<FahrenheitToCelsiusResponse xmlns="https://www.w3schools.com/xml/">
<FahrenheitToCelsiusResult>...</FahrenheitToCelsiusResult>
</FahrenheitToCelsiusResponse>
I found that pojo should be modified (add namespace attribute to XmlElement annotation) as follows:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
class FahrenheitToCelsiusResponse {
@XmlElement(
name = "FahrenheitToCelsiusResult",
namespace = "https://www.w3schools.com/xml/" // <-- important!
)
private double result;
public double getResult() {
return result;
}
}
Regards
Upvotes: 0
Reputation: 136062
try this
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());
}
}
Upvotes: 4