EnverYigit
New Member
The situation is that a vendor supplied an XML schema for their XML documents that they can submit to my service. I didn't like their schema and so I wrote my own schema along with an XSLT to transform the received XML. My schema was used with JAXB's xjc tool to generate .java files that bind some pojos into a suitable object model. If it were not for the fact that a transformation step were needed, this would be trivial to implement in Spring MVC.The received XML must first be transformed before being mapped onto the JAXB classes. Roughly analogous to the following snippet:\[code\]@RequestMapping(value="http://stackoverflow.com/receiveXml", method=RequestMethod.POST )public ResponseEntity<String> receiveXml( @RequestBody String vendorXmlPayload ) { // 1. Make sure vendorXmlPayload adheres to vendor's schema vendorSchema.newValidator().validate(new StreamSource(new StringReader(vendorXmlPayload))); // 2. Transform xml payload to my schema StringWriter sw = new StringWriter(); transformer.transform(new StreamSource(new StringReader(vendorXmlPayload)), new StreamResult(sw)) // 3. Validate transformed XML against my schema mySchema.newValidator().validate(new StreamSource(new StringReader(sw.toString()))); // 4. Unmarshall to JAXB-annotated classes DomainObject obj = (DomainObject) unmarshaller.unmarshal(new StreamSource(new StringReader(sw.toString()))); (errors != null) ? return ... HttpStatus.BAD_REQUEST : return ..... HttpStatus.OK} \[/code\]Are there some elegant Spring annotations to condense all of that on the MVC Controller? Namely is there a way to perform the transform & unmarshal with the @RequestBody annotation or something? Perhaps like this fictitious snippet:\[code\]@RequestMapping(value="http://stackoverflow.com/receiveXml", method=RequestMethod.POST )@Transform(transformer="myTransform.xslt")public ResponseEntity<String> receiveXml( @RequestBody DomainObj domainObj) { // Process my DomainObj as I normally would (errors != null) ? return ... HttpStatus.BAD_REQUEST : return ..... HttpStatus.OK}\[/code\]@InitBinder doesn't quite look like it fits this scenario. Most "Spring MVC XSLT" searches deal with transforming output rather than input.