我知道如何在Jackson中使用自定义序列化程序(通过扩展JsonSerializer
),但我希望默认的序列化程序适用于所有字段,除了1个字段,我想使用自定义序列化程序覆盖它.
注释不是一个选项,因为我正在序列化生成的类(来自Thrift).
在编写自定义jackson序列化程序时,如何仅指定要覆盖的某些字段?
更新:
这是我要序列化的类:
class Student {
int age;
String firstName;
String lastName;
double average;
int numSubjects
// .. more such properties ...
}
Run Code Online (Sandbox Code Playgroud)
上面的类有许多特性,其中大多数使用本机类型.我想覆盖自定义序列化程序中的一些属性,让Jackson像往常一样处理其余的属性.例如,我只想将"年龄"字段转换为自定义输出.
我有两个JPA实体,一个带有SDR导出的存储库,另一个带有Spring MVC控制器,还有一个非导出的存储库.
MVC公开实体具有对SDR管理实体的引用.请参阅下面的代码参考.
User
从中检索a时,问题就出现了UserController
.SDR管理的实体不会序列化,而且似乎Spring可能正在尝试在响应中使用HATEOAS引用.
这是GET
一个完全填充的User
外观:
{
"username": "foo@gmail.com",
"enabled": true,
"roles": [
{
"role": "ROLE_USER",
"content": [],
"links": [] // why the content and links?
}
// no places?
]
}
Run Code Online (Sandbox Code Playgroud)
如何User
通过嵌入式SDR管理实体从Controller中清楚地返回实体?
Spring MVC管理
实体
@Entity
@Table(name = "users")
public class User implements Serializable {
// UID
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonIgnore
private Long id;
@Column(unique = true)
@NotNull
private String username;
@Column(name = "password_hash")
@JsonIgnore
@NotNull
private String passwordHash;
@NotNull …
Run Code Online (Sandbox Code Playgroud) 鉴于以下POJO ..
public class City {
private String title;
private List<Person> people;
}
Run Code Online (Sandbox Code Playgroud)
...
public class Person {
private String name;
private int age;
}
Run Code Online (Sandbox Code Playgroud)
我想让Jackson将类的实例序列化为以下示例 JSON:
{
"title" : "New York",
"personName_1" : "Jane Doe",
"personAge_1" : 42,
"personName_2" : "John Doe",
"personAge_2" : 23
}
Run Code Online (Sandbox Code Playgroud)
JSON格式由外部API定义,我无法更改.
我已经发现我可以使用自定义序列化程序对列表字段进行注释,例如:
@JsonSerialize(using = PeopleSerializer.class)
private List<Person> people;
Run Code Online (Sandbox Code Playgroud)
......这是我尝试过的基本实现:
public class PeopleSerializer extends JsonSerializer<List<Person>> {
private static final int START_INDEX = 1;
@Override
public void serialize(List<Person> people,
JsonGenerator generator,
SerializerProvider …
Run Code Online (Sandbox Code Playgroud) 我正在遵循 spring 网站https://spring.io/guides/gs/sumption-rest/的启动指南。
我没有遵循确切的教程,因为我正在使用另一个端点:http : //www.omdbapi.com ? s= rush。
我在将 JSON 转换为 POJO 时遇到问题。我没有收到任何错误或异常。有人能指出我哪里出错了吗?
你可以在这里找到完整的代码
这是我的 POJO:
package com.sample.restapi.model;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown=true)
public class SearchResponse {
private List<Search> search;
private String totalResults;
private String response;
public SearchResponse() {
}
public List<Search> getSearch() {
return search;
}
public void setSearch(List<Search> search) {
this.search = search;
}
public String getTotalResults() {
return totalResults;
}
public void setTotalResults(String totalResults) {
this.totalResults = totalResults;
}
public …
Run Code Online (Sandbox Code Playgroud) java ×4
jackson ×2
spring-boot ×2
json ×1
nested ×1
resttemplate ×1
spring-data ×1
spring-mvc ×1