4

So I need help in using java eclipse collections as part of the response using the spring boot Response Entity JSON . I have tried using the generic way but I get a response exception error that it failed to convert to java ArrayList type so can anyone provide an example for a normal rest endpoint that uses eclipse collections data instead of java collections list?

here is an example code

 @GetMapping("/list")
public ResponseEntity<MutableList<Person>> getData() {

    return ResponseEntity.ok(Map.of(
                    "success", true,
                    
                    "data", Map.of(
                            "users", personService.getUsers()
                    )
            ));

}
kinsley kajiva
  • 1,840
  • 1
  • 21
  • 26

2 Answers2

5

Instructions for enabling eclipse-collections Jackson module that adds Json serialization support for Eclipse Collection Types:

By default Spring MVC MappingJackson2HttpMessageConverter will create its own ObjectMapper with default options using Jackson2ObjectMapperBuilder. As per Spring Boot docs 76.3 Customize the Jackson ObjectMapper chapter:

Any beans of type com.fasterxml.jackson.databind.Module are automatically registered with the auto-configured Jackson2ObjectMapperBuilder and are applied to any ObjectMapper instances that it creates. This provides a global mechanism for contributing custom modules when you add new features to your application.

so it should be enough to register eclipse collections module as a bean:

@Bean
public EclipseCollectionsModule eclipseCollectionsModule() 
{
    return new EclipseCollectionsModule();
}
0

Replace your return expression with the following code:

return ResponseEntity.ok(personService.getUsers().stream().collect(Collectors2.toList()));
qqNade
  • 1,942
  • 8
  • 10