我正在構建一個REST web服務。某些類具有DateTime
(JodaTime)類型的屬性。(德)與傑克遜序列化日期時間(喬達)
當發送該對象到我的客戶端(JavaScript)的,我的對象
private DateTime date;
轉化爲
{ date: { chronology: {}, millis: 1487289600000 } }
的問題是,我在發送對象時返回給服務器有一個錯誤因爲我不能實例化chronology
我想有類似{ date: 1487289600000}
- 任何其他格式可以工作。
環境
- 傑克遜 - 註解2.8.8
- 傑克遜核2.8.8
- 傑克遜 - databin 2.8.8
- 傑克遜 - 數據類型 - 約達2.8.8
- joda-time 2.7
我的上下文解析器
@Provider
public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {
final ObjectMapper mapper = new ObjectMapper();
public ObjectMapperContextResolver() {
mapper.registerModule(new JodaModule());
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
}
@Override
public ObjectMapper getContext(Class<?> type) {
return mapper;
}
}
我缺少什麼?如果我不使用ObjectMapperContextResolver
我有同樣的結果
更新以下@卡西奧Mazzochi莫林的回答
我加jackson-jaxrs-json-provider 2.8.8
,jackson-jaxrs-base 2.8.8
和jackson-module-jaxb-annotations 2.8.8
我的上下文解析器現在這個樣子
@Provider
public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {
final ObjectMapper mapper = new ObjectMapper();
public ObjectMapperContextResolver() {
mapper.registerModule(new JodaModule());
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
}
@Override
public ObjectMapper getContext(Class<?> type) {
return mapper;
}
}
和我的應用程序配置
@javax.ws.rs.ApplicationPath("/")
public class ApplicationConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new java.util.HashSet<>();
resources.add(AuthenticationFilter.class);
resources.add(CORSFilter.class);
resources.add(ObjectMapperContextResolver.class);
resources.add(JacksonJsonProvider.class);
resources.add(ServiceResource.class);
return resources;
}
}
我serviceResource
@Path("service")
public class ServiceResource {
@Path("/forecast/stocks/{modelId}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Value> getStocks(@PathParam("modelId") String modelId, @QueryParam("startDate") String startDate, @QueryParam("endDate") String endDate) {
try {
DateTime datetimeStart = formatStringToDatetime(startDate);
DateTime datetimeEnd = formatStringToDatetime(endDate);
return logicClass.getStocks(modelId, datetimeStart, datetimeEnd);
} catch (Exception e) {
logger.log(Level.SEVERE, "Error calling /hydromax/forecast/stocks", e);
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
}
}
}
而且Value
對象
public class Value {
private DateTime date;
private Float value;
public Value() {
}
//getter and setter
}
更新
我加入以下代碼裏面ApplicationConfig
@Override
public Map<String, Object> getProperties() {
Map<String, Object> props = new HashMap<>();
props.put("jersey.config.server.disableMoxyJson", true);
return props;
}
我DateTime
現在轉化爲
"date":{"dayOfMonth":16,"dayOfWeek":4,"era":1,"year":2017,"dayOfYear":47,"weekOfWeekyear":7,"secondOfMinute":0,"millisOfSecond":0,"weekyear":2017,"monthOfYear":2,"hourOfDay":10,"minuteOfHour":0,"yearOfEra":2017,"yearOfCentury":17,"centuryOfEra":20,"millisOfDay":36000000,"secondOfDay":36000,"minuteOfDay":600,"millis":1487235600000,"zone":{"fixed":false,"uncachedZone":{"fixed":false,"cachable":true,"id":"Europe/Paris"},"id":"Europe/Paris"},"chronology":{"zone":{"fixed":false,"uncachedZone":{"fixed":false,"cachable":true,"id":"Europe/Paris"},"id":"Europe/Paris"}},"afterNow":false,"beforeNow":true,"equalNow":false}
並在第一時間我稱之爲部署後的服務,我有這樣的錯誤
javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: com/fasterxml/jackson/module/jaxb/JaxbAnnotationIntrospector
你註冊了你的'ContextResolver'嗎? –
@CássioMazzochiMolin是的'resources.add(ObjectMapperContextResolver.class);' – Weedoze
對我來說,它的工作原理:'{「date」:「2017-02-17T00:00:00.000Z」} – 2017-06-20 12:06:21