Java 使用 JaxRS 自定义 JSON 序列化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24489186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Customize JSON serialization with JaxRS
提问by Olivier Grégtheitroade
In a webservice call, I would like to return my objects with this JSON structure.
在 Web 服务调用中,我想使用此 JSON 结构返回我的对象。
{
"date" : "30/06/2014",
"price" : {
"val" : "12.50",
"curr" : "EUR"
}
}
I'd like to map this JSON code to this Java structure (with joda-timeand joda-money):
我想将此 JSON 代码映射到此 Java 结构(使用joda-time和joda-money):
public class MyResponse {
LocalDate date;
Money price;
}
My webservice currently looks like this:
我的网络服务目前看起来像这样:
@javax.ws.rs.POST
@javax.ws.rs.Path("test")
@javax.ws.rs.Produces({MediaType.APPLICATION_JSON})
@javax.ws.rs.Consumes({MediaType.APPLICATION_JSON})
public MyResponse test(MyRequest request) {
MyResponse response = new MyResponse();
response.setDate(LocalDate.now());
response.setMoney(Money.parse("EUR 12.50"));
return response;
}
So my question is: where do I register a custom handler to format dates as I want as well as money representations?
所以我的问题是:我在哪里注册自定义处理程序以根据需要格式化日期以及货币表示?
采纳答案by lefloh
If you are using Hymanson(which should be the default for JBoss EAP 6) you can use custom JsonSerializers
如果您使用Hymanson(这应该是 JBoss EAP 6 的默认设置),您可以使用自定义JsonSerializers
For the LocalDate
:
对于LocalDate
:
public class DateSerializer extends JsonSerializer<LocalDate> {
@Override
public void serialize(LocalDate date, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
jgen.writeString(date.toString("dd/MM/yyyy"));
}
}
For the Money
:
对于Money
:
public class MoneySerializer extends JsonSerializer<Money> {
@Override
public void serialize(Money money, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
jgen.writeStartObject();
jgen.writeStringField("val", money.getAmount().toString());
jgen.writeStringField("curr", money.getCurrencyUnit().getCurrencyCode());
jgen.writeEndObject();
}
}
Both Serializers can be registered globally:
两个序列化器都可以全局注册:
@Provider
public class HymansonConfig implements ContextResolver<ObjectMapper> {
private ObjectMapper objectMapper;
public HymansonConfig() {
objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule("MyModule", new Version(1, 0, 0, null));
module.addSerializer(Money.class, new MoneySerializer());
module.addSerializer(LocalDate.class, new DateSerializer());
objectMapper.registerModule(module);
}
public ObjectMapper getContext(Class<?> objectType) {
return objectMapper;
}
}
For parsing JSON in this custom format you need to implement custom JsonDeserializers.
要以这种自定义格式解析 JSON,您需要实现自定义JsonDeserializers。
If you are using Jettisonyou can do the same thing with custom XmlAdapters.
如果您使用Jettison,您可以使用自定义XmlAdapters做同样的事情。