요격기 나 필터 소리를 사용하여 과장된 소리를 들려줍니다. 사용자 시간대로 변환 할 수있는 datetime 필드에 대한 각 요청을 확인하는 것은 특정 datetime 필드를 사용자 특정 시간대로 변환하려는 경우 너무 많습니다.
더 쉬운 접근법은 클라이언트 측에서/클라이언트 측으로 자바 객체를 직렬화 할 때 사용자 정의 JsonSerializer
과 JsonDeserializer
을 지정하는 것입니다. Java 8을 사용하기 때문에 ZonedDateTime을 사용하여 DateTime과 Zone을 하나의 필드에 저장할 수 있습니다.
userTimeZone을 세션에 저장하는 것이 일반적입니다. 커스텀시 리즈 라이저는 스프링 빈일 필요가 있기 때문에 세션을 주입 할 수 있고 그 안에 저장된 userTimeZone을 가져올 수 있습니다. 클라이언트 측에
public class TheDTO {
@JsonSerialize(using = UserTimeZoneAwareSerializer.class)
@JsonDeserialize(using = UserTimeZoneAwareDeserializer.class)
private ZonedDateTime dateTime;
public ZonedDateTime getDateTime() {
return dateTime;
}
public void setDateTime(ZonedDateTime dateTime) {
this.dateTime = dateTime;
}
}
시리얼 라이저, 핸들러에서 :
것은 당신에게 어떤 psuecode
DTO를 공유
@Component
public class UserTimeZoneAwareSerializer extends JsonSerializer<ZonedDateTime> {
@Autowired
private HttpSession httpSession;
@Override
public void serialize(ZonedDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
// Grab the userTimeZone from the session then convert from UTC to userTimeZone
gen.writeObject(/**/);
}
}
디시리얼라이저, 클라이언트 측에서 핸들러 :
@Component
public class UserTimeZoneAwareDeserializer extends JsonDeserializer<ZonedDateTime> {
@Autowired
private HttpSession httpSession;
@Override
public ZonedDateTime deserialize(JsonParser p, DeserializationContext ctxt)
// Grab the userTimeZone then convert from userTimeZone to UTC
// ...
}
}
이렇게하면 사용자 시간대를 인식하려는 ZonedDateTime
필드에 쉽게 주석을 달 수 있습니다.
[@Bnrdo] (https://stackoverflow.com/users/1767366/bnrdo), 동시성은 어떨까요? serializer/deserializer가 싱글 톤이 될 것이라고 생각합니다. 맞습니까? 그들이 싱글 톤인 경우 각 클라이언트에 올바른 httpSession을 주입 할 것이라는 보장은 없습니다. 이 경우에는 ThreadLocal 접근 방식이 더 잘 작동한다고 생각합니다. DTO 주석을 추가 할 필요없이 기본적으로 serializer/deserializer를 정의 할 수 있습니까? –
예, 직렬/직렬 변환기는 싱글 렛이며 httpsession은 물론 세션 범위입니다. 이 경우 사용자에게 올바른 세션을 가져 오는 것에 대해 걱정할 필요가 없습니다. 봄이 처리합니다. Spring은 httpsession을 지능적으로 주입하고 내부적으로 적절한 사용자에게 위임합니다. [this] (https://dzone.com/articles/using-http-session-spring)을 참조하십시오. dirty에서 좋은 기사 – Bnrdo
의미가 있습니다, 수락 :-) –