如何将参数传递给JsonSerializer? [英] How to pass parameter to JsonSerializer?
问题描述
我有一个简单的数据服务:
I have a simple data service :
@GET
public Data getData(@QueryParam("id") Long id) {
Data data = dataService.getData(id);
return data;
}
以及匹配的 DataSerializer
实现 JsonSerializer< Data>
:
DataSerializer
通过以下方式注册到杰克逊:
And a matching DataSerializer
that implements JsonSerializer<Data>
:
The DataSerializer
is registered to Jackson via :
simpleModule.addSerializer(Data.class , dataSerializer);
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(simpleModule);
效果很好。
但是今天,我想添加另一个 Locale
参数,并希望 DataSerializer
输出相应的内容:
It works well.
But today , I want to add another Locale
parameter , and hope the DataSerializer
to output correspondent content :
@GET
public Data getData(@QueryParam("id") Long id , @QueryParam("locale") Locale locale)
'数据
'本身包含各种语言环境变体,我希望获得指定的语言环境输出。
The 'Data
' itself contains various locale variations , and I hope to get the assigned locale output.
但是当我得到语言环境
从参数,我不知道如何将 locale
值传递给 DataSerializer
...
But when I get the locale
from the parameter , I don't know how to pass the locale
value to the DataSerializer
…
无论如何都要实现这个目标吗?
Is there anyway to achieve this ?
除了这个解决方案:
Data data = dataService.getData(id.get() , locale);
这不是我想要的。
似乎 ThreadLocal
是实现这一目标的唯一方法,但我觉得这很难看。还有其他任何可行的解决方案吗?
It seems ThreadLocal
is the only way to achieve this , but I feel that is ugly. Any other feasible solutions ?
谢谢。
环境:dropwizard-0.7.0-rc2,jackson-core :jar:2.3.1
Environments : dropwizard-0.7.0-rc2 , jackson-core:jar:2.3.1
=====================更新======= ===
===================== updated ==========
回复@ andrei-i:
reply to @andrei-i :
因为我的数据本身已包含各种语言环境版本。
例如:
Because my data itself already contains various locale versions. for example :
Data helloData = dataService.get("hello");
helloData.getName(Locale.English) == "Hello";
helloData.getName(Locale.France) == "Bonjour";
helloData.getName(Locale.Germany) == "Hallo";
我想直接将语言环境从URL传递给JsonSerializer,以获得一个版本的数据表示。
I want to directly pass the locale from URL to JsonSerializer , to get one version of the data presentation.
并且'可能'还有其他版本(不仅仅是语言环境),因此,不考虑继承数据混合区域设置。
And there 'may' be other version (not just locale) , so , inheriting Data mixing Locale is not considered.
推荐答案
我知道这不是一个新问题,但这是我遇到的类似问题:
I know that this is not a new question but here is what I came up with facing the similar problem:
-
创建自定义注释:
created custom annotation:
@Target({ ElementType.FIELD, ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface JsonLocalizable {
public String localizationKey();
}
Jackson序列化器:
Jackson serializer:
public class LocalizingSerializer extends StdSerializer<String> implements ContextualSerializer {
private String localizationKey;
public LocalizingSerializer() {
super(String.class);
}
public LocalizingSerializer(String key) {
super(String.class);
this.localizationKey = key;
}
@Override
public void serialize(String value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
String localizedValue = //.... get the value using localizationKey
jgen.writeString(localizedValue);
}
@Override
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException {
String key = null;
JsonLocalizable ann = null;
if (property != null) {
ann = property.getAnnotation(JsonLocalizable.class);
}
if (ann != null) {
key = ann.localizationKey();
}
//if key== null??
return new LocalizingSerializer(key);
}
}
注释要本地化的字段:
Annotate the field you want to localize:
public class TestClass {
@JsonSerialize(using = LocalizingSerializer.class)
@JsonLocalizable(localizationKey = "my.key")
private String field;
public String getField() {
return this.field;
}
public void setField(String field) {
this.field = field;
}
}
这篇关于如何将参数传递给JsonSerializer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!