Skip to content

Commit f4b2886

Browse files
committed
Polish "Support JAXBElement in Jaxb2RootElementHttpMessageConverter"
See gh-33233
1 parent a5e2557 commit f4b2886

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

spring-web/src/main/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverter.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ public boolean canRead(Class<?> clazz, @Nullable MediaType mediaType) {
121121

122122
@Override
123123
public boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType) {
124-
return ((JAXBElement.class.isAssignableFrom(clazz) || AnnotationUtils.findAnnotation(clazz, XmlRootElement.class) != null) && canWrite(mediaType));
124+
boolean supportedType = (JAXBElement.class.isAssignableFrom(clazz) ||
125+
AnnotationUtils.findAnnotation(clazz, XmlRootElement.class) != null);
126+
return (supportedType && canWrite(mediaType));
125127
}
126128

127129
@Override
@@ -190,12 +192,12 @@ protected Source processSource(Source source) {
190192
}
191193

192194
@Override
193-
protected void writeToResult(Object o, HttpHeaders headers, Result result) throws Exception {
195+
protected void writeToResult(Object value, HttpHeaders headers, Result result) throws Exception {
194196
try {
195-
Class<?> clazz = getMarshallerType(o);
197+
Class<?> clazz = getMarshallerType(value);
196198
Marshaller marshaller = createMarshaller(clazz);
197199
setCharset(headers.getContentType(), marshaller);
198-
marshaller.marshal(o, result);
200+
marshaller.marshal(value, result);
199201
}
200202
catch (MarshalException ex) {
201203
throw ex;
@@ -205,12 +207,12 @@ protected void writeToResult(Object o, HttpHeaders headers, Result result) throw
205207
}
206208
}
207209

208-
private static Class<?> getMarshallerType(Object o) {
209-
if (o instanceof JAXBElement<?> jaxbElement) {
210+
private static Class<?> getMarshallerType(Object value) {
211+
if (value instanceof JAXBElement<?> jaxbElement) {
210212
return jaxbElement.getDeclaredType();
211213
}
212214
else {
213-
return ClassUtils.getUserClass(o);
215+
return ClassUtils.getUserClass(value);
214216
}
215217
}
216218

0 commit comments

Comments
 (0)