0

I am trying to display a List of items in JSON format. My code structure utilizing SpringBoot and JPA Repository on Server side:

  1. Entity class
  2. Repository class created
  3. Service written (contains repository.findAll() function)
  4. Controller class

Goal is to output the record set extracted from SQL database onto localhost:8080/api/getinspection.

I have added Gson dependency in my pom.xml and in my controller class added code to convert to JSON.

I get an error saying:

java.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy.
Forgot to register a type adapter?

I have researched on type adapter on stackoverflow and tried to implement the solution, but in vain. Please help.

Service class

public List<INSPCTN> getInspections() {
 return inspctnRepository.findAll();  }

Controller Class

    @Service
     public class InspectionService {

    @Autowired
    INSPCTNRepository inspctnRepository;
         @GetMapping(path="/getInspection", produces = "application/JSON")
             public String getInspections() {
                   List<INSPCTN> list = inspectionService.getInspections();
                Gson gson = new Gson();
               String json = gson.toJson(list);
                  return json;
         }
    }

Expected result: List of records from the database in JSON format

Actual:

There was an unexpected error (type=Internal Server Error, status=500).

Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy.
Forgot to register a type adapter?

recnac
  • 3,744
  • 6
  • 24
  • 46
Pavani
  • 11
  • 2
  • 1
    Possible duplicate of [Could not serialize object cause of HibernateProxy](https://stackoverflow.com/questions/13459718/could-not-serialize-object-cause-of-hibernateproxy) – nortontgueno May 02 '19 at 14:40
  • The solution was to add spring.jackson.serialization.fail-on-empty-beans=false in the application.properties file. This link helped resolve the issue. https://stackoverflow.com/questions/24994440/no-serializer-found-for-class-org-hibernate-proxy-pojo-javassist-javassist – Pavani May 07 '19 at 12:27

0 Answers0