使用 DateTimeFormat.ISO.DATE 配置 Jackson - 不工作
Configure Jackson instead using DateTimeFormat.ISO.DATE - not working
我想在每次请求约会时使用 DateTimeFormat.ISO.DATE 配置 Jackson,例如:
@RequestMapping(value = "income")
public ResponseEntity calculateIncome(
@RequestParam(value = "companyName") String companyName,
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
@RequestParam(value = "startDate") LocalDate startDate,
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
@RequestParam(value = "endDate") LocalDate endDate
)
我已经尝试在 JacksonConfig 中设置它
mapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
或
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
还有
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
或
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
即使在 application.properties 我也尝试过
spring.jackson.serialization.write_dates_as_timestamps=true
我正在使用 spring-boot 机智以下依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.10.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>
我只是不想一遍又一遍地重复相同的@DataTimeFormat
但没有它,我仍然会收到错误消息:
在 IntelJ 中
2018-03-01 15:35:05.539 WARN 8168 --- [nio-8080-exec-1]
.w.s.m.s.DefaultHandlerExceptionResolver : Failed to bind request
element:
org.springframework.web.method.annotation.MethodArgumentTypeMismatchException:
Failed to convert value of type 'java.lang.String' to required type
'java.time.LocalDate'; nested exception is
org.springframework.core.convert.ConversionFailedException: Failed to
convert from type [java.lang.String] to type
[@org.springframework.web.bind.annotation.RequestParam
java.time.LocalDate] for value '2018-02-28'; nested exception is
java.lang.IllegalArgumentException: Parse attempt failed for value
[2018-02-28]
邮递员
{
"timestamp": 1519914905555,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.web.method.annotation.MethodArgumentTypeMismatchException",
"message": "Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam java.time.LocalDate] for value '2018-02-28'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2018-02-28]",
"path": "/incVat"
}
或
{
"timestamp": "2018-03-01T15:36:44.823+0000",
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.web.method.annotation.MethodArgumentTypeMismatchException",
"message": "Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam java.time.LocalDate] for value '2018-02-28'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2018-02-28]",
"path": "/incVat"
}
您的pom.xml需要这个:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
在你的配置中 class:
@Configuration
public class WebConfigurer {
@Bean
@Primary // pay attention on this
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.build();
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
return objectMapper;
}
}
以及 Jackson2Json 配置:
@Configuration
public class Jackson2JsonConfiguration {
@Bean
public Jackson2JsonObjectMapper jackson2JsonObjectMapper(ObjectMapper objectMapper) {
return new Jackson2JsonObjectMapper(objectMapper);
}
}
如果不行,试试:
- 更改 pom.xml 中
jsr310
依赖项的导入顺序。
- 在属性文件中添加:
jackson.serialization.write-dates-as-timestamps = false
我在尝试使这项工作也遇到了奇怪的问题。
作为最后一个选项,您可以迁移到 Spring Boot 2.0,因为他们翻转了 Jackson 配置默认值,将 JSR-310 日期写为 ISO-8601 字符串。
解决方案:
我找到了解决方案 here
这都是关于 LocalDate
的自定义编辑器和 Formatter
的实现
@Bean
public Formatter<LocalDate> localDateFormatter() {
return new Formatter<LocalDate>() {
@Override
public LocalDate parse(String text, Locale locale) throws ParseException {
return LocalDate.parse(text, DateTimeFormatter.ISO_DATE);
}
@Override
public String print(LocalDate object, Locale locale) {
return DateTimeFormatter.ISO_DATE.format(object);
}
};
}
我想在每次请求约会时使用 DateTimeFormat.ISO.DATE 配置 Jackson,例如:
@RequestMapping(value = "income")
public ResponseEntity calculateIncome(
@RequestParam(value = "companyName") String companyName,
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
@RequestParam(value = "startDate") LocalDate startDate,
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
@RequestParam(value = "endDate") LocalDate endDate
)
我已经尝试在 JacksonConfig 中设置它
mapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
或
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
还有
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
或
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
即使在 application.properties 我也尝试过
spring.jackson.serialization.write_dates_as_timestamps=true
我正在使用 spring-boot 机智以下依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.10.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>
我只是不想一遍又一遍地重复相同的@DataTimeFormat 但没有它,我仍然会收到错误消息:
在 IntelJ 中
2018-03-01 15:35:05.539 WARN 8168 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to bind request element: org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam java.time.LocalDate] for value '2018-02-28'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2018-02-28]
邮递员
{
"timestamp": 1519914905555,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.web.method.annotation.MethodArgumentTypeMismatchException",
"message": "Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam java.time.LocalDate] for value '2018-02-28'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2018-02-28]",
"path": "/incVat"
}
或
{
"timestamp": "2018-03-01T15:36:44.823+0000",
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.web.method.annotation.MethodArgumentTypeMismatchException",
"message": "Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam java.time.LocalDate] for value '2018-02-28'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2018-02-28]",
"path": "/incVat"
}
您的pom.xml需要这个:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
在你的配置中 class:
@Configuration
public class WebConfigurer {
@Bean
@Primary // pay attention on this
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.build();
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
return objectMapper;
}
}
以及 Jackson2Json 配置:
@Configuration
public class Jackson2JsonConfiguration {
@Bean
public Jackson2JsonObjectMapper jackson2JsonObjectMapper(ObjectMapper objectMapper) {
return new Jackson2JsonObjectMapper(objectMapper);
}
}
如果不行,试试:
- 更改 pom.xml 中
jsr310
依赖项的导入顺序。 - 在属性文件中添加:
jackson.serialization.write-dates-as-timestamps = false
我在尝试使这项工作也遇到了奇怪的问题。
作为最后一个选项,您可以迁移到 Spring Boot 2.0,因为他们翻转了 Jackson 配置默认值,将 JSR-310 日期写为 ISO-8601 字符串。
解决方案:
我找到了解决方案 here
这都是关于 LocalDate
的自定义编辑器和 Formatter
@Bean
public Formatter<LocalDate> localDateFormatter() {
return new Formatter<LocalDate>() {
@Override
public LocalDate parse(String text, Locale locale) throws ParseException {
return LocalDate.parse(text, DateTimeFormatter.ISO_DATE);
}
@Override
public String print(LocalDate object, Locale locale) {
return DateTimeFormatter.ISO_DATE.format(object);
}
};
}