Reputation: 603
I'd like know how do to a method return a JSON array of List, for example:
@GET
@Produces("application/json")
public List<String> aMethod(){
return Array.asList("text1", "text2", "text3");
}
I'd want to know, how do to receive a List argument type in my method, for example
@PUT
@Consumes("application/json") void otherMethod(List<String>){
// do something ;
}
I've read about JaxbContext, I understanding how it can help me.
Upvotes: 4
Views: 5068
Reputation: 2290
I tried the same without success, so I ended with packing a string into one field object.
return listWithString
.stream()
.map(
stringElement -> {
MyBoxObject bo = new MyBoxObject();
bo.setSomeField(stringElement);
return bo;
})
.collect(Collectors.toList());
Upvotes: 0
Reputation: 12006
With JAXB there are two type's of List
supported. The first is a List of elements, the second a delimited string (a "normal" XML value or attribute, which is parsed into a list using some delimiter). The first seems to be what you want ("array").
For reference, see: http://jaxb.java.net/jaxb20-ed/api/javax/xml/bind/annotation/XmlList.html
You will note that in both cases the List you want would need to be encapsulated by some other object. Fundamentally, XML (and by extension JAXB) likes to trace everything back to a single root node/object. So to model it, you need something like this:
@XmlRootElement(name="wrapper")
public abstract class ListWrapper {
public List<String> names;
}
Then your methods would need to be changed to accept/return ListWrapper
objects and extract the actual List from it.
Upvotes: 4