- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个使用 @JsonIgnore
作为 ID 字段的 DTO,一切正常。但是,出于使用 resttemplate
进行测试的目的,在使用映射器将字符串转换为 DTO 后,ID 将设置为 null。如何解决这个问题?
public class CarDTO {
@JsonIgnore
private Long id;
// there are more fields but not necessary to put all here
@JsonProperty
@ApiModelProperty(hidden = true)
public Long getId() {
return id;
}
}
这是 resttemplate
调用:
@Test
public void createACar() throws Exception {
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
CarDTO carDTO = getCarDTO("LL22222111");
HttpEntity<CarDTO> entity = new HttpEntity<>(carDTO, headers);
// Here the response has id with value
ResponseEntity response = restTemplate.postForEntity(BASE_URI, entity, String.class);
// But after conversion of string to carDTO id is set to null
CarDTO carResponse = mapper.readValue(response.getBody().toString(), CarDTO.class);
// Assertions ...
}
我注释掉了@JsonIgnore
以确保它是否能正常工作,而且确实如此。
最佳答案
您指定:
@JsonIgnore
private Long id;
@JsonProperty
public Long getId() {
return id;
}
这意味着您为 Jackson 启用了序列化(使用 getter)并禁用了反序列化(在字段上使用 @JsonIgnore
)
在您的测试中,这不会执行任何 JSON 反序列化(只是将响应正文存储为 String
):
ResponseEntity response = restTemplate.postForEntity(BASE_URI, entity, String.class);
因此,响应对象正文中不应跳过 id。
它准确反射(reflect)了服务器作为响应返回的内容。
但这会对 CarDTO
执行反序列化:
CarDTO carResponse = mapper.readValue(response.getBody().toString(), CarDTO.class);
因此 Jackson 应用您在 CarDTO
的 Jackson 注释中指定的内容:如果需要反序列化该字段,只需删除该注解即可。
关于java - Mapper获取用@JsonIgnore注释的字段的空值(resttemplate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57348484/
Field 上的 JsonIgnore 与 Jackson 字段的 getter 上的 JsonIgnore 有什么区别? 最佳答案 @JsonIgnore注解用于忽略反序列化和序列化的字段,可以直接
是否可以在序列化 JSON 响应的同时根据 If 条件排除某些元素? if(a == 1) { //show element } else { //don't show element }
我有一个域javabean,一些bean包含很多带有密码的信息并登录 Ip,我使用 @jsonIgnore 来过滤我不使用的属性希望最终用户知道。 但是有一个问题,用其他方法我使用相同的 javabe
是否可以在序列化 JSON 响应的同时根据 If 条件排除某些元素? if(a == 1) { //show element } else { //don't show element }
在处理项目时,我犯了一个错误,将 @JsonIgnore 放在 setter 而不是 getter 属性上,如下所示 private Set books; public Set getBooks()
我使用@JsonIgnore属性来忽略pojo中的一些属性,但是使用Gson库解析json后,这些字段在json响应中不会被忽略,请帮忙。 import com.fasterxml.jackson.a
我正在使用一对多基数连接表,我使用的类相互引用。而且我正在使用 @JsonIgnore 注释,但没有深入理解它。 最佳答案 @JsonIgnore 用于忽略序列化和反序列化中使用的逻辑属性。 @Jso
由于某种原因,注释@JsonIgnore在我的项目中不起作用。我已经阅读了一些答案,这可能是由于使用不同的不兼容的 Jackson 版本(org.codehaus 与 com.fasterxml)引起
我是 Java 和 Jackson 以及我尝试使用的许多其他技术的新手,所以我希望得到详细的回答。 有没有一种方法可以防止使用 Jackson 将一个或多个字段序列化为 JSON String_lik
我有以下抽象类: public abstract class StandardTimeStamp { @Temporal(TemporalType.TIMESTAMP) @Column(nul
我有一个类(class)家长 public class Parent { private int id; @JsonIgnore int getId() {} void set
我在处理 Lombok 和 Jackson 时遇到了非常奇怪的问题。以下是我正在处理的一段代码。 @Getter @Setter @NoArgsConstructor @XmlRootElement
我正在使用 spring 4.0.1、hibernate 4.3.5、jackson 1.9.2 和 STS IDE我正在创建一个以 JSON 格式返回数据的 RESTful 网络服务当我使用 Hib
我有这样的东西: @Entity @Table(name = "myEntity") public class MyEntity { //.... @Column(name = "conten
如何解决这个问题? 我尝试将 @JsonIgnore 添加到 getter,但结果是相同的。 POJO: public class Category { // Omitted details
我有一个带有 @JsonIgnore-d 字段的类: public class MyClass { ... @JsonIgnore private SomeType myfie
我有类似的东西: @Entity @Table(name = "myEntity") public class MyEntity { //.... @Column(name = "conten
我可能有 60-70 个类,它们都有各种 Id 列,当我从 Web API 返回 JSON 数据时,我想排除这些列。在内部,我加入了 Id,但任何面向前端的东西都使用 Guid。所以我的主键是 Id
JsonIgnore 属性可用于忽略序列化中的某些属性。我想知道是否可以做相反的事情?所以 JsonSerializer 会忽略每个属性,除非它有一个特殊属性? 最佳答案 是的。当您使用 [JsonO
我有两个 Java 类:Usuario 和 Entrada。 乌苏里奥: @Entity @Table(name="usuario") @NamedQuery(name="Usuario.findAl
我是一名优秀的程序员,十分优秀!