インターセプタまたはフィルタサウンドを使用して、私に過度のサウンドを与えます。特定の日時フィールドをユーザー固有のタイムゾーンに変換するだけの場合、ユーザーのタイムゾーンに変換できる日時フィールドの各要求を確認することは多すぎます。
より簡単なアプローチは、クライアント側との間でJavaオブジェクトをデシリアライズするときにカスタムJsonSerializer
とJsonDeserializer
を指定することです。 Java 8を使用しているので、ZonedDateTimeを使用してDateTimeとZoneを1つのフィールドに格納できます。
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)、並行処理はどうですか?私はシリアライザ/デシリアライザがシングルトンになるだろうと思っています。シングルトンの場合、各クライアントに対して適切なhttpSessionを注入するという保証はありません。私はこの場合、ThreadLocalアプローチがうまくいくと思います。 DTOアノテーションを追加する必要なくシリアライザ/デシリアライザをデフォルトで定義する機会はありますか? –
はい、シリアライザはシングルレングスで、httpsessionはセッションスコープです。その場合、ユーザーにとって正しいセッションを取得することについて心配する必要はありません。春はそれを処理します。 Springはhttpsessionのインテリジェントなプロキシインジェクションを行い、それを適切なユーザに内部的に委譲します。 [this](https://dzone.com/articles/using-http-session-spring)を参照してください。d土曜日の素敵な記事 – Bnrdo
意味を成して、受け入れる:-) –