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

    }
}
10
  • please provide employeesIterable here Commented Oct 7, 2016 at 10:10
  • 2
    We need to at least see the code for the POJO. It might be that you don't have any serializable fields. Commented Oct 7, 2016 at 10:10
  • Use ResponseBody with the return type as public @ResponseBody Iterable<Employee> getAllEmployees() { Commented Oct 7, 2016 at 10:12
  • 1
    @KaranVerma @RestController annotation already takes care of that. Commented Oct 7, 2016 at 10:16
  • 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? Commented Oct 7, 2016 at 10:19

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I didn't know I can use them at class level. Nice, I will use that, but not in this case... nothing has changed.
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?
Can you build and run from console as it is?
the @Getter and @Setter can be replaced with single annotation @Data
basically you need public Getters and Setters in your Entity class. Thats the issue I see here
|
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>

3 Comments

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 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.
Any solution by now? I just stuck into it.
-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.

2 Comments

It doesn't change anything. The problem is with lombok annotations - they just doesn't work. But why is that?
@RestController automatically adds @ResponseBody annotation to all returns.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.