javajsonspringspring-bootcassandra

Why does Spring Boot Controller give JSON but empty structure [{},{}]


I have a spring boot application with the following controller

package com.training.casapp.controller;

import com.training.casapp.entity.Student;
import com.training.casapp.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class StudentController {

   @Autowired
    StudentRepository studentRepository;

    @GetMapping(value = "/studentslist", produces = MediaType.APPLICATION_JSON_VALUE)
    List<Student> getStudentsList() {
        List<Student> studentsList = studentRepository.findAll();
        return studentsList;
    }
}

and the CassandraRepository as

package com.training.casapp.repository;

import com.training.casapp.entity.Student;
import org.springframework.data.cassandra.repository.CassandraRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface StudentRepository extends CassandraRepository<Student, Integer> {}

The Student Class is ddefined as follows

package com.training.casapp.entity;

import org.springframework.data.cassandra.core.cql.PrimaryKeyType;
import org.springframework.data.cassandra.core.mapping.Column;
import org.springframework.data.cassandra.core.mapping.PrimaryKeyColumn;
import org.springframework.data.cassandra.core.mapping.Table;

@Table("student")
public class Student {

    @PrimaryKeyColumn(type = PrimaryKeyType.PARTITIONED) private Integer student_id;
    @Column private String student_city;
    @Column private Integer student_fees;
    @Column private String student_name;
    @Column private Long student_phone;
    
}

I have 2 records in student table

enter image description here

however, when I access http://localhost:8081/casapp/studentslist I get following response in browser

[{},{}]

Even though I have configured everything correctly, for some reason it seems that the JSON conversion is not happening properly. I mean I can see 2 braces in the http output. The single braces pair {} is corresponding to a student. However, still this is not the output that I have desired. Any idea whats causing this ?

I have also tried overriding the toString() method. Didn't help.


Solution

  • Jackson serializer by default does not serialize private fields without accessor methods.

    Either: