'How to use Eclipse collection in Spring boot ResponseEnity in Java
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()
)
));
}
Solution 1:[1]
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();
}
Solution 2:[2]
Replace your return expression with the following code:
return ResponseEntity.ok(personService.getUsers().stream().collect(Collectors2.toList()));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Alex Goldberg |
Solution 2 | qqNade |