arrayobject以及状态和消息

xe55xuns  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(225)

我希望api响应如下: {"success":"false/true","msg":"some message","data":{}} 如果有数据,则应打印响应数据
"data":{} ApiResponse Class ```
public ApiResponse(Boolean success, String message,JSONObject data) {
this.success = success;
this.message = message;
this.setData(data);
}

数据返回 `Controller` ```
JSONObject dataObject = new JSONObject(user);
return new ResponseEntity(new ApiResponse(false, "User is Disabled",dataObject , HttpStatus.UNAUTHORIZED);
az31mfrm

az31mfrm1#

SpringBoot将在内部使用jackson objectmapper来序列化对象。
通过在创建objectmapperbean时使用此属性,可以指定即使字段为null也要包含这些字段

@Bean
    ObjectMapper objectMapper() {
        return Jackson2ObjectMapperBuilder.json()
            .serializationInclusion(JsonInclude.Include.ALWAYS) // Include even empty values in the json
            .build();
}

相关问题