码迷,mamicode.com
首页 > 编程语言 > 详细

解决springboot jpa Could not write JSON: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer

时间:2020-03-10 01:27:00      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:pat   ext   jdbc   name   message   timezone   mes   jackson   port   

出现问题 

{
    "timestamp": 1583769258574,
    "status": 500,
    "error": "Internal Server Error",
    "exception": "org.springframework.http.converter.HttpMessageNotWritableException",
    "message": "Could not write JSON: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: springboot.learn.entities.User_$$_jvst863_0[\"handler\"])",
    "path": "/user/1",
    "company": "mg",
    "ext": null
}

解决问题 

原因

因为jsonplugin用的是Java的内审机制.hibernate会给被管理的pojo加入一个hibernateLazyInitializer属性,jsonplugin会把hibernateLazyInitializer也拿出来操作,并读取里面一个不能被反射操作的属性就产生了这个异常.

package springboot.learn.entities;


import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import javax.persistence.*;

//使用JPA 注解配置映射关系
@Entity//告诉JPA这是一个实体类 (和数据表映射的类)
@Table(name = "tpl_user")
@JsonIgnoreProperties({"hibernateLazyInitializer","handler"})// 忽略json配置
public class User {

 application.properties的配置

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://39.105.167.131:3306/smile_boot?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
    username: root
    password: Nrblwbb7$

  jpa:
    properties:
      hibernate:
        hbm2ddl:
          auto: update #数据库表的migration
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect #规定数据库未innodb型
        format_sql: true
    show-sql: true#在控制台打印sql

 

解决springboot jpa Could not write JSON: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer

标签:pat   ext   jdbc   name   message   timezone   mes   jackson   port   

原文地址:https://www.cnblogs.com/guokefa/p/12452651.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!