我正在使用springmvc和hibernate进行休息服务的示例,其中我以毫秒格式获取日期数据类型的日期响应,我通过谷歌搜索使用了下面的代码,但这些代码没有影响它的结果相同毫秒格式。下面是我使用的代码
controller.java
/* Getting List of objects in Json format in Spring Restful Services */
@RequestMapping(value = "/list", method = RequestMethod.GET)
public @ResponseBody List getDatalist() {
List DataList = null;
try {
DataList = dataServices.getDataEntityList();
} catch (Exception e) {
e.printStackTrace();
}
return DataList;
}
DataValueTable.java
@JsonAutoDetect
@Entity
@Table(name = "DataValueTable")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class DataValueTable implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "ID")
private long id;
@JsonSerialize(using=JsonDateSerializer.class)
@Column(name = "Datatype")
private Date dataType;
@Column(name = "Datacategory")
private String dataCategory;
@Column(name = "DataValue")
private boolean dataValue;
public Date getDataType() {
return dataType;
}
public void setDataType(Date dataType) {
this.dataType = dataType;
}
JsonDateSerializer .java
package com.beingjavaguys.model;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
import org.springframework.stereotype.Component;
/**
* Used to serialize Java.util.Date, which is not a common JSON
* type, so we have to create a custom serialize method;.
*
* @author Loiane Groner
* http://loianegroner.com (English)
* http://loiane.com (Portuguese)
*/
@Component
public class JsonDateSerializer extends JsonSerializer<Date>{
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
@Override
public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)
throws IOException, JsonProcessingException {
String formattedDate = dateFormat.format(date);
gen.writeString(formattedDate);
}
}
弹簧-config.xml中
<context:component-scan base-package="com.beingjavaguys.controller" />
<mvc:annotation-driven />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:sqlserver://localhost;database=Sample" />
<property name="username" value="sdsd" />
<property name="password" value="sdsd" />
<property name="hibernate.dialect" value="com.beingjavaguys.model.SqlServerDialectWithNvarchar" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.beingjavaguys.model.Employee</value>
<value>com.beingjavaguys.model.DataValueTable</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="dataDao" class="com.beingjavaguys.dao.DataDaoImpl"></bean>
<bean id="dataServices" class="com.beingjavaguys.services.DataServicesImpl"></bean>
</beans>
现在我得到了响应&#34; 12312121000&#34;。我希望它以02-02-2015格式显示。欢迎任何建议和答案。
答案 0 :(得分:2)
使用jackson,您可以注释实体,例如
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss.000 ", timezone="UTC")
public Timestamp getDate() {
return date;
}
这是我用来取回模式中时间戳的测试用例的一个例子。
ObjectMapper mapper = new ObjectMapper();
JaxbAnnotationModule module = new JaxbAnnotationModule();
mapper.registerModule(module);
mapper.setSerializationInclusion(Include.NON_NULL);
// Object to JSON - entityBean I want to map to JSON
String jsonOutput = mapper.writeValueAsString(entityBean);
请注意。您必须将带注释的JsonFormat分配给getter方法。