Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
public class FooEntity {
String foo;
String bar;
public String getFoo() {
return foo;
public void setFoo(String foo) { = foo;
public String getBar() {
return bar;
public void setBar(String bar) { = bar;
public class FooList {
public List<FooEntity> fooList;
public FooList() {
this.fooList = new ArrayList<FooEntity>();
public void add(FooEntity fooEntity) {
public List<FooEntity> getFooList() {
return fooList;
public void setFooList(List<FooEntity> fooList) {
this.fooList = fooList;
public class WrappingTest {
public void fooListToJson() throws IOException {
FooList fooList = new FooList();
FooEntity fooEntity1 = new FooEntity();
fooEntity1.setBar("fooEntity1 bar value");
fooEntity1.setFoo("fooEntity1 foo value");
FooEntity fooEntity2 = new FooEntity();
fooEntity2.setBar("fooEntity2 bar value");
fooEntity2.setFoo("fooEntity2 foo value");
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
StringWriter stringWriter = new StringWriter();
final JsonGenerator jsonGenerator = mapper.getJsonFactory().createJsonGenerator(stringWriter);
mapper.writeValue(jsonGenerator, fooList);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment