HttpMessageConverter for Single Object and List of Object
I have an Object (here: Property) and I want to add csv export ability to my Spring Backend for single and list of objects.
I added this to my config:
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.(more config).mediaType("csv", new MediaType("text", "csv"));
}
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new PropertyConverter());
converters.add(new StatsConverter());
}
and the Property Converter looks like this:
public class PropertyConverter extends AbstractGenericHttpMessageConverter<Property> {
private static final Logger LOGGER = LoggerFactory.getLogger(PropertyConverter.class);
public PropertyConverter() {
super(new MediaType("text", "csv"));
}
@Override
protected void writeInternal(Property property, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
try (var writer = new OutputStreamWriter(outputMessage.getBody())) {
new StatefulBeanToCsvBuilder<>(writer).withSeparator(',').build().write(property);
} catch (CsvDataTypeMismatchException | CsvRequiredFieldEmptyException ex) {
LOGGER.error("CSV failed to convert property: ".concat(property.getExternalId()).concat(", exception: ".concat(ex.toString())));
}
}
@Override
protected Property readInternal(Class<? extends Property> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
return null;
}
@Override
public Property read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
return null;
}
}
This code works for a Single Property. When I try to return a list of Properties:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class eu.webeng.model.Property (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; eu.webeng.model.Property is in unnamed module of loader 'app')
...
Caused by: java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class eu.webeng.model.Property (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; eu.webeng.model.Property is in unnamed module of loader 'app')
at eu.webeng.converter.PropertyConverter.writeInternal(PropertyConverter.java:20) ~[classes/:na]
at org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:104) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:287) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:219) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:82) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
....
I tried to add a PropertyListConverter but then it doesn't work for Single Property. When I tried to add both, the first converter added is being used.
How can I make the Converter work for Single and List of Property (or any Object)
from Recent Questions - Stack Overflow https://ift.tt/33I31zN
https://ift.tt/eA8V8J
Comments
Post a Comment