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.

Share:
14,460

Related videos on Youtube

Radziasss
Author by

Radziasss

java, javascript, extjs, angularjs

Updated on September 03, 2020

Comments

  • Radziasss
    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:

    enter image description here

    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
      Dmitry Zagorulkin over 7 years
      please provide employeesIterable here
    • rorschach
      rorschach over 7 years
      We need to at least see the code for the POJO. It might be that you don't have any serializable fields.
    • KayV
      KayV over 7 years
      Use ResponseBody with the return type as public @ResponseBody Iterable<Employee> getAllEmployees() {
    • rorschach
      rorschach over 7 years
      @KaranVerma @RestController annotation already takes care of that.
    • Radziasss
      Radziasss over 7 years
      As 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
      rorschach over 7 years
      Well, 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
      Radziasss over 7 years
      Yes, IDE (sts) can see methods, I can call them
    • Sanjay Kumar
      Sanjay Kumar over 6 years
      Add getter setter in your model / pojo. It will work !
    • Chris Neve
      Chris Neve over 5 years
      Still getting this issue today. Nothing suggested here worked. Ideas welcome
    • Jebil
      Jebil almost 5 years
      basically you need public Getters and Setters in your Entity class. Thats the issue I see here
  • Radziasss
    Radziasss over 7 years
    It doesn't change anything. The problem is with lombok annotations - they just doesn't work. But why is that?
  • nurgasemetey
    nurgasemetey over 7 years
    @RestController automatically adds @ResponseBody annotation to all returns.
  • nurgasemetey
    nurgasemetey over 7 years
    Spring 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
    Radziasss over 7 years
    I didn't know I can use them at class level. Nice, I will use that, but not in this case... nothing has changed.
  • Radziasss
    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
    Radziasss over 7 years
    It 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
    Pedro Tavares over 7 years
    Can you build and run from console as it is?
  • Yishagerew
    Yishagerew about 7 years
    Any solution by now? I just stuck into it.
  • alltej
    alltej over 6 years
    the @Getter and @Setter can be replaced with single annotation @Data
  • Jebil
    Jebil almost 5 years
    basically you need public Getters and Setters in your Entity class. Thats the issue I see here
  • Matthias Sommer
    Matthias Sommer about 2 years
    I had the same issue and I resolved it by adding @Getter to my response dto class.