I am facing an issue with the code shown below. The below code is generating the json as follows :
[{"id":123,"subjects":"English"},{"id":456,"subjects":"Maths"}]
We can see that the name tag is missing in generated JSON. I don't understand when I am returning Student List, the resulting json should contain the name tag since there is a property in the Student class of type "Name". Please help !
PS: I have taken the code in debug & I saw that "students arraylist" 0 index & index does contain the name prpoperty along with id & subject. But the JSON doesn't contain this property.
Restcontroller:
package com.example.demo;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
@SpringBootApplication
@RestController
public class Demo1Application {
@Autowired
Student student;
List<Student> students = new ArrayList<>();
@RequestMapping(value="/getStudents", method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
public List<Student> getstudent () {
students = student.setStudent();
return students;
}
public static void main(String[] args) {
SpringApplication.run(Demo1Application.class, args);
System.out.println("Hello");
}
}
Student Bean Class:
package com.example.demo;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
public class Student {
int id;
String Subject;
@Autowired
@Qualifier("name")
Name name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSubjects() {
return Subject;
}
public void setSubject(String subject) {
Subject = subject;
}
public void setName(String Fname, String Mname, String Lname) {
Name nm = new Name();
name = nm.setfullname(Fname,Mname,Lname) ;
}
public Student(int id, String Subject) {
this.id = id;
this.Subject = Subject;
}
public Student() {
System.out.println("Student Object created");
}
public List<Student> setStudent() {
List<Student> Students = new ArrayList<>();
Student s = new Student ();
Student s1 = new Student ();
s.setId(123);
s.setSubject("English");
s.setName("Hemant", "123", "Mamod");
s1.setId(456);
s1.setSubject("Maths");
s1.setName("Akshay", "123", "pandey");
Students.add(s);
Students.add(s1);
return Students;
}
}
Name Bean Class:
package com.example.demo;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
@Qualifier("name")
public class Name {
String Firstname;
String Middlename;
String Lastname;
public Name() {
System.out.println("Name Object created");
}
public String getFirstname() {
return Firstname;
}
public void setFirstname(String firstname) {
Firstname = firstname;
}
public String getMiddlename() {
return Middlename;
}
public void setMiddlename(String middlename) {
Middlename = middlename;
}
public String getLastname() {
return Lastname;
}
public void setLastname(String lastname) {
Lastname = lastname;
}
public Name setfullname(String Fname, String Mname, String Lname) {
Firstname = Fname;
Middlename = Mname;
Lastname = Lname;
return this;
}
}
The problem is that you have not getter of the Name
in the Student
class.
Please add the getter as follow:
@Component
public class Student {
int id;
String Subject;
Name name;
/******/
public Name getName() {
return name;
}
}
Speaking of NullPinterException
with a name
property - it means that it has not been autowired for some reasons, so in your case you can remove an @Autowired
and @Qualifier
annotations or figure out what is a problem with autowiring