Polish "Support JAXBElement in Jaxb2RootElementHttpMessageConverter"

See gh-33233
This commit is contained in:
Stéphane Nicoll 2024-07-19 10:13:13 +02:00
parent a5e2557738
commit f4b2886775
1 changed files with 9 additions and 7 deletions

View File

@ -121,7 +121,9 @@ public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessa
@Override
public boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType) {
return ((JAXBElement.class.isAssignableFrom(clazz) || AnnotationUtils.findAnnotation(clazz, XmlRootElement.class) != null) && canWrite(mediaType));
boolean supportedType = (JAXBElement.class.isAssignableFrom(clazz) ||
AnnotationUtils.findAnnotation(clazz, XmlRootElement.class) != null);
return (supportedType && canWrite(mediaType));
}
@Override
@ -190,12 +192,12 @@ public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessa
}
@Override
protected void writeToResult(Object o, HttpHeaders headers, Result result) throws Exception {
protected void writeToResult(Object value, HttpHeaders headers, Result result) throws Exception {
try {
Class<?> clazz = getMarshallerType(o);
Class<?> clazz = getMarshallerType(value);
Marshaller marshaller = createMarshaller(clazz);
setCharset(headers.getContentType(), marshaller);
marshaller.marshal(o, result);
marshaller.marshal(value, result);
}
catch (MarshalException ex) {
throw ex;
@ -205,12 +207,12 @@ public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessa
}
}
private static Class<?> getMarshallerType(Object o) {
if (o instanceof JAXBElement<?> jaxbElement) {
private static Class<?> getMarshallerType(Object value) {
if (value instanceof JAXBElement<?> jaxbElement) {
return jaxbElement.getDeclaredType();
}
else {
return ClassUtils.getUserClass(o);
return ClassUtils.getUserClass(value);
}
}