Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jaxb single element in array

Tags:

jaxb

jersey

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 ?

like image 878
Kevin Avatar asked Apr 12 '11 20:04

Kevin


1 Answers

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?

like image 62
Kevin Avatar answered Oct 02 '22 19:10

Kevin