Spring Boot中接收Web端Json对象,需要接收Date日期,将String转化为Date对象
众所周知,JSON数据传输过程中,只允许传输Integer和String类型的数据,但是如果后端使用@RequestBody接收一个实体类,实体类中对应的字段格式是Date,该怎么办呢?
原实体类:
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class data {
private Integer id;
private String string;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
这个用了lombok无法解决set和get问题,我还在解决中。
Controller文件:
import com.example.Entity.data;
import org.springframework.web.bind.annotation.*;
import com.example.Util.RespBean;
@RestController
public class test {
@RequestMapping(value = "/test",method = RequestMethod.POST)
public RespBean My_Test(@RequestBody data my_data){
try{
System.out.println(my_data);
return RespBean.success("测试成功",my_data);//成功返回
}catch (Exception e){
return RespBean.error("测试失败!",e.toString());//错误返回
}
}
}
首先,报错为:
2021-07-15 15:00:18.660 WARN 1664 — [nio-8888-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException:
JSON parse error: Cannot deserialize value of type java.util.Date
from String “2010-01-10 00:00:00”:
not a valid representation (error: Failed to parse Date value ‘2010-01-10 00:00:00’: Cannot parse date “2010-01-10 00:00:00”: while it seems to fit format ‘yyyy-MM-dd’T’HH:mm:ss.SSSX’, parsing fails (leniency? null));
nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type java.util.Date
from String “2010-01-10 00:00:00”:
not a valid representation (error: Failed to parse Date value ‘2010-01-10 00:00:00’: Cannot parse date “2010-01-10 00:00:00”: while it seems to fit format ‘yyyy-MM-dd’T’HH:mm:ss.SSSX’, parsing fails (leniency? null)) at [Source: (PushbackInputStream);
line: 4, column: 12] (through reference chain: com.example.old_care_system.Entity.data[“date”])]
这个错误主要是因为Web端传过来的JSON数据无法匹配实体中的Date参数的格式。
使用PostMan测试的接收结果为:
解决方案:
在对应的实体类中加入@JsonFormat(shape = JsonFormat.Shape.STRING, pattern=“yyyy-MM-dd hh:mm:ss”),这个注解是将JSON格式中对应字段的String数据转化为Date数据。如下:
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class data {
private Integer id;
private String string;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd hh:mm:ss")
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
加入该注解后就能合法接收数据了,Web端只需要将格式适配即可。
成功接收显示:
至于如何自定义返回类,可以观看我之前的文章。
到此就结束了,感谢各位观看。