Is it OK to use Gson instance as a static field in a model bean (reuse)?

前端 未结 4 1185
逝去的感伤
逝去的感伤 2020-11-29 02:44

Here\'s the model I implemented:

public class LoginSession {
    private static final Gson gson = new Gson();

    private String id;
    private String name         


        
4条回答
  •  遥遥无期
    2020-11-29 03:07

    The core Gson class is thread-safe. I just encountered a thread-safety issue that was supposedly with GSON. The issue happened when using a custom JsonDeserializer and JsonSerializer for Date parsing and formatting. As it turned out, the thread-safety issue was with my method's use of a static SimpleDateFormat instance which is not thread-safe. Once I wrapped the static SimpleDateFormat in a ThreadLocal instance, everything worked out fine.

提交回复
热议问题