public class Context extends Object
store(String, Object)
method and
later retrieve it with get(String, Class)
or remove it with remove(String, Class)
.
You can achieve the same thing with ThreadLocalHolder
that stores the data in a thread
local map but it is cleaner to use this Context class as you wont have to worry about removing
values from it. Indeed java web servers reuse created threads, so if you store data in a thread
local variable and don't remove it, it will still be present when another request comes in and is
bound to that thread! This can also lead to memory leaks! Don't forget the
try-store-finally-remove block if you stick with ThreadLocalHolder.
This class stores also the views present in the current context, those views will be applied to
the matching objects during serialization and deserialization.BeanView
,
BeanViewConverter
,
ThreadLocalHolder
Constructor and Description |
---|
Context(Genson genson) |
Context(Genson genson,
List<Class<? extends BeanView<?>>> views) |
Modifier and Type | Method and Description |
---|---|
<T> T |
get(String key,
Class<T> valueType)
Returns the value mapped to key in this context or null.
|
boolean |
hasViews() |
<T> T |
remove(String key,
Class<T> valueType)
Removes the mapping for this key from the context.
|
Object |
store(String key,
Object o)
Puts the object o in the current context indexed by key.
|
List<Class<? extends BeanView<?>>> |
views() |
Context |
withView(Class<? extends BeanView<?>> view) |
public final Genson genson
public Context(Genson genson)
public boolean hasViews()
public Object store(String key, Object o)
key
- must be not nullo
- public <T> T get(String key, Class<T> valueType)
key
- must be not nullvalueType
- the type of the value, null not allowedClassCastException
- if the value mapped to key is not of type valueType.public <T> T remove(String key, Class<T> valueType)
key
- must be not nullvalueType
- the type of the value, null not allowedClassCastException
- if the value mapped to key is not of type valueType.get(String, Class)
Copyright © 2019. All rights reserved.