我正在使用 MappingJacksonJsonView 将类序列化为 JSON,但是,我希望能够根据 getter 名称从默认名称重命名某些字段。
这是因为我必须为 jQuery 文件上传输出像“delete_url”和“delete_type”这样的字段名称。我正在使用@Jsonserialize 注释来手动选择要序列化的字段。
@JsonAutoDetect(getterVisibility = Visibility.NONE)
public interface Picture {
@JsonSerialize
String getName();
@JsonSerialize
String getDelete_url();
...
例如,我被迫调用一个方法getDelete_url()
,而我想调用它getDeleteUrl()
,但仍然输出 key “delete_url”
序列化为 JSON 时。
最佳答案
您应该能够使用 @JsonProperty
获得资格.
@JsonAutoDetect(getterVisibility = Visibility.NONE)
public interface Picture {
@JsonSerialize
@JsonProperty("name")
String getName();
@JsonSerialize
@JsonProperty("delete_url")
String getDeleteUrl();
//...
关于java - 重命名 Spring 中 MappingJacksonJsonView 使用的 JSON 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7893619/