使用 Dropwizard 时配置 Jackson 映射器
Configuring Jackson mapper when using Dropwizard
我想使用 Java 8 java.time with Jersey/Jackson in the context of a Dropwizard app. I understand I need to use jackson-modules-java8 并配置映射器对象。
但是我该如何配置 Jersey 的自动映射器来为我反序列化传入的 JSON? IE。我该去哪里 mapper.registerModule(new JavaTimeModule());
?
为了说明目前的情况,这里有一个例子 class 表示传入 JSON:
public class Example {
// Want to use java.time instead
private Date date;
private final String ISO_OFFSET_DATE_TIME = "YYYY-MM-DD'T'HH:mm:ssZ";
@JsonCreator
public Example(@JsonProperty("date")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = ISO_OFFSET_DATE_TIME)
Date date) {
this.date = date;
}
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = ISO_OFFSET_DATE_TIME)
public Date getDate() {
return date;
}
}
如您所见,它使用了较旧的日期 API。 Jersey 资源如下所示:
@Path("/example")
@Consumes(MediaType.APPLICATION_JSON)
public class ExampleResource {
@POST
public void consume(Example example) {
// Do stuff with example.date
}
}
JavaTimeModule
是 registered by default in Dropwizard 1.0.0 and above. For previous versions, the dropwizard-java8 bundle provided support for Java 8 features. Java 8 is the baseline for Dropwizard 1.0.0, and the bundle was merged into baseline.
假设您使用 Dropwizard 1.0.0 或更高版本,如果您仍然需要访问 ObjectMapper
,您可以在 Application<T>
:
- 在方法
void initialize(Bootstrap<T> bootstrap)
中,通过 bootstrap.getObjectMapper()
- 在方法
abstract void run(T configuration, Environment environment)
中,通过 environment.getObjectMapper()
这样,您就可以注册其他模块,或者启用或禁用 Jackson 功能。其中一些影响 how Java 8 types are serialized and deserialized。
我想使用 Java 8 java.time with Jersey/Jackson in the context of a Dropwizard app. I understand I need to use jackson-modules-java8 并配置映射器对象。
但是我该如何配置 Jersey 的自动映射器来为我反序列化传入的 JSON? IE。我该去哪里 mapper.registerModule(new JavaTimeModule());
?
为了说明目前的情况,这里有一个例子 class 表示传入 JSON:
public class Example {
// Want to use java.time instead
private Date date;
private final String ISO_OFFSET_DATE_TIME = "YYYY-MM-DD'T'HH:mm:ssZ";
@JsonCreator
public Example(@JsonProperty("date")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = ISO_OFFSET_DATE_TIME)
Date date) {
this.date = date;
}
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = ISO_OFFSET_DATE_TIME)
public Date getDate() {
return date;
}
}
如您所见,它使用了较旧的日期 API。 Jersey 资源如下所示:
@Path("/example")
@Consumes(MediaType.APPLICATION_JSON)
public class ExampleResource {
@POST
public void consume(Example example) {
// Do stuff with example.date
}
}
JavaTimeModule
是 registered by default in Dropwizard 1.0.0 and above. For previous versions, the dropwizard-java8 bundle provided support for Java 8 features. Java 8 is the baseline for Dropwizard 1.0.0, and the bundle was merged into baseline.
假设您使用 Dropwizard 1.0.0 或更高版本,如果您仍然需要访问 ObjectMapper
,您可以在 Application<T>
:
- 在方法
void initialize(Bootstrap<T> bootstrap)
中,通过bootstrap.getObjectMapper()
- 在方法
abstract void run(T configuration, Environment environment)
中,通过environment.getObjectMapper()
这样,您就可以注册其他模块,或者启用或禁用 Jackson 功能。其中一些影响 how Java 8 types are serialized and deserialized。