NetBeans Forums
| View previous topic :: View next topic |
| Author |
Message |
ido.ran
Joined: 12 Mar 2009 Posts: 13
|
Posted: Wed Mar 25, 2009 2:14 pm Post subject: jersey JAXB JSON single-element array |
|
|
Hi,
I'm using GlassFish V2 with jersey 1.0 and JAXB.
I have RESTful Web Service which expose several resources.
I'm using container-item pattern.
My problem is that when the container converter has only one element to return, JAXB format the array as property and not as array.
I was expecting to see this output:
{"@uri":"http://localhost:8080/AuroraService-war/resources/objectTypes","objectType":[{"@uri":"http://localhost:8080/AuroraService-war/resources/objectTypes/61b72060-96d6-40bf-a57c-a49a4ea2a8a8/","id":"61b72060-96d6-40bf-a57c-a49a4ea2a8a8","name":"AnItem"}]}
but I'm getting this output insted:
{"@uri":"http://localhost:8080/AuroraService-war/resources/objectTypes","objectType":{"@uri":"http://localhost:8080/AuroraService-war/resources/objectTypes/61b72060-96d6-40bf-a57c-a49a4ea2a8a8/","id":"61b72060-96d6-40bf-a57c-a49a4ea2a8a8","name":"AnItem"}}
I've bold the important parts. Notice that in the first output the item is surrounded with array brackets and in the second it is not.
What annotation should I use to make JAXB understand it should be format as array?
Thank you,
Ido. |
|
| Back to top |
|
 |
ido.ran
Joined: 12 Mar 2009 Posts: 13
|
Posted: Sat Mar 28, 2009 12:46 pm Post subject: Anyone know? |
|
|
| Thank you. |
|
| Back to top |
|
 |
roshanjk
Joined: 22 Mar 2010 Posts: 2
|
Posted: Mon Mar 22, 2010 9:38 am Post subject: Use a JaxbContextResolver |
|
|
I faced a similar issue recently - the solution might be useful to someone, although this is a year old post.
Consider this example:
class UserWrapperPojo {
List<User> users;
// The usual getter-setters.
}
class UserPojo {
String name;
// More properties and the getter-setters.
}
Now, if the wrapper contained multiple user objects, the response was:
{"users": [ {name="John", age="21"}, {name="Jane", age="18"}] }
However, if the wrapper contained a single user object, the response was:
{"users": {name="John", age="21"} }
That was throwing my client-side parsing for a toss. The solution here is to create a "JaxbContextResolver" and add the approprite @XmlElement annotations to the Pojos.
In the ContextResolver we explicitly declare the elements which should be treated as an array.
@Component
@Provider
public class JaxbContextResolver implements ContextResolver<JAXBContext> {
private JAXBContext context;
private Class[] types = { UserPojo.class, UserWrapperPojo.class };
public JaxbContextResolver() throws Exception {
this.context = new JSONJAXBContext(JSONConfiguration.mapped().arrays("users").build(), types);
}
public JAXBContext getContext(Class<?> objectType) {
return context;
}
}
Finally add the @XmlRootElement annotation to every Pojo. Likewise the getUsers() method in the UseWrapperPojo was annotated as:
@XmlElement
public List<User> getUsers() {
...
}
This resulted in the expected response:
{"users": [{name="John", age="21"}] }
---
Roshan Kulkarni
http://www.roshankulkarni.info |
|
| Back to top |
|
 |
roshanjk
Joined: 22 Mar 2010 Posts: 2
|
|
| Back to top |
|
 |
pansonm
Joined: 02 Oct 2010 Posts: 1
|
Posted: Sat Oct 02, 2010 7:34 pm Post subject: Single Item List Bug |
|
|
Thanks for posting this workaround, but this is BUG and should be fixed. Can someone on the jersey team fix this? I cannot think of any situation where you would want to return a list in some cases and an object in others. The "feature" is terrible.
Jackson handles this case properly -- my solution was to change my return type to a string and use Jackson to generate the json -- not very happy with it, but it works. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|