简体   繁体   中英

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

Using the below class I'm attempting to generate a formatted date:

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;
}

I generate the JSON using:

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));

    }
}

which renders the following 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"}

I'm expecting the JSON to contain:

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

But it seems the toString() method is included in the JSON output?

How to correctly model the relationship in the Request class using lombok such that the format {"sf":"datePurchased":1643652199803,"stringTest":"2022-03-31T18:03:20"} is produced?

getStringTest() should return a String value of datePurchased in format "yyyy-mm-dd'T'HH:mm:ss"

If all you want is to include a formatted version of datePurchased as stringTest , all you need to do is annotate getStringField with JsonFormat indicating the pattern to use. You don't even need to declare sf as Jackson can take care of formatting for you.

This must be enough:

//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;
}

And with the same test as yours, I get an output like this:

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

If you were forced to declare sf for some reason, you could have avoided it being included in your final JSON by annotating it (the field/property) directly with @JsonIgnore

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM