注释会影响jackson如何写入每个对象和基元的类型

时间:2018-03-20 23:16:16

标签: java json annotations jackson mapping

我认为注释可能会干扰json输出。输出打印的对象的字段为整数,字符串,值类型和字符,它们不在类中。如何避免这种情况并得到简单的json?

我正在使用带有ObjectMapper的jackson-databind 2.8.9。

BaseEntity.java

@MappedSuperclass
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public abstract class BaseEntity implements Serializable {

    private static final long serialVersionUID = 5234608700757438450L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    protected Integer id;

    @Version
    @XmlTransient
    protected Integer version;
    ...
}

Type.java

@Entity
@Table(name="type")
@XmlRootElement
public class Type extends BaseEntity {

    private static final long serialVersionUID = 1L;

    static final String PREFIX = "work.entity.WorkType.";
    public static final String findAll = PREFIX + "findAll";

    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "name", nullable = false, length = 45)
    private String name;

    @Size(max = 45)
    @Column(name = "description", length = 45)
    private String description;
    ...
}

代码

...
Type type = new Type("Work);
type.setId(1);
type.setDescription("Duration months");
ObjectMapper mapper = new ObjectMapper();
mapper.writeValueAsString(type)
...

Json在生成之前输出代码

{
  "id" : {
    "integral" : true,
    "valueType" : "NUMBER"
  },
  "name" : {
    "string" : "Work",
    "valueType" : "STRING",
    "chars" : "Work"
  },
  "description" : {
    "string" : "Duration months",
    "valueType" : "STRING",
    "chars" : "Duration months"
  }
}

Json,我想获得¿?

{
  "id" : 1,
  "name" : "Work",
  "description" : "Duration months",
}

0 个答案:

没有答案