Reputation: 11662
I'm using JAXB/Jersey (1.3) to convert java to json in a REST API. The java class I'm returning is like:
public class MyClass {
List<String> myTags;
public List<String> getMyTags() {
return myTags;
}
}
My problem is that if there is only a single element in the list myTags, then the data is converted to json as a string object, not an array of strings. That is, I get:
{
"myTags": "myString"
}
What I want is:
{
"myTags": ["myString"]
}
Anyone know whats up ?
Upvotes: 16
Views: 11745
Reputation: 64
I was facing the similar issue and found simple fix. Marking @JsonSerialize instead of @XmlRootElement has worked for me.
@JsonSerialize
public class MyClass {
List<String> myTags;
public List<String> getMyTags() {
return myTags;
}
}
Upvotes: -1
Reputation: 11662
As per Luciano's comments, the problem lies in the fact that Jersey wasn't using Jackson as the default JSON converter. I tried excluding Jettison from the pom dependency, but it still didn't resolve the issue. I found an answer to explicitly tell Jersey to use Jackson here:
How can I customize serialization of a list of JAXB objects to JSON?
Upvotes: 7