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

problem appear 

{
    "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
}

Solve the problem 

the reason

Because jsonplugin use the Java internal audit mechanism .hibernate will be managed pojo join a hibernateLazyInitializer property, jsonplugin hibernateLazyInitializer will also operate out and read a property which can not be reflected operation gave rise to this exception.

Package Penalty for springboot.learn.entities; 


Import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 

Import the javax.persistence *. ; 

// use JPA annotations mapping an 
@Entity // tell this is a JPA entity class (data tables and maps class) 
the @Table (name = "tpl_user" ) 
@JsonIgnoreProperties ({ "hibernateLazyInitializer", "Handler" }) // ignore json configuration
 public  class the User {

 application.properties configuration

the Spring: 
  the DataSource: 
    Driver-class-name: com.mysql.cj.jdbc.Driver 
    url: jdbc: MySQL: //39.105.167.131:? 3306 / smile_boot serverTimezone UTC = & useUnicode = to true & characterEncoding = UTF-8 & useSSL = to true 
    username : root 
    password: Nrblwbb7 $ 

  JPA: 
    the Properties: 
      Hibernate: 
        hbm2ddl: 
          Auto: Migration Update # database tables 
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect # specified database is not innodb type 
        format_sql: to true 
    Show-SQL: # printed on the console to true sql

 

Guess you like

Origin www.cnblogs.com/guokefa/p/12452651.html