Spring REST Controller returns JSON with empty data
14,460
I think you should use Lombok as class level instead of field level.
@Entity
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Employee implements Serializable {}
This may solve your problem.
Related videos on Youtube
Comments
-
Radziasss over 3 years
I have a simple Spring Boot web application. I'm trying to receive some data from server. The Controller returns a collection, but the browser receives empty JSON - the number of curly brackets is equals to the number of objects from server, but its content is empty.
@RestController public class EmployeeController { @Autowired private EmployeeManagerImpl employeeManagerImpl; @RequestMapping(path="/employees", method = RequestMethod.GET) public Iterable<Employee> getAllEmployees() { Iterable<Employee> employeesIterable = employeeManagerImpl.getAllEmployees(); return employeesIterable; } }
The method fires and a browser shows:
Nothing more in the console. Any ideas?
EDIT: Employee.java
@Entity public class Employee implements Serializable{ private static final long serialVersionUID = -1723798766434132067L; @Id @Getter @Setter @GeneratedValue private Long id; @Getter @Setter @Column(name = "first_name") private String firstName; @Getter @Setter @Column(name = "last_name") private String lastName; @Getter @Setter private BigDecimal salary; public Employee(){ } }
-
Dmitry Zagorulkin over 7 yearsplease provide
employeesIterable
here -
rorschach over 7 yearsWe need to at least see the code for the POJO. It might be that you don't have any serializable fields.
-
KayV over 7 yearsUse ResponseBody with the return type as public @ResponseBody Iterable<Employee> getAllEmployees() {
-
rorschach over 7 years@KaranVerma
@RestController
annotation already takes care of that. -
Radziasss over 7 yearsAs is usually, it started after I asked you there. I removed lombok annotations and added common getters and setters. Now i'm receiving what I wanted. But why it doesn't work with lombok?
-
rorschach over 7 yearsWell, there could be numerous possibilities - most probable is that Lombok is not properly configured. Can you call the getters/setters in your code? After building your app are the getters/setters included in the
Employee.class
file? -
Radziasss over 7 yearsYes, IDE (sts) can see methods, I can call them
-
Sanjay Kumar over 6 yearsAdd getter setter in your model / pojo. It will work !
-
Chris Neve over 5 yearsStill getting this issue today. Nothing suggested here worked. Ideas welcome
-
Jebil almost 5 yearsbasically you need public Getters and Setters in your Entity class. Thats the issue I see here
-
-
Radziasss over 7 yearsIt doesn't change anything. The problem is with lombok annotations - they just doesn't work. But why is that?
-
nurgasemetey over 7 years
@RestController
automatically adds@ResponseBody
annotation to all returns. -
nurgasemetey over 7 yearsSpring Boot uses Jackson by default. The problem he experiences about Lombok not generating getters thus the fields are not serialized because Jackson looks for getters.
-
Radziasss over 7 yearsI didn't know I can use them at class level. Nice, I will use that, but not in this case... nothing has changed.
-
Radziasss over 7 years@nurgasemetey that is true. This default configuration you are writting about, HARDI, I get for free thanks to spring boot. There's something wrong with lombok getters, I didn't solve this yet.
-
Radziasss over 7 yearsIt worked with Data. Then I switched Data with annotations that it covers: ToString, EqualsAndHashCode, Getter, Setter and RequiredArgsConstructor. It worked. Then I only left Getter and Setter. It also worked... I don't get it at all. There must by something wrong with building project by STS then?
-
Pedro Tavares over 7 yearsCan you build and run from console as it is?
-
Yishagerew about 7 yearsAny solution by now? I just stuck into it.
-
alltej over 6 yearsthe
@Getter
and@Setter
can be replaced with single annotation@Data
-
Jebil almost 5 yearsbasically you need public Getters and Setters in your Entity class. Thats the issue I see here
-
Matthias Sommer about 2 yearsI had the same issue and I resolved it by adding @Getter to my response dto class.