Reputation: 1313
Lets say I have a list of objects like this: LinkedList<JsonAssessment> jsonAssessments...
.
It is returned to this kind of method:
@RequestMapping(value = "mapping", method = RequestMethod.POST)
public
@ResponseBody
List<JsonAssessment> doSomething(....) {
.....
}
I am making AJAX call to this controller everything is working correctly as expected but I don't like the naming my JSON is returned. In firebug I am seeing:
{"LinkedList":[{"assessmentName":"........
The question is how can I rename that root element LinkedList
? Is there any config I have to set?
EDIT
I do not want to use any wrapper objects.
EDIT
My ObjectMapper:
public class JsonObjectMapper extends ObjectMapper {
public JsonObjectMapper() {
super();
this.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
this.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
this.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
}
Spring: 3.2.4.RELEASE Jackson: 2.1.2
EDIT
This object mapper is declared in MVC message converters:
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="jsonObjectMapper"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
And that is what I've tried with naming strategy:
public class JsonPropertyNamingStrategy extends PropertyNamingStrategy {
public static final MyNamingStrategy MY_NAMING_STRATEGY = new MyNamingStrategy();
private static final LinkedHashMap<String, String> PROPERTIES = new LinkedHashMap<String, String>() {{
put("LinkedList", "resultList");
}};
public static class MyNamingStrategy extends PropertyNamingStrategyBase {
@Override
public String translate(String propertyName) {
if (propertyName == null) {
return null;
}
if (PROPERTIES.containsKey(propertyName)) {
return PROPERTIES.get(propertyName);
}
return propertyName;
}
}
}
I was debugging it and when it comes to translate method property names comes all except the root element. I have everything that LinkedList
contains, but LinkedList
is not coming to this method.
Upvotes: 3
Views: 11694
Reputation: 125232
Instead of directly returning a the List, wrap it inside a ResponseEntity
that will give you a response without a root element
@RequestMapping(value = "mapping", method = RequestMethod.POST)
public
@ResponseBody ResponseEntity<List<JsonAssessment>> doSomething(....) {
.....
return new ResponseEntity(yourList);
}
That way you don't have a root element. If you still want a root element you could add it to a Map
. Results in the following JSON "[{"assessmentName":"........]
@RequestMapping(value = "mapping", method = RequestMethod.POST)
public
@ResponseBody ResponseEntity<Map<String, List<JsonAssessment>>> doSomething(....) {
.....
Map results = new HashMap();
result.put("assesments", yourlist);
return new ResponseEntity(results);
}
Should output {"assesments":[{"assessmentName":"........
Although you are stilling wrapping the objects here, it is in objects that are freely available, you don't have to add your own custom classes.
This is what we are using in a couple of our @Controller
s, we are using Spring 3.2 and Jackson 2.2.
Upvotes: 3
Reputation: 337
If you are using Jackson Mapper you can use Annotations to define names of properties in classes by
@JsonProperty("foo")
or set the order by
@JsonPropertyOrder({"foo", "bar"})
and so on.
See further Jackson Annotations
edit: Sorry, just saw the wrapper-comment. The only solution i saw is using a wrapper like this: How to rename root key in JSON serialization with Jackson
Upvotes: 0
Reputation: 47300
put the list in a wrapper class is a a commonly implemented strategy
Upvotes: 0