users@jersey.java.net

[Jersey] Re: how to rename XmlRootElement in JSON

From: Jakub Podlesak <jakub.podlesak_at_oracle.com>
Date: Tue, 31 May 2011 08:37:59 +0200

Please see in-line...

On 05/30/2011 06:01 PM, Maxrunner wrote:
> Ok, so i use the example you gave just now right?Here's my code:
>
> @Component
you do not need the @Component annotation

> @Provider
> public class JacksonObjectMapperProvider implements
> ContextResolver<ObjectMapper>{
> private ObjectMapper defaultObjectMapper;
> private ObjectMapper listsObjectMapper;
>
> public JacksonObjectMapperProvider() {
> super();
> MyObjectMapperProvider();
> }

I do not know what the above means. Please note, that you should register
just one ContextResolver<ObjectMapper> instance, as Jackson will use
only the first one
if more that one such provider is found. To create different object
mappers, you should
use the getContext method bellow.

>
> public void MyObjectMapperProvider() {
> defaultObjectMapper = new ObjectMapper();
> defaultObjectMapper.configure(Feature.INDENT_OUTPUT, true);
> defaultObjectMapper.configure(Feature.WRAP_ROOT_VALUE, true);
> listsObjectMapper= new ObjectMapper();
> listsObjectMapper.configure(Feature.INDENT_OUTPUT, true);
> listsObjectMapper.configure(Feature.WRAP_ROOT_VALUE, false);

right, the above should ensure, the enclosing element is omitted.

> }
> public ObjectMapper getContext(Class<?> type) {
>
> if (type == ListWrapperWithMap.class) {
> return listsObjectMapper;
> } else {
> return defaultObjectMapper;
> }
> }
> }
>
> This should work right? Also is this the correct url?
> http://download.java.net/maven/2/com/sun/jersey/jersey-archive/1.8-SNAPSHOT/
>
Correct. The fixed version should already be available there.

~Jakub

> Regards,
>
> On Mon, May 30, 2011 at 4:50 PM, Jakub Podlesak-2 [via Jersey]
> <[hidden email] </user/SendEmail.jtp?type=node&node=6419697&i=0>> wrote:
>
> I have just fixed the bug. In a few hours, if everything goes fine,
> the fixed version should become available in the maven repository
> as 1.8-SNAPSHOT.
>
> Then you should be able to return different ObjectMapper instance
> based on the arg0 parameter, and hide the annoying extra
> "ListWrapperWithMap"
> element.
>
> ~Jakub
>
>
>
> On 05/30/2011 05:18 PM, Maxrunner wrote:
>> 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]
>> <[hidden email]
>> <http://user/SendEmail.jtp?type=node&node=6419538&i=0>> 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.
>>
>>
>>
>> ------------------------------------------------------------------------
>> View this message in context: Re: how to rename XmlRootElement in
>> JSON
>> <http://jersey.576304.n2.nabble.com/how-to-rename-XmlRootElement-in-JSON-tp6173292p6419538.html>
>>
>>
>> Sent from the Jersey mailing list archive
>> <http://jersey.576304.n2.nabble.com/> 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-tp6173292p6419654.html
>
> To unsubscribe from how to rename XmlRootElement in JSON, click here.
>
>
>
> ------------------------------------------------------------------------
> View this message in context: Re: how to rename XmlRootElement in JSON
> <http://jersey.576304.n2.nabble.com/how-to-rename-XmlRootElement-in-JSON-tp6173292p6419697.html>
> Sent from the Jersey mailing list archive
> <http://jersey.576304.n2.nabble.com/> at Nabble.com.