【发布时间】:2020-02-04 23:01:50
【问题描述】:
我在使用 Spring Boot 和 postgresql 处理布尔值时遇到了一些问题。我删除了一个字段,该字段在实体 bean 和数据库表中都定义为布尔值。看起来在选择抛出其余 API 时,我返回的是 JSON 字符而不是布尔值。在插入时,无论我尝试使用布尔值还是字符(即根据查询的响应构建正文),我都会收到错误消息。我在 OpenJDK 11.PostgreSQL JDBC 驱动程序 42.2.5 上运行。 Wildflt 15.
我已经尝试使用 select (get) 中返回的 char "t" 和 JSON boolean true。我依赖的json boolean的定义
https://json-schema.org/understanding-json-schema/reference/boolean.html
这是创建表
CREATE TABLE TRANSFER_PARTITIONED_IMAGE (
TRANSFER_PARTITIONED_IMAGE_ID INTEGER NOT NULL,
IMAGE_NAME VARCHAR(50) NOT NULL,
REQUESTED_PART_SIZE_MB INTEGER NOT NULL,
SIZE_BYTES INTEGER NOT NULL,
IMAGE_MD5_HASH VARCHAR(100),
NUMBER_PARTITIONS INTEGER,
DELETED BOOLEAN NOT NULL
);
这是我用来从 postgresql 命令行在表中插入一行的插入
crowbar=> insert into transfer_partitioned_image
values(45,'foofoobarbarbarbarsnafu',10,10,'aa',10,true);
选择时返回的 JSON。请注意,它为布尔字段返回一个字符“t”。
{
"transferPartitionedImageId": 45,
"imageName": "foofoobarbarbarbarsnafu",
"requestedPartSizeMB": 10,
"sizeBytes": 10,
"imageMD5Hash": "aa",
"numberPartitions": 10,
"deleted": "t"
}
如果我尝试使用“t”作为布尔值插入,我会收到服务器 500 错误
{
"timestamp": "2019-10-07T13:48:38.077+0000",
"message": "could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement",
"details": "uri=/crowbar/data/rit/transferPartitionedImage"
}
来自日志文件
2019-10-07 13:48:38,075 INFO [stdout] (default task-1) insert
2019-10-07 13:48:38,075 INFO [stdout] (default task-1) into
2019-10-07 13:48:38,075 INFO [stdout] (default task-1) transfer_partitioned_image
2019-10-07 13:48:38,075 INFO [stdout] (default task-1) (deleted, image_md5_hash, image_name, number_partitions, requested_part_size_mb, size_bytes)
2019-10-07 13:48:38,075 INFO [stdout] (default task-1) values
2019-10-07 13:48:38,075 INFO [stdout] (default task-1) (?, ?, ?, ?, ?, ?)
2019-10-07 13:48:38,076 DEBUG [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-1) could not execute statement [n/a]: org.postgresql.util.PSQLException: ERROR: column "deleted" is of type boolean but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
如果我使用 JSON 布尔值尝试相同的操作(尝试了大写和小写 TRUE)
{
"transferPartitionedImageId": 1111,
"imageName": "foofoobarfoofoobarsnafusnafu",
"requestedPartSizeMB": 10,
"sizeBytes": 10,
"imageMD5Hash": "aa",
"numberPartitions": 10,
"deleted": true
}
我收到 400 错误请求错误
Status Code: 400 Bad Request
Connection: keep-alive
Date: Mon, 07 Oct 2019 13:53:13 GMT
Transfer-Encoding: chunked
来自日志
019-10-07 13:53:13,160 DEBUG [org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver] (default task-1) Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.lang.Character` out of VALUE_TRUE token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.Character` out of VALUE_TRUE token
at [Source: (PushbackInputStream); line: 8, column: 14] (through reference chain: com.ticomgeo.crowbar.data.entity.TransferPartitionedImage["deleted"])]
我的实体 bean 定义如下所示
Entity Bean definition
@Entity
public class TransferPartitionedImage implements Serializable {
private static final long serialVersionUID = -3444719649254510891L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer transferPartitionedImageId;
@NotNull
@Size(min = 10, max = 50, message = "Image name must be 10 to 50 characters")
@Column(name = "IMAGE_NAME", nullable = false, length = 50)
private String imageName;
@NotNull
@Column(name = "REQUESTED_PART_SIZE_MB", nullable = false)
private Integer requestedPartSizeMB;
@Column(name = "SIZE_BYTES")
private Integer sizeBytes;
@Column(name = "IMAGE_MD5_HASH")
private String imageMD5Hash;
@Column(name = "NUMBER_PARTITIONS")
private Integer numberPartitions;
@Column (name = "DELETED")
private Boolean deleted;
public Integer getTransferPartitionedImageId() {
return transferPartitionedImageId;
}
public void setTransferPartitionedImageId(Integer transferPartitionedImageId) {
this.transferPartitionedImageId = transferPartitionedImageId;
}
public String getImageName() {
return imageName;
}
public void setImageName(String imageName) {
this.imageName = imageName;
}
public Integer getRequestedPartSizeMB() {
return requestedPartSizeMB;
}
public void setRequestedPartSizeMB(Integer requestedPartSizeMB) {
this.requestedPartSizeMB = requestedPartSizeMB;
}
public Integer getSizeBytes() {
return sizeBytes;
}
public void setSizeBytes(Integer sizeBytes) {
this.sizeBytes = sizeBytes;
}
public String getImageMD5Hash() {
return imageMD5Hash;
}
public void setImageMD5Hash(String imageMD5Hash) {
this.imageMD5Hash = imageMD5Hash;
}
public Integer getNumberPartitions() {
return numberPartitions;
}
public void setNumberPartitions(Integer numberPartitions) {
this.numberPartitions = numberPartitions;
}
public Boolean getDeleted() {
return deleted;
}
public void setDeleted(Boolean deleted) {
this.deleted = deleted;
}
public Boolean isDeleted() {
return deleted ;
}
我本来希望选择返回一个 JSON 布尔值,即
已删除:是的
而不是一个字符“t”。我还希望将选择返回的内容的示例粘贴到具有新主键的帖子正文中以对插入起作用。
【问题讨论】:
标签: json database postgresql hibernate spring-boot