如何配置 Jackson ObjectMapper 以显示根值



在我的春季项目中,我添加了杰克逊 1,现在是 2,然后我已经看到了这种差异。以前的响应是使用"登录响应"射精的,现在 Json 中没有对象名称。
用于登录响应的旧对象类如下:

public class LoginResponse {
    private String code;
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
}

以下是我用于登录的新对象类带有注释和SerializationFeature.WRAP_ROOT_VALUE的响应,false:

@JsonRootName(value = "loginResponse") 
public class LoginResponse {
private String code;
public String getCode() {
    return code;
}
public void setCode(String code) {
    this.code = code;
}
ObjectMapper aa= new ObjectMapper().configure(SerializationFeature.WRAP_ROOT_VALUE, false);

}

我想要这个输出:

{
   "loginResponse":
   {
       "code": 0
   }
}

但它给了我以下回应:

 {
       "page": 0
}

请任何人都知道这一点。如何解决?请。

我认为

SerializationFeature.WRAP_ROOT_VALUE应该设置为true,而不是像上面的代码片段那样设置为false。

你把它设置为 false 而不是 true。

使用这个。

ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);

最新更新