如何使用 Lombok 正确包含 "has a" 关系?

How to properly include a "has a" relationship using Lombok?

使用下面的 class 我正在尝试生成格式化日期:

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.text.SimpleDateFormat;
import java.util.Date;

@Builder
@ToString
@Getter
@Setter
public class Request
{

    private final SimpleDateFormat sf = new SimpleDateFormat("yyyy-mm-dd'T'HH:mm:ss");

    public String getStringTest(){
        return sf.format(new Date());
    }

    private final Date datePurchased;
}

我使用 :

生成 JSON
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Date;

public class Main {

    public static void main(String args[]) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        Request request = Request.builder().datePurchased(new Date()).build();
        System.out.println(objectMapper.writeValueAsString(request));

    }
}

呈现以下内容 JSON :

{"sf":{"calendar":-880959400190,"numberFormat":{"groupingUsed":false,"parseIntegerOnly":true,"maximumIntegerDigits":2147483647,"minimumIntegerDigits":1,"maximumFractionDigits":0,"minimumFractionDigits":0,"positivePrefix":"","positiveSuffix":"","negativePrefix":"-","negativeSuffix":"","multiplier":1,"groupingSize":3,"decimalSeparatorAlwaysShown":false,"parseBigDecimal":false,"roundingMode":"HALF_EVEN","decimalFormatSymbols":{"zeroDigit":"0","groupingSeparator":",","decimalSeparator":".","perMill":"‰","percent":"%","digit":"#","patternSeparator":";","infinity":"∞","minusSign":"-","currencySymbol":"€","currency":"EUR","internationalCurrencySymbol":"EUR","naN":"NaN","monetaryDecimalSeparator":".","exponentSeparator":"E"},"currency":"EUR"},"2DigitYearStart":-880959400190,"dateFormatSymbols":{"eras":["BC","AD"],"months":["January","February","March","April","May","June","July","August","September","October","November","December",""],"shortMonths":["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec",""],"weekdays":["","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],"shortWeekdays":["","Sun","Mon","Tue","Wed","Thu","Fri","Sat"],"zoneStrings":[["UTC","Coordinated Universal Time","UTC","Coordinated Universal Time","UTC","Coordinated Universal Time","UTC"],["Europe/Saratov","Saratov Standard Time","GMT+04:00","Saratov Standard Time","GMT+04:00","Saratov Time","GMT+04:00"],["Europe/Dublin","Greenwich Mean Time","GMT","Irish Standard Time","IST","Irish Time","IT"],["Europe/Astrakhan","Astrakhan Standard Time","GMT+04:00","Astrakhan Standard Time","GMT+04:00","Astrakhan Time","GMT+04:00"],["Asia/Famagusta","Eastern European Time","EET","Eastern European Summer Time","EEST","Eastern European Time","EET"],["Antarctica/Palmer","Chile Time","CLT","Chile Summer Time","CLST","Chile Time","CLT"],["Pacific/Bougainville","Bougainville Standard Time","BST","Bougainville Daylight Time","BST","Bougainville Time","BT"],["Asia/Srednekolymsk","Srednekolymsk Time","SRET","Srednekolymsk Daylight Time","SREDT","Srednekolymsk Time","SRET"],["Europe/Ulyanovsk","Ulyanovsk Standard Time","GMT+04:00","Ulyanovsk Standard Time","GMT+04:00","Ulyanovsk Time","GMT+04:00"],["America/Punta_Arenas","Punta Arenas Standard Time","GMT-03:00","Punta Arenas Standard Time","GMT-03:00","Punta Arenas Time","GMT-03:00"],["America/Los_Angeles","Pacific Standard Time","GMT-08:00","Pacific Daylight Time","GMT-07:00","Pacific Time","GMT-08:00"],["America/Denver","Mountain Standard Time","GMT-07:00","Mountain Daylight Time","GMT-06:00","Mountain Time","GMT-07:00"],["America/Phoenix","Mountain Standard Time","GMT-07:00","Mountain Daylight Time","GMT-07:00","Mountain Time","GMT-07:00"],["America/Chicago","Central Standard Time","GMT-06:00","Central Daylight Time","GMT-05:00","Central Time","GMT-06:00"],["America/New_York","Eastern Standard Time","GMT-05:00","Eastern Daylight Time","GMT-04:00","Eastern Time","GMT-05:00"],["America/Indianapolis","Eastern Standard Time","GMT-05:00","Eastern Daylight Time","GMT-04:00","Eastern Time","GMT-05:00"],["Pacific/Honolulu","Hawaii Standard Time","GMT-10:00","Hawaii Daylight Time","GMT-10:00","Hawaii Time","GMT-10:00"],["America/Anchorage","Alaska Standard Time","GMT-09:00","Alaska Daylight Time","GMT-08:00","Alaska Time","GMT-09:00"],.........................."stringTest":"2022-03-31T18:03:20"}

我希望 JSON 包含:

{"datePurchased":1643654681044,"stringTest":"2022-03-31T18:03:20"}

但是 toString() 方法似乎包含在 JSON 输出中?

如何使用 lombok 正确建模请求 class 中的关系,以便生成格式 {"sf":"datePurchased":1643652199803,"stringTest":"2022-03-31T18:03:20"}

getStringTest() 应该 return datePurchasedString 值,格式为 "yyyy-mm-dd'T'HH:mm:ss"

如果您只想将 datePurchased 的格式化版本包含在 stringTest 中,您需要做的就是用 JsonFormat 注释 getStringField 指示模式采用。您甚至不需要声明 sf,因为 Jackson 可以为您处理格式设置。

这一定够了:

//Same lombok annotations
public class Request {
    
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    public Date getStringTest() {
        return this.datePurchased;
    }

    private final Date datePurchased;
}

和你一样的测试,我得到这样的输出:

{"datePurchased":1643656778516,"stringTest":"2022-01-31T19:19:38"}

如果您出于某种原因被迫声明 sf,您可以通过直接用 @JsonIgnore