我通过将默认时间戳格式设置为 yyyy-MM-dd HH:mm:ss z
配置了我的应用程序@Configuration
@EnableWebMvc
public class KukunWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter {
public MappingJackson2HttpMessageConverter jacksonJsonMessageConverter() {
MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper mapper = new ObjectMapper();
// Registering Hibernate5Module to support lazy objects
mapper.registerModule(new Hibernate5Module());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"));
messageConverter.setObjectMapper(mapper);
return messageConverter;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(jacksonJsonMessageConverter());
super.configureMessageConverters(converters);
}
}
但是我可以通过请求正文传递日期字段,该字段给出 400错误的请求。
流程实体字段:
@Entity
@Table(name = "process")
public class Process{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "process_id")
private Long processId;
@Column(name = "process_name")
@NotNull
private String processname;
@Column(name = "process_date")
@Temporal(TemporalType.DATE)
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date processDate;
//other fields and setter&getter methods
}
控制器方法:
@PostMapping
public ResponseEntity<Response> createProcess(
@RequestBody Process process) throws GenericException {
}
请求正文:
{
"processDate":"2019-03-30"
}
通过配置设置默认时间戳时,如何通过请求正文传递日期值?
答案 0 :(得分:1)
让我们看一下@JsonFormat注释以控制 整个类的日期格式在单个类上而不是全局上 应用程序:
define