Spring 将日期时间格式从web服务转换为字符串
这是我的POM.xml。。错误正在覆盖joda时间的托管版本2.8.2Spring 将日期时间格式从web服务转换为字符串,spring,mongodb,rest,datetime,Spring,Mongodb,Rest,Datetime,这是我的POM.xml。。错误正在覆盖joda时间的托管版本2.8.2 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 ht
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-rest-service-cors</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
我的模型课:
public class Record {
@Id private Long id;
private String cameraid;
private DateTime timestamp;
private String filename;
public Record(Long id,String cameraid, DateTime timestamp, String filename) {
this.id = id;
this.cameraid = cameraid;
this.timestamp = timestamp;
this.filename = filename;
}
我的控制器类:
@Autowired
RecordRepository rep;
@RequestMapping(value="list")
public List<Record> getList() {
List<Record> recordList = rep.findAll();
return recordList;
@Autowired
记录库代表;
@请求映射(value=“list”)
公共列表getList(){
List recordList=rep.findAll();
返回记录列表;
MongoRepository类:
import org.springframework.data.mongodb.repository.MongoRepository;
public interface RecordRepository extends MongoRepository<Record, String> {
}
import org.springframework.data.mongodb.repository.MongoRepository;
公共接口RecordRepository扩展了MongoRepository{
}
如何在web服务上以这种格式(2016-07-16T19:20:30+01:00)显示时间戳?有两种方法可以实现这一点
public class CustomDateTimeSerializer extends JsonSerializer<DateTime> {
// Customize format as per your need
private static DateTimeFormatter formatter = DateTimeFormat
.forPattern("yyyy-MM-dd'T'HH:mm:ss");
@Override
public void serialize(DateTime value, JsonGenerator generator,
SerializerProvider serializerProvider)
throws IOException {
generator.writeString(formatter.print(value));
}
}
@Configuration
public class JacksonConfiguration {
@Bean
public JodaModule jacksonJodaModule() {
final JodaModule module = new JodaModule();
module.addSerializer(DateTime.class, new CustomDateTimeSerializer());
return module;
}
}
使用WebBinder API和属性编辑器支持
实现属性编辑器支持
public class DateTimeEditor extends PropertyEditorSupport {
private final DateTimeFormatter formatter;
public DateTimeEditor(String dateFormat) {
this.formatter = DateTimeFormat.forPattern(dateFormat);
}
public String getAsText() {
DateTime value = (DateTime) getValue();
return value != null ? value.toString(formatter) : "";
}
public void setAsText( String text ) throws IllegalArgumentException {
if ( !StringUtils.hasText(text) ) {
// Treat empty String as null value.
setValue(null);
} else {
setValue(new DateTime(formatter.parseDateTime(text)));
}
}
}
将此PropertyEditor添加到Rest控制器
@RestController
@RequestMapping("/abc")
public class AbcController {
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(DateTime.class, new DateTimeEditor("yyyy-MM-dd", false));
}
}
您正在使用哪个JSON转换器/框架将Pojo转换为JSON,例如Jackson、Gson或其他任何东西?您还可以告诉我您正在使用哪个DateTime类吗?它来自使用JSON的Jodai,而我使用joda Time。它可能很简单,只需向您的项目添加一个依赖项。如果它是Spring Boot应用程序,它将自动注册y、 如果没有,您可能需要自己注册模块。@Jesper在我添加了依赖项之后,下一步该怎么办?对不起,我是spring的新手。在JodaModule上,我有一个错误。它不作为类型旋转,它是jackson数据类型的一部分-joda@KevinKai
JodaModule
在中,您必须将其作为依赖项添加到项目中。是的,我添加了。是吗版本有什么不同吗?可能有。您使用的是哪个版本
@RestController
@RequestMapping("/abc")
public class AbcController {
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(DateTime.class, new DateTimeEditor("yyyy-MM-dd", false));
}
}