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
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.
CodePudding user response:
Jackson serializer by default does not serialize private fields without accessor methods.
Either:
- make fields available (make it public or add getters)
- configure
ObjectMapper
used by jackson to also serialize private fields (for details you can reference this link: https://www.baeldung.com/spring-boot-customize-jackson-objectmapper)
CodePudding user response:
Answer
Even though I have configured everything correctly, for some reason it seems that the JSON conversion is not happening properly.
This should be the solution
// Defining the table name into DB - OK
@Table("student")
public class Student {
// Defining the id column w/type - OK
// ...
// Defining a private field per column - OK
// ...
// Defining public empty constructor - Missing!
public Student() {}
// Defining public getters and setters for each field - Missing!
public String getStudent_city() {
return student_city;
}
public String setStudent_city(String student_city) {
this.student_city = student_city;
}
// ...
}
when you create JPA entities, in addition to what you did, you must also set the empty constructor and public getters/setters
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 ?
In your case, if you notice, it gets the data from the database correctly but, since there are no public get and set methods, Hibernate fails to do the mapping between the local object and tables in the db and therefore does not allow the data to be stored locally (and then shown) correctly
I hope I was clear, let me know how it goes! :)