在Spring Boot中,可以使用@JsonIgnore注解来标记不希望返回的字段。在模型类的对应字段上添加@JsonIgnore注解,即可让该字段在返回结果中被忽略。

示例代码如下:

public class User {
    private String username;
    
    @JsonIgnore
    private String password;
    
    // getters and setters
}

在上述示例中,User类的password字段被标记为@JsonIgnore,因此在返回结果中将不会包含该字段。

请注意,使用@JsonIgnore注解会影响到该字段的序列化和反序列化,因此在需要对该字段进行操作时需要注意。如果只是希望在返回结果中忽略该字段,而不影响其他操作,可以考虑使用@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)注解。这样可以保留字段的序列化功能,但在反序列化时会被忽略。

示例代码如下:

public class User {
    private String username;
    
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private String password;
    
    // getters and setters
}

在上述示例中,User类的password字段被标记为@JsonProperty(access = JsonProperty.Access.WRITE_ONLY),因此在返回结果中将不会包含该字段,但在反序列化时会被忽略。

springboot 如何让模型的字段不返回

原文地址: http://www.cveoy.top/t/topic/h08O 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录