I am using spring-ws for a web service and can’t seem to figure out how to return different responses for the same initial request. Lets just say that I get a certain type of request, we will call it FindGasRequest. Now I already have the endpoint mapping set up properly so that when a FindGasRequest comes in it maps to the correct endpoint and then I perform some business logic and then return a FindGasResponse.
Here’s my question. Say I get a FindGasRequest and then in my business logic I determine that instead of returning back a FindGasResponse I want to return a ListGasStationsResponse. How would I do this if my method marked by the @PayloadRoot is expecting a FindGasResponse? I guess how can I change the payload to be something else?
I do have error checking set up so that I can throw an exception and it will return a FindGasErrorResponse through and exception handler. So my first thought was to do something similar but it seems rather hacky to throw a exception when no error occurred.
sauce,
Assuming your message schema and contract with the client allows returning a ListGasStationsResponse or FindGasResponse in response to a FindGasRequest, you’ll need to give up the annotation-based FindGas endpoint, and instead make your endpoint extend AbstractMarshallingPayloadEndpoint.
You should then override AbstractMarshallingPayloadEndpoint.invokeInternal to return one of the two different response types as an Object. Your marshaller (this works for me with the Jaxb2Marshaller) should then automatically be able to take that Object and create the XML response for one of those types.
Note that you may then have a mix of annotation-based and xml-configured endpoints, in which case you should follow the instructions here to set up mappings for them: http://forum.springsource.org/showthread.php?78685-Mixing-annotations-with-xml-configuration-of-endpoints.
This approach has worked for me in Spring-WS 1.5.