无法编写JSON:无法反序列化;嵌套异常是

时间:2017-11-30 13:28:36

标签: json spring serialization kotlin

两种型号:

@Entity
@Table(name="products")
data class Product(
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        var id: Long = 0,
        var name: String = "",
        var price: Number = 0
)

@Entity
@Table(name="users")
data class User(
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        var id: Long = 0,
        var name: String = "",
        var email: String = "",
        @OneToMany(targetEntity = Product::class)
        @JoinColumn(name = "user_id")
        var products: List<Product> = listOf()
) {
    var password: String = ""
        @JsonIgnore
        get() = field
}

我的存储库:

@Repository
interface UserRepository : CrudRepository<User, Long> {
    fun findByEmail(email: String): User
}

在我的控制器中:

@GetMapping("/users")
    fun index(): MutableIterable<User> = repository.findAll()

当我访问http://localhost:8080/users时,我收到以下错误:

  

无法写入HTTP消息:   org.springframework.http.converter.HttpMessageNotWritableException:   无法编写JSON:无法反序列化;嵌套异常是   com.fasterxml.jackson.databind.JsonMappingException:不能   反序列化(通过参考链:   的java.util.ArrayList [0] - &GT; xxxxxxxxxxx.models.User [&#34;产品&#34;])

1 个答案:

答案 0 :(得分:0)

尝试使用

JpaRepository<User, Long>代替CrudRepository<User, Long>

List<User>代替MutableIterable<User>