我正在使用 Spring MVC 3.2RC1 中的 REST API。
我正在获取一个带有 org.joda.time.DateTime 时间戳的 JPA 实体,并让 Spring 使用
将其序列化为 JSON@RequestMapping(value = "/foobar", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
在 Spring 中使用默认的 Jackson2 设置,因为我只添加了
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.1.1</version>
</dependency>
到我的 POM 并让 Spring 自行连接。
Controller 正在生成:
"created":{"year":2012,"dayOfMonth":30,"dayOfWeek":5,"era":1,"dayOfYear":335,"weekOfWeekyear":48,"weekyear":2012,"monthOfYear":11,"yearOfEra":2012,"yearOfCentury":12,"centuryOfEra":20,"millisOfSecond":39,"millisOfDay":52684039,"secondOfMinute":4,"secondOfDay":52684,"minuteOfHour":38,"minuteOfDay":878,"hourOfDay":14,"millis":1354282684039,"zone":{"uncachedZone":{"cachable":true,"fixed":false,"id":"Europe/Stockholm"},"fixed":false,"id":"Europe/Stockholm"},"chronology":{"zone":{"uncachedZone":{"cachable":true,"fixed":false,"id":"Europe/Stockholm"},"fixed":false,"id":"Europe/Stockholm"}},"afterNow":false,"beforeNow":true,"equalNow":false}
但我希望它是 ISO8601 日期,例如 2007-11-16T20:14:06.3Z(或带有偏移量)。
我的猜测是我需要访问 ObjectMapper 并设置 mapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);但是在使用时如何访问 ObjectMapper
<mvc:annotation-driven />
附:我使用 UserType 使用 JPA/Hibernate4 将对象持久化到 PostgreSQL 以获得 JodaTime 支持。 D.S.
更新
下面的配置解决了 java.util.Date 的问题,但 JodaTime 仍然没有骰子。
<annotation-driven>
<message-converters>
<beans:bean
class="org.springframework.http.converter.StringHttpMessageConverter" />
<beans:bean
class="org.springframework.http.converter.ResourceHttpMessageConverter" />
<beans:bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<beans:property name="objectMapper">
<beans:bean
class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"
p:indentOutput="true" p:simpleDateFormat="yyyy-MM-dd'T'HH:mm:ss.SSSZ">
</beans:bean>
</beans:property>
</beans:bean>
</message-converters>
</annotation-driven>
最佳答案
或者,如果您更喜欢在 Java 中进行配置,它可能如下所示:
@Configuration
@EnableWebMvc
public class RestConfig extends WebMvcConfigurerAdapter {
private ObjectMapper objectMapper() {
Jackson2ObjectMapperFactoryBean bean = new Jackson2ObjectMapperFactoryBean();
bean.setIndentOutput(true);
bean.setSimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
bean.afterPropertiesSet();
ObjectMapper objectMapper = bean.getObject();
objectMapper.registerModule(new JodaModule());
return objectMapper;
}
private MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(objectMapper());
return converter;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(mappingJackson2HttpMessageConverter());
}
}
关于json - Spring 3.2RC1 中来自 JodaTime 的 jackson2 JSON ISO 8601 日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13700853/
我正在努力使我的Springrest应用程序可以处理xml和json响应,但添加Jaxb消息转换器似乎破坏了我的json映射。@BeanpublicMappingJackson2HttpMessageConverterjsonConverter(){MappingJackson2HttpMessageConverterconverter=newMappingJackson2HttpMessageConverter();SimpleModulesimpleModule=newSimpleModule();simpleModule.addSerializer(String.class,ne
我们如何仅用1个字段显示耗时。例如期间=1年4个月零5天==>结果="1年前"周期=3个月5天==>结果=“3个月前”周期=4小时5分钟==>结果=“4小时前”所以我只想要最高的可用字段。 最佳答案 Joda-Time中没有方法可以做到这一点,因此您必须手动依次测试每个字段。 关于java-JodaTimePeriodFormat,只有1个字段的耗时,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/q
我正在使用UserType3.0.0.RC1中的org.jadira.usertype.dateandtime.joda.PersistentDateTime类将JodaTimeDateTime映射到Hibernate。TheJavadocs对于类提到我想设置“databaseZone”和“jvmZone”属性,但我在UserType文档中找不到任何指示如何执行此操作的内容。我找到了thisthread这似乎暗示这些是由类似于以下内容的XML设置的:truejvm我尝试将这些添加到我的hibernate配置中,但这只会出错。有谁知道这个配置需要去哪里?谢谢。
我正在尝试编写一个简单的实用方法,用于将整数天数添加到Joda时间instant.这是我的第一次尝试。/***Addsanumberofdaysspecifiedtotheinstantintimespecified.**@paraminstant-thedatetobeaddedto*@paramnumberOfDaysToAdd-thenumberofdaystobeaddedtotheinstantspecified*@returnaninstantthathasbeenincrementedbythenumberofdaysspecified*/publicstaticInst
我需要将SimpleDateFormat更改为在jodatime中等效的其他格式。这是需要更改的代码。publicstaticStringdateFormat(Datedate,Stringformat){SimpleDateFormatsdf=newSimpleDateFormat(format);returnsdf.format(date);}我尝试过使用DateTimeFormatter。publicstaticStringdateFormat(DateTimedate,Stringformat){DateTimeFormatterdtf=DateTimeFormat.forPa
我正在尝试获取从那天开始算起的当前毫秒数。所以我想做以下计算。86400000-currMilliSecondsFromBeginningOfDay。任何帮助将不胜感激。谢谢 最佳答案 longresult=newDateTime().millisOfDay().getMillis();或longresult=newDateTime().getMillis()-newDateTime().withMillisOfDay(0).getMillis();或longresult=newLocalTime().get(DateTimeFie
LocalDatestartDate=newLocalDate(2014,1,2);LocalDateTimestartDateTime=newLocalDateTime(2014,1,2,14,0);我需要比较startDate和startDateTime的日期,如下所示://booleanequalDates=startDate.isequal(startDateTime.getDate());这有可能吗? 最佳答案 如果你只是想比较日期部分,你可以这样做:LocalDatestartDate=newLocalDate(2014
在EJB3和Spring3中,hibernate有很多共同点。我需要找出在哪里可以将Spring框架与hibernate而不是EJB3一起使用,反之亦然。 最佳答案 您可以互换使用它们。如果您使用EJB3,则必须拥有完整的JavaEE、EJB3应用服务器。有些是免费的,有些则不是。如果您使用Spring3,则需要在CLASSPATH中包含SpringJAR,但不需要完整的JavaEE应用程序服务器。Tomcat或Jetty就足够了,具体取决于您的需要。EJB3实现有多个供应商;毕竟,EJB3只是一个规范。Spring只有一个供应商。
我正在使用此代码来计算输入的过去日期和当前日期之间的月数。它使用JodaTimeLocalDatedate1=newLocalDate(installmentStartDate2);LocalDatedate2=newLocalDate(newjava.util.Date());PeriodTypemonthDay=PeriodType.yearMonthDayTime();Perioddifference=newPeriod(date1,date2,monthDay);intmonths=difference.getMonths();returnmonths+1;现在,当我输入201
您好,我正在使用Joda时间将我的字符串日期转换为DateTime对象。我目前有以下字符串:2014-02-16T00:17:20.000Z如何将其转换为DateTime对象?我试过:DateTimeFormatterformatter=DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZZZ");DateTimedt=formatter.parseDateTime("2014-02-16T00:17:20.000Z");但是我收到以下错误:java.lang.IllegalArgumentException:Invalidformat:"