Java 如何使用Jackson重命名JSON序列化中的根密钥

Java 如何使用Jackson重命名JSON序列化中的根密钥,java,json,jackson,root,Java,Json,Jackson,Root,我使用Jackson对对象列表进行JSON序列化 以下是我得到的: {"ArrayList":[{"id":1,"name":"test name"}]} {"ArrayList":[{"id":1,"name":"test name"}]} 但我想要这个: {"rootname":[{"id":1,"name":"test name"}]} // ie showing the string I want as the root name. {"rootname":[{"id":1,"na

我使用Jackson对对象列表进行JSON序列化

以下是我得到的:

{"ArrayList":[{"id":1,"name":"test name"}]}
{"ArrayList":[{"id":1,"name":"test name"}]}
但我想要这个:

{"rootname":[{"id":1,"name":"test name"}]} // ie showing the string I want as the root name.
{"rootname":[{"id":1,"name":"test name"}]} // ie showing the string I want as the root     name.
以下是我的方法:

接口:

public interface MyInterface {
    public long getId();
    public String getName();
}
实现类:

@JsonRootName(value = "rootname")
public class MyImpl implements MyInterface {
    private final long id;
    private String name;

    public MyImpl(final long id,final name) {
        this.id = id;
        this.name = name;
    }

   // getters     
}
JSon序列化:

public class MySerializer {
    public static String serializeList(final List<MyInterface> lists) {
        //check for null value.Throw Exception
        final ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
        return mapper.writeValueAsString(lists);
    }
}
但我想要这个:

{"rootname":[{"id":1,"name":"test name"}]} // ie showing the string I want as the root name.
{"rootname":[{"id":1,"name":"test name"}]} // ie showing the string I want as the root     name.
我已经尝试了所有我能找到的建议解决方案,但未能实现我的目标。我看过:


还是我遗漏了什么?我用jackson 1.9.12来做这个。欢迎在这方面提供任何帮助。

默认情况下,Jackson在尝试确定要为包装的值显示的根名称时使用两种注释之一-或。它希望此注释位于正在序列化的类型上,否则将使用该类型的名称作为根名称

在本例中,您正在序列化一个列表,这就是根名称为“ArrayList”(被序列化类型的简单名称)的原因。列表中的每个元素都可以是带有@JsonRootName注释的类型,但列表本身不是

当您尝试包装的根值是一个集合时,则需要某种定义包装名称的方法:

保持器/包装器类 您可以创建一个包装类来保存列表,并使用注释来定义所需的属性名称(仅当您无法直接控制
ObjectMapper
/JSON转换过程时,才需要使用此方法):


您需要在类的顶部使用此注释


@JsonTypeName(“rootname”)
我知道,我迟到了,但我有更好的方法,不需要Holder/Wrapper类。它从注释中拾取根键

package com.test;

import com.fasterxml.jackson.annotation.JsonRootName;


@JsonRootName("Products")
public class ProductDTO {
    private String name;
    private String description;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}
以下是测试类:-

package com.test;

import java.io.IOException;
import java.util.ArrayList;

import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;


public class ProductDTOTestCase {
    @Test
    public void testPersistAndFindById() throws JsonGenerationException, JsonMappingException, IOException {
        ObjectMapper mapper = new ObjectMapper();
        ProductDTO productDTO = new ProductDTO();
        productDTO.setDescription("Product 4 - Test");

        ArrayList<ProductDTO> arrayList = new ArrayList<ProductDTO>();
        arrayList.add(productDTO);

        String rootName = ProductDTO.class.getAnnotation(JsonRootName.class).value();
        System.out.println(mapper.writer().withRootName(rootName).writeValueAsString(arrayList));

    }


}
@JsonTypeName(“usuarios”)
@JsonTypeInfo(include=JsonTypeInfo.As.WRAPPER\u对象,use=JsonTypeInfo.Id.NAME)
公共类UsuarioDT扩展了ArrayList{
@JsonProperty(“rowsAffected”)
私有整数afectados;
公共整数getAfectados(){
返回afectados;
}
public void setAfectados(整数afectados){
this.afectados=afectados;
}
}

尝试序列化MyImpl实例,而不是MyImpl列表。序列化程序正在做他的工作,因为他正在包装一个列表而不是一个MyDe界面对象。请考虑添加一个解释为什么这是答案。非常好的答案+ +当你创建一个对象映射器并把它传递给第三方LIBS的时候,你怎么能通用地实现ObjectWriter案例?我看不出有什么办法可以告诉jackson“一直使用这个objectwriter来处理这些类型”。只需一行
newObjectMapper().enable(SerializationFeature.WRAP_ROOT_VALUE).writer().withRootName(“rootName”).writeValueAsString(列表)
package com.test;

import java.io.IOException;
import java.util.ArrayList;

import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;


public class ProductDTOTestCase {
    @Test
    public void testPersistAndFindById() throws JsonGenerationException, JsonMappingException, IOException {
        ObjectMapper mapper = new ObjectMapper();
        ProductDTO productDTO = new ProductDTO();
        productDTO.setDescription("Product 4 - Test");

        ArrayList<ProductDTO> arrayList = new ArrayList<ProductDTO>();
        arrayList.add(productDTO);

        String rootName = ProductDTO.class.getAnnotation(JsonRootName.class).value();
        System.out.println(mapper.writer().withRootName(rootName).writeValueAsString(arrayList));

    }


}
{"Products":[{"name":null,"description":"Product 4 - Test"}]}
@JsonTypeName("usuarios")
@JsonTypeInfo(include= JsonTypeInfo.As.WRAPPER_OBJECT,use= JsonTypeInfo.Id.NAME)
public class UsuarioDT extends ArrayList<Usuario> {

    @JsonProperty("rowsAffected")
    private Integer afectados;

    public Integer getAfectados() {
        return afectados;
    }

    public void setAfectados(Integer afectados) {
        this.afectados = afectados;
    }
}