I have the following SpringBootApplication
:
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@SpringBootApplication
@Configuration
public class RestServer implements WebMvcConfigurer {
public static void main(String[] args) {
SpringApplication.run(RestServer.class, args);
}
}
And I would like Guava's Multimap
get automatically serialized to JSON.
So I have this code in one of my classes, where all other fields are successfully serialized to JSON:
@JsonProperty("multimap_test")
public Multimap<Instrument, AdjustedLimitOrder> getMultimap() {
return someMultiMap;
}
But the returned JSON field looks like this:
"multimap_test": {
"empty": false
}
Do I have to specify a deserializer on the field?
Using this doesn't work because there is no default constructor:
@JsonProperty("multimap_test")
public Multimap<Instrument, AdjustedLimitOrder> getMultimap() {
return someMultimap;
}
Note that my example is implementing WebMvcConfigurer
.
Here is a list of links of instructions that didn't seem conclusive on how to do this, because they discuss the case when the SpringBootApplication
implements SpringBootServletInitializer
or WebMvcConfigurerAdapter
(which is deprecated).
- http://gdpotter.com/2017/05/24/custom-spring-mvc-jackson/
- Enable json serialization of Multimap in Spring Boot Project
How can Guava's Multimap
get automatically serialized?