Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to unmarshall to different @RequestBody object types?
    primarykey
    data
    text
    <p>I'm using Spring in my web service which receives XML as input. It can be XML embebed in the HTTP request or as a plain text in the <em>request</em> attribute.</p> <p>Currently my web service is handling two different XML schemas so my unmarshaller can unmarshall the XML files to two object types (for example: Foo and Bar).</p> <p>In my Controller, I have the next code to handler the <em>request</em> attribute:</p> <pre><code>@RequestMapping(value={"/mypath"}, method={RequestMethod.POST}, headers={"content-type=application/x-www-form-urlencoded"}) @ResponseBody public ResponseObject getResponse(@RequestParam("request") String request, HttpServletRequest req) { </code></pre> <p>It works perfectly, with the request string I can unmarshall to Foo object or Bar object.</p> <p>The problem comes with the XML embebed:</p> <pre><code>@RequestMapping(value={"/mypath"}, method={RequestMethod.POST}, headers={"content-type=text/xml"}) @ResponseBody public ResponseObject getResponse(@RequestBody Foo request, HttpServletRequest req) { </code></pre> <p>and</p> <pre><code>@RequestMapping(value={"/mypath"}, method={RequestMethod.POST}, headers={"content-type=text/xml"}) @ResponseBody public ResponseObject getResponse(@RequestBody Bar request, HttpServletRequest req) { </code></pre> <p>and here is the MessageConverter:</p> <pre><code>&lt;bean id="marshallingHttpMessageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"&gt; &lt;property name="marshaller" ref="jaxb2Marshaller" /&gt; &lt;property name="unmarshaller" ref="jaxb2Marshaller" /&gt; &lt;/bean&gt; &lt;oxm:jaxb2-marshaller id="jaxb2Marshaller" contextPath="path.to.Foo:path.to.Bar"/&gt; </code></pre> <p>I think that the MessageConverter should do the unmarshall automagically but I receive the next error:</p> <blockquote> <p>java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path '/ws/mypath.ws': [...] If you intend to handle the same path in multiple methods, then factor them out into a dedicated handler class with that path mapped at the type level!</p> </blockquote> <p>How can I unmarshall automatically to different <code>@RequestBody</code> object types? (with the same <em>web service path</em>)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload