17

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(){

    }
}
Radziasss
  • 193
  • 1
  • 1
  • 11

3 Answers3

12

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.

T04435
  • 12,507
  • 5
  • 54
  • 54
Pedro Tavares
  • 1,119
  • 8
  • 19
  • 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 Oct 10 '16 at 05:29
  • 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? – Radziasss Oct 11 '16 at 06:42
  • Can you build and run from console as it is? – Pedro Tavares Oct 12 '16 at 14:05
  • 1
    the `@Getter` and `@Setter` can be replaced with single annotation `@Data` – alltej Dec 22 '17 at 21:17
  • 2
    basically you need public Getters and Setters in your Entity class. Thats the issue I see here – Jebil May 30 '19 at 10:46
  • I had the same issue and I resolved it by adding @Getter to my response dto class. – Matthias Sommer Apr 19 '22 at 09:34
0

Do you have a converter in the project that converts JAVA objects to JSON. If not, you need on. Try using Jackson in your project.

Once Jackson jars are imported into the project, the dispatcher servlet should have below:

    <beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <beans:property name="messageConverters">
            <beans:list>
                <beans:ref bean="jsonMessageConverter" />
            </beans:list>
        </beans:property>
    </beans:bean>

    <!-- Configure bean to convert JSON to POJO and vice versa -->
    <beans:bean id="jsonMessageConverter"
        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    </beans:bean>
HARDI
  • 394
  • 5
  • 12
  • 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. – nurgasemetey Oct 09 '16 at 09:37
  • @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 Oct 10 '16 at 05:36
  • Any solution by now? I just stuck into it. – Yishagerew May 05 '17 at 18:45
-1

Try adding @ResponseBody to your REST method's return type declaration in the method signature. That should do it if you're using the standard Spring Boot starter project.

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83