gpt4 book ai didi

java - JSON 解析错误 - 发布 json 时数据类型错误

转载 作者:行者123 更新时间:2023-12-02 09:39:24 24 4
gpt4 key购买 nike

我有一个实体用户,它有两个数据类型的字段。

@Entity
@Table(name = "users", schema = "public")
public class User {

@Id
@Column(name = "user_id", updatable = false, nullable = false, unique = true)
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
private UUID id;
@Column(name = "name")
private String name;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd@HH:mm:ss")
@Column(name = "created_on")
@JsonIgnore
private Date createdOn;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd@HH:mm:ss")
@Column(name = "modified_on")
@JsonIgnore
private Date modifiedOn;

// construuctors, getter, setter
}

我有一个带有方法 save() 的 Controller ,它将有关用户的数据保存到 postgesql 数据库。

@RestController
@RequestMapping("/user")
public class UsersController {

@Autowired
private UsersService service;

@PostMapping("/save")
public void save(@RequestBody User user){
service.save(user);
}

}

我有一个 JSON,我将其发布到 Controller

{ "name":"Max", "createdOn":"2019-07-26 11:13:39", "modifiedOn":"2019-07-26 11:13:39" }

最后在执行过程中出现错误

"JSON parse error: Cannot deserialize value of type java.sql.Date from String \"2019-07-26 11:13:39\": not a valid representation (error: Failed to parse Date value '2019-07-26 11:13:39': Cannot parse date \"2019-07-26 11:13:39\": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ', parsing fails (leniency? null)); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type java.sql.Date from String \"2019-07-26 11:13:39\": not a valid representation (error: Failed to parse Date value '2019-07-26 11:13:39': Cannot parse date \"2019-07-26 11:13:39\": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ', parsing fails (leniency? null))\n at [Source: (PushbackInputStream); line: 3, column: 13] (through reference chain: com.example.api.entity.User[\"createdOn\"])"

最佳答案

尝试添加一个T:

2019-07-26T11:13:39

关于java - JSON 解析错误 - 发布 json 时数据类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57222438/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com