What is the difference between Collections from casted from a HashMap over entryset() and casted ArrayList for Jackson?
I am developing a Spring Rest application. One of my methods is that:
@RequestMapping(method = RequestMethod.GET)
public @ResponseBody
Collection<Configuration> getConfigurationInJSON() {
Collection<Configuration> confList = new ArrayList<Configuration>();
...
I fill my confList and send it for GET request, it works. However when I want to keep that confList in a HashMap and send it after g开发者_如何学Goot it's entrySet as like that:
@RequestMapping(method = RequestMethod.GET)
public
@ResponseBody
Collection<Configuration> getAllConfigurationsInJSON() {
return configurationMap.values();
}
It gives me 406 error, so it means there is a wrong. What are the differences between that collections and why the second one is not same with first example?
For the sake of simplicity, can you just copy the values()
collection?
new ArrayList<Configuration>(configurationMap.values());
Only thing that comes to my mind is that Spring expects mutable collection, but don't really understand why. Hard to say without debugging, try enabling org.springframework.web
full logging.
The obvious difference is that configurationMap.values()
is a Set
.
You need to check if the JSON marshaller expects a List
to be returned and is not able to marshal Set
instances, as the marshaller will check the actual type of the returned value instead of the declared return type of the method, which is Collection
.
By the way, isn't there any clue in the logs about this ?
精彩评论