开发者

jaxb single element in array

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 ?


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?


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;
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜