@Override public void afterPropertiesSet() throws Exception { boolean hasContextPath = StringUtils.hasLength(this.contextPath); boolean hasClassesToBeBound = !ObjectUtils.isEmpty(this.classesToBeBound); boolean hasPackagesToScan = !ObjectUtils.isEmpty(this.packagesToScan); if (hasContextPath && (hasClassesToBeBound || hasPackagesToScan) || (hasClassesToBeBound && hasPackagesToScan)) { throw new IllegalArgumentException("Specify either 'contextPath', 'classesToBeBound', " + "or 'packagesToScan'"); } if (!hasContextPath && !hasClassesToBeBound && !hasPackagesToScan) { throw new IllegalArgumentException( "Setting either 'contextPath', 'classesToBeBound', " + "or 'packagesToScan' is required"); } if (!this.lazyInit) { getJaxbContext(); } if (!ObjectUtils.isEmpty(this.schemaResources)) { this.schema = loadSchema(this.schemaResources, this.schemaLanguage); } }
/** * Return a newly created JAXB marshaller. JAXB marshallers are not necessarily thread safe. */ protected Marshaller createMarshaller() { try { Marshaller marshaller = getJaxbContext().createMarshaller(); initJaxbMarshaller(marshaller); return marshaller; } catch (JAXBException ex) { throw convertJaxbException(ex); } }
/** * Return a newly created JAXB unmarshaller. * Note: JAXB unmarshallers are not necessarily thread-safe. */ protected Unmarshaller createUnmarshaller() { try { Unmarshaller unmarshaller = getJaxbContext().createUnmarshaller(); initJaxbUnmarshaller(unmarshaller); return unmarshaller; } catch (JAXBException ex) { throw convertJaxbException(ex); } }
/** * unmarshal. * * @param marshaller marshaller * @param xml xml * @param type type * @param <T> type * @return object */ @SneakyThrows public static <T> T unmarshal(final Jaxb2Marshaller marshaller, final String xml, final Class<T> type) { final JAXBElement<T> element = marshaller.getJaxbContext().createUnmarshaller() .unmarshal(new StreamSource(new StringReader(xml)), type); return element.getValue(); }