The java fastjson parameter Date is 8 hours less

scene:

After the A personnel service changes the role, it needs to notify the B process service to change the personnel, and use the api and restful method to notify. The parameter of A request is fastjson.

Request parameters:
 

{
	"reason": "测试变更",
	"logID": 75,
	"endDate": 1684512060000,
	"workContent": "FLOW",
	"dataEntryTime": 1684483157000,
	"autoRecover": "0BT",
	"dateEntryStaffName": "admin",
	"staffBName": "测试-小王",
	"beginDate": 1684425600000,
	"staffBID": 133710,
	"dateEntryStaffID": 1,
	"stateTime": 1684483157000,
	"isAllHasCommission": "0BF",
	"staffName": "测试-小李",
	"action": "MODIFY",
	"state": "A",
	"actionDate": 1681891141000,
	"staffID": 98467653
}

Timestamp Conversion URL

What the B process service receives is:

{
	"reason": "测试变更",
	"logID": 75,
	"endDate": "2023-05-19T16:01:00.000+0000",
	"workContent": "FLOW",
	"dataEntryTime": "2023-05-19T07:59:17.000+0000",
	"autoRecover": "0BT",
	"dateEntryStaffName": "admin",
	"staffBName": "测试-小王",
	"beginDate": "2023-05-18T16:00:00.000+0000",
	"staffBID": 133710,
	"dateEntryStaffID": 1,
	"stateTime": "2023-05-19T07:59:17.000+0000",
	"isAllHasCommission": "0BF",
	"staffName": "测试-小李",
	"action": "MODIFY",
	"state": "A",
	"actionDate": "2023-04-19T07:59:01.000+0000",
	"staffID": 98467653
}

The time has become a string, and it is still 8 hours short.

Think about adding a time zone to the attribute to solve it:

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date endDate;

But it is JSONObject, there is no place to set it. At this time, it must be set during parameter processing.

    @Bean
    public ObjectMapper jacksonObjectMapperCustomization() {
        TimeZone timeZone = TimeZone.getTimeZone("Asia/Shanghai");
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder().timeZone(timeZone);
        return builder.build();
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.removeIf(c -> c instanceof MappingJackson2HttpMessageConverter);
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter(
                jacksonObjectMapperCustomization());
        ObjectMapper objectMapper = new ObjectMapper();
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, StringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, StringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        jackson2HttpMessageConverter.setObjectMapper(objectMapper);
        converters.add(jackson2HttpMessageConverter);
        converters.add(new ByteArrayHttpMessageConverter());
        converters.add(new FormHttpMessageConverter());
        converters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
    }

set it and it didn't work.

Processing: receive with entity class

@ApiModel(value = "AB角变更参数类")
@Data
public class StaffRoleParam2 {
    private String logID;

    private String staffID;

    private String staffName;

    private Date beginDate;

    private Date endDate;

    private String reason;

    private String autoRecover;

    private String state;

    private Date stateTime;

    private String staffBID;

    private String staffBName;

    private String dateEntryStaffID;

    private String dateEntryStaffName;

    private Date dataEntryTime;

    private String msgType;

    private String isAllHasCommission;

    private String workContent;

    private Date actionDate;

    private String opAction;
}

It is normal to change it to receive with entity class. The time uses the Date type.

Summarize:

        For interface calls between different services, if the time is of Date type, it will become a string if it is processed by fastjson, which may also cause a time difference. At this time, it is best to use the object entity class parameter to receive and receive. Fortunately, it is better to make a note for easy maintenance. deal with.

Guess you like

Origin blog.csdn.net/qq_35461948/article/details/130806256