Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Jackson wrapping my objects with an extra layer named after the class?

When I serialize

public class FOO {
int field1;
String field2;
}

I got the following.

{"FOO":{"field1":0,"field2":"value"}}

Can you point me how can I make the output look like this

{"field1":0,"field2":"value"}
like image 362
mohamede1945 Avatar asked Jun 25 '11 18:06

mohamede1945


1 Answers

I've figured out how to make it. Actually the problem is that MappingJacksonJsonView has a map So that's why it returns it that way {"FOO":{"field1":0,"field2":"value"}}

But If I configured it that way

<beans:bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
       <beans:property name="extractValueFromSingleKeyModel" value="true" />
</beans:bean>

It will serialize the object itself not the whole map. I hope it will help someone else.

like image 180
mohamede1945 Avatar answered Nov 05 '22 06:11

mohamede1945