Skip to content
Advertisement

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:

JavaScript

and the Property Converter looks like this:

JavaScript

This code works for a Single Property. When I try to return a list of Properties:

JavaScript

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)

Advertisement

Answer

Does it work if you try adding the following method to your (single-property) converter?

JavaScript

The AbstractMessageConverterMethodProcessor class loops through all registered converters and skips any for which the canWrite method returns false. I’ve adapted the method above from the AbstractGenericHttpMessageConverter class, adding an extra check on the class of the value to be written, so that it should only be used to write out Property values.

Note that the single-property converter should be added to the list of converters before the multi-property converter. Alternatively, make a similar modification to your multi-property converter too.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement