简体   繁体   English

Kotlin-无法编写JSON:属性的getter定义冲突(HttpMessageNotWritableException)

[英]Kotlin - Could not write JSON: Conflicting getter definitions for property (HttpMessageNotWritableException)

Im getting the following error while trying to output a JSON response from a controller class. 我在尝试从控制器类输出JSON响应时收到以下错误。

org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Conflicting getter definitions for property "product_id": com.kd.hub.core.entities.PosProduct#getProductId(0 params) vs com.kd.hub.core.entities.PosProduct#getProductName(0 params); org.springframework.http.converter.HttpMessageNotWritableException:无法编写JSON:属性“ product_id”的getter定义冲突:com.kd.hub.core.entities.PosProduct#getProductId(0 params)与com.kd.hub.core。 Entity.PosProduct#getProductName(0个参数); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Conflicting getter definitions for property "product_id": com.kd.hub.core.entities.PosProduct#getProductId(0 params) vs com.kd.hub.core.entities.PosProduct#getProductName(0 params) at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:296) 嵌套异常是com.fasterxml.jackson.databind.JsonMappingException:属性“ product_id”的获取方法定义冲突:com.kd.hub.core.entities.PosProduct#getProductId(0 params)与com.kd.hub.core.entities。 org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:296)上的PosProduct#getProductName(0个参数)

My Product.kt file 我的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
);

You have a typo here: 您在这里有错字:

@Column(name = "product_name") @JsonProperty("product_id") var productName : String ?= null,

Should be @JsonProperty("product_name") 应该是@JsonProperty("product_name")

You set the @JsonProperty to "product_id" on two of your strings causing there to be conflicting names. 您在两个字符串@JsonProperty设置为“ product_id”,从而导致名称冲突。 You need to change one of them. 您需要更改其中之一。 The two affected variables are 受影响的两个变量是

    @Id @GeneratedValue(strategy = GenerationType.AUTO) @JsonProperty("product_id")  var productId  : UUID = UUID.randomUUID(),
    @Column(name = "product_name") @JsonProperty("product_id") var productName : String ?= null,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用getter方法时出现错误(HttpMessageNotWritableException:无法写入JSON:bean类的无效属性'') - I am getting an error (HttpMessageNotWritableException: Could not write JSON: Invalid property '' of bean class) when using getter method 无法读取 JSON:属性的设置器定义冲突 - Could not read JSON: Conflicting setter definitions for property HttpMessageNotWritableException - 无法在连接表中写入 JSON - HttpMessageNotWritableException - Could not write JSON in Join Tables 如何在不访问源代码的情况下解决 jackson 中属性的冲突 getter 定义 - How to solve conflicting getter definitions for property in jackson without access to source HttpMessageNotWritableException:无法写入 JSON:将数据添加到数据库后无法初始化代理 - HttpMessageNotWritableException: Could not write JSON: Could not initialize proxy AFTER adding data into DB 无法写入HTTP消息:org.springframework.http.converter.HttpMessageNotWritableException:无法写入JSON:错误 - Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Error org.springframework.http.converter.HttpMessageNotWritableException:无法写入 JSON :(是 javaer.uLangException); 嵌套异常是 - org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: (was java.lang.NullPointerException); nested exception is org.springframework.http.converter.HttpMessageNotWritableException:无法写入JSON:指定的地图为空 - org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Specified map is empty 如何避免具有相同属性名称和不同数据类型的属性的 getter 定义冲突 - How to avoid the Conflicting getter definitions for property with same attribute name with different datatype 属性“ thenComparing”的setter定义冲突: - Conflicting setter definitions for property “thenComparing”:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM