尝试从控制器类输出JSON响应时出现以下错误。
org.springframework.http.converter.HttpMessageNotWritableException: 无法编写JSON:属性的getter定义冲突 “ product_id”: com.kd.hub.core.entities.PosProduct#getProductId(0 params)与 com.kd.hub.core.entities.PosProduct#getProductName(0 params); 嵌套异常为 com.fasterxml.jackson.databind.JsonMappingException:发生冲突 属性“ product_id”的获取方法定义: com.kd.hub.core.entities.PosProduct#getProductId(0 params)与 com.kd.hub.core.entities.PosProduct#getProductName(0 params)at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:296)
我的Product.kt文件
@Entity
@Table(name = "sm_pos_product")
data class PosProduct(
@Id @GeneratedValue(strategy = GenerationType.AUTO) @JsonProperty("product_id") var productId : UUID = UUID.randomUUID(),
@Column(name = "product_name") @JsonProperty("product_id") var productName : String ?= null,
@Column(name = "strength") @JsonProperty("strength") var strength : String? = null,
@Column(name = "manufacturer") @JsonProperty("manufacturer") var manufacturer : String? = null,
@Column(name = "display_name") @JsonProperty("display_name") var displayName : String? = null,
// Stock Keeping Units
@Column(name = "sku_identifier") @JsonProperty("sku_identifier") var skuName : String? = null,
@Column(name = "sku_id") @JsonProperty("sku_id") var skuID : String ?= null,
// Master Stock
@Column(name = "sellable_stock") @JsonProperty("sellable_stock") var sellableStock : Double ?= 0.0,
@Column(name = "returned_stock") @JsonProperty("returned_stock") var returnedStock : Double ?= 0.0,
@Column(name = "expired_stock") @JsonProperty("expired_stock") var expiredStock : Double ?= 0.0
);
答案 0 :(得分:2)
您在这里有错字:
@Column(name = "product_name") @JsonProperty("product_id") var productName : String ?= null,
应该为@JsonProperty("product_name")
答案 1 :(得分:2)
您在两个字符串上将@JsonProperty
设置为“ product_id”,从而导致名称冲突。您需要更改其中之一。受影响的两个变量是
@Id @GeneratedValue(strategy = GenerationType.AUTO) @JsonProperty("product_id") var productId : UUID = UUID.randomUUID(),
@Column(name = "product_name") @JsonProperty("product_id") var productName : String ?= null,