users@jersey.java.net

[Jersey] Re: how to rename XmlRootElement in JSON

From: Maxrunner <joao.rossa_at_gmail.com>
Date: Mon, 30 May 2011 08:18:18 -0700 (PDT)

Yes i have that feature set to true, so that if send only one organica, it
shows the root name. Here's my context wrapper as it is:


@Component
@Provider
public class JacksonObjectMapperProvider implements
ContextResolver<ObjectMapper>{

 public ObjectMapper getContext(Class<?> arg0) {

AnnotationIntrospector primary = new JacksonAnnotationIntrospector();
AnnotationIntrospector secondary = new JaxbAnnotationIntrospector();
AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary,
secondary);
 ObjectMapper result = new ObjectMapper();
result.configure(Feature.INDENT_OUTPUT, true);//human readable indentation
result.configure(Feature.WRAP_ROOT_VALUE, true);//show root element
 // make deserializer use JAXB annotations
 result.getDeserializationConfig().setAnnotationIntrospector(pair);
 // make serializer use JAXB annotations
 result.getSerializationConfig().setAnnotationIntrospector(pair);
 return result;
}
}


On Mon, May 30, 2011 at 4:15 PM, Jakub Podlesak-2 [via Jersey] <
ml-node+6419512-2061819540-73314_at_n2.nabble.com> wrote:

> I guess, you have the Jackson Feature.WRAP_ROOT_VALUE set to true in
> your object mapper.
> You probably need that to properly serialize another type.
>
> The bug [1] i was referring to earlier currently blocks
> you from doing something like follows, so that you can use different
> object mapper settings for various Java types:
>
> @Provider
> public class MyObjectMapperProvider implements
> ContextResolver<ObjectMapper> {
>
> final ObjectMapper defaultObjectMapper;
> final ObjectMapper organicasObjectMapper;
>
> public MyObjectMapperProvider() {
> defaultObjectMapper = ... // Jackson Feature.WRAP_ROOT_VALUE
> set to true here;
> organicasObjectMapper = ... // Jackson Feature.WRAP_ROOT_VALUE
> set to false here;
> }
>
> @Override
> public ObjectMapper getContext(Class<?> type) {
>
> if (type == ListWrapperWithMap.class) {
> return organicasObjectMapper;
> } else {
> return defaultObjectMapper;
> }
> }
> }
>
> If the above was working fine, i guess your problem would be solved, right?
>
>
> ~Jakub
>
> [1]http://java.net/jira/browse/JERSEY-726
>
>
> On 05/30/2011 04:45 PM, Maxrunner wrote:
>
> > This is what i get using the Map example, still the same problem, i get
> the
> > name "ListWrapperWithMap" which i dont want, if i can hide this name and
> let
> > only the "Organicas" name remain seems like a good idea for returning
> list
> > of objects with the name i want....
> >
> > {
> > "ListWrapperWithMap" : {
> > "Organicas" : [ {
> > "designacao" : null,
> > "dataAlteracao" : "2010-12-23",
> > "id" : 123,
> > "active" : true
> > }, {
> > "designacao" : "Organica Mind",
> > "dataAlteracao" : "2000-05-21",
> > "id" : 124,
> > "active" : false
> > }, {
> > "designacao" : "Organica Strength",
> > "dataAlteracao" : "2002-12-23",
> > "id" : 125,
> > "active" : true
> > }, {
> > "designacao" : "Organica Stamina",
> > "dataAlteracao" : "2011-06-14",
> > "id" : 126,
> > "active" : true
> > }, {
> > "designacao" : "Organica Luck",
> > "dataAlteracao" : "2006-02-01",
> > "id" : 127,
> > "active" : false
> > }, {
> > "designacao" : "Organica Love",
> > "dataAlteracao" : "2003-04-07",
> > "id" : 128,
> > "active" : false
> > } ]
> > }
> > }
> >
> > --
> > View this message in context:
> http://jersey.576304.n2.nabble.com/how-to-rename-XmlRootElement-in-JSON-tp6173292p6419404.html
>
> > Sent from the Jersey mailing list archive at Nabble.com.
> >
>
>
>
> ------------------------------
> If you reply to this email, your message will be added to the discussion
> below:
>
> http://jersey.576304.n2.nabble.com/how-to-rename-XmlRootElement-in-JSON-tp6173292p6419512.html
> To unsubscribe from how to rename XmlRootElement in JSON, click here<http://jersey.576304.n2.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=6173292&code=am9hby5yb3NzYUBnbWFpbC5jb218NjE3MzI5MnwyMDYzODA1MDkw>.
>
>


--
View this message in context: http://jersey.576304.n2.nabble.com/how-to-rename-XmlRootElement-in-JSON-tp6173292p6419538.html
Sent from the Jersey mailing list archive at Nabble.com.