XmlRootElementPayloadMethodProcessor extends AbstractJaxb2PayloadMethodProcessor { @Override public Object resolveArgument(MessageContext messageContext, MethodParameter parameter) throws JAXBException { Class<?> parameterType = parameter.getParameterType(); if (parameterType.isAnnotationPresent(XmlRootElement.class)) { return unmarshalFromRequestPayload(messageContext, parameterType); } else { JAXBElement<?> element = unmarshalElementFromRequestPayload(messageContext, parameterType); return element != null ? element.getValue() : null; } }  @Override Object resolveArgument(MessageContext messageContext, MethodParameter parameter);  }
@Test public void resolveArgumentRootElement() throws JAXBException { WebServiceMessage request = new MockWebServiceMessage( "<root xmlns='http: MessageContext messageContext = new DefaultMessageContext(request, new MockWebServiceMessageFactory()); Object result = processor.resolveArgument(messageContext, rootElementParameter); assertTrue("result not a MyRootElement", result instanceof MyRootElement); MyRootElement rootElement = (MyRootElement) result; assertEquals("invalid result", "Foo", rootElement.getString()); }
@Test public void resolveArgumentType() throws JAXBException { WebServiceMessage request = new MockWebServiceMessage( "<type xmlns='http: MessageContext messageContext = new DefaultMessageContext(request, new MockWebServiceMessageFactory()); Object result = processor.resolveArgument(messageContext, typeParameter); assertTrue("result not a MyType", result instanceof MyType); MyType type = (MyType) result; assertEquals("invalid result", "Foo", type.getString()); }
@Test public void resolveArgumentFromCustomSAXSource() throws JAXBException { XMLReader xmlReader = new AbstractXmlReader() { @Override public void parse(String systemId) throws IOException, SAXException { parse(); } @Override public void parse(InputSource input) throws IOException, SAXException { parse(); } private void parse() throws SAXException { ContentHandler handler = getContentHandler(); handler.startDocument(); handler.startPrefixMapping("", "http: handler.startElement("http: handler.startElement("http: handler.characters("Foo".toCharArray(), 0, 3); handler.endElement("http: handler.endElement("http: handler.endPrefixMapping(""); handler.endDocument(); } }; final SAXSource source = new SAXSource(xmlReader, new InputSource()); WebServiceMessage request = new WebServiceMessage() { @Override public void writeTo(OutputStream outputStream) throws IOException { throw new UnsupportedOperationException(); } @Override public Source getPayloadSource() { return source; } @Override public Result getPayloadResult() { throw new UnsupportedOperationException(); } }; MessageContext messageContext = new DefaultMessageContext(request, new MockWebServiceMessageFactory()); Object result = processor.resolveArgument(messageContext, rootElementParameter); assertTrue("result not a MyRootElement", result instanceof MyRootElement); MyRootElement rootElement = (MyRootElement) result; assertEquals("invalid result", "Foo", rootElement.getString()); }