简体   繁体   English

泽西·杰克逊的根名称以ArrayList的形式出现

[英]Jersey Jackson root name coming as ArrayList

I have an array list of objects. 我有一个对象数组列表。 ArrayList. 数组列表。 The Employee class is annotated with @XMLRootElement(name = "employees"). Employee类使用@XMLRootElement(name =“ employees”)进行注释。 I use jersey 1.8.1, Jackson 1.9.2 with POJOMappingFeature.The response is like 我使用球衣1.8.1,杰克逊1.9.2和POJOMappingFeature。响应就像

{
    ArrayList: [{name: John, age: 28}, {name: Mike, age:29}]
}

How do i make jackson display the correct root name (employees) in the json response. 我如何让杰克逊在json响应中显示正确的根名称(员工)。 i tried using @JsonName(value = "employees") on Employee class also. 我也尝试在Employee类上使用@JsonName(value =“ employees”)。 I need to do this without using a list wrapper like EmployeeListWrapper with attribute List. 我需要不使用带有属性List的EmployeeListWrapper之类的列表包装器来执行此操作。 i would like to have the response like 我想得到这样的回应

{
    employees: [{name: John, age: 28}, {name: Mike, age:29}]
}

Is it possible to do this using any jackson object mapper configuration. 是否可以使用任何杰克逊对象映射器配置来执行此操作。 Any help would be highly appreciated. 任何帮助将不胜感激。

You probably will not achieve this with @XMLRootElement or @JsonRootName annotations since the annotation would have to be put on the ArrayList class itself. 您可能无法使用@XMLRootElement@JsonRootName批注实现此目的,因为批注必须放在ArrayList类本身上。 And since you require to do it without any collection wrapper, then you will have to use Jackson ObjectMapper directly. 并且由于您需要在没有任何集合包装的情况下进行操作,因此您将不得不直接使用Jackson ObjectMapper

The mapper provides access to ObjectWriter builder, 映射器提供对ObjectWriter构建器的访问,

Builder object that can be used for per-serialization configuration of serialization parameters, such as JSON View and root type to use. 可以用于序列化参数(例如JSON View和要使用的根类型)的按序列化配置的Builder对象。

And the writer has withRootName() method that is what you need. 并且作者具有您需要的withRootName()方法。

Method for constructing a new instance with configuration that specifies what root name to use for "root element wrapping". 用配置构造新实例的方法,该配置指定用于“根元素包装”的根名称。

See code snippet below. 请参见下面的代码段。

ObjectWriter writer = ObjectMapper.writer().withRootName("employees");
writer.writeValueAsString(employees);

By default Jackson may not recognize the JAXB annotations but you can customise the object mapper to do this. 默认情况下,Jackson可能无法识别JAXB批注,但是您可以自定义对象映射器来执行此操作。

If you want to stick to the Jackson annotations, you can use @JsonRootName to indicate name to use for root-level wrapping. 如果要坚持使用Jackson批注,则可以使用@JsonRootName指示用于根级包装的名称。

Another option is to override method findRootName() in JaxbAnnotationIntrospector and use this inside ObjectMapper. 另一种选择是在重写方法findRootName() JaxbAnnotationIntrospector并使用该内部ObjectMapper。

So the code will look like: 因此代码如下所示:

@Override
public String findRootName(AnnotatedClass ac) {
    // will return "employees" for @XmlType(name = "employees")
    // Or you can return the class name itself
    return ac.getAnnotations().get(XmlType.class).name();
}

Reference: Customizing root name in Jackson JSON provider 参考: 在Jackson JSON提供程序中自定义根名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM