iCrus
iCrus

Reputation: 1248

JSON not returning columns in Spring Data REST

I am trying out examples for Spring Data REST however the JSON object returned in my testing does not return the column names (which were earlier 'PUT') and just returns the links to the objects. What could be wrong?

Scenario:

Entity: 'User'

@Entity
@Data
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String guid;
    private String fullName;
    private String email;
}

Repository: UserRepository (Exposed as REST service)

@RepositoryRestResource(collectionResourceRel = "users", path = "users")
public interface UserRepository extends JpaRepository<User, Long> {
}

REST 'PUT' request to create a USER object:

enter image description here

REST GET Call to get the JSON response for the User object (the problem) enter image description here

No id, Guid or email is returned in the JSON response.

Upvotes: 3

Views: 2401

Answers (2)

JonAar Livernois
JonAar Livernois

Reputation: 344

I had the same problem, but I was not using autogenerated Lombok classes.

I found a solution. The problem in what I was experiencing was that only links appear in the json output of a Spring Data Repository.

For example:

 {
      "_embedded": {
        "users": [{
              "_links": {
                "self": {
                  "href": "http://localhost:8080/users/1"
                },
                "user": {
                  "href": "http://localhost:8080/users/1"
                }
              }
            }, {
              "_links": {
                "self": {
                  "href": "http://localhost:8080/users/2"
                },
                "user": {
                  "href": "http://localhost:8080/users/2"
                }
              }
            }, {
              "_links": { ...

The Solution:

Add getters and setters to the Entity, one for each variable you want to show in the response. Once you add getters and setters, you will be able to run the application again, and get json responses that contain values for each of your variables.

In one of the other answers, the workaround was to remove Lombok @Data annotation. Lombok was not generating getters and setters for the class using that annotation in time, so, in turn, none of the returned JSON responses contained any initialized values.

The outcome looks better: {

  "_embedded" : {
    "users" : [ {
      "username" : "admin",
      "firstName" : "Administrator",
      "lastName" : "Administrator",
      "roles" : [ { "roleName" : "ROLE_ADMIN", "description" : "Administrator"
      } ],
      "_links" : { "self" : { "href" : "http://localhost:8080/users/1" },
        "user" : { "href" : "http://localhost:8080/users/1" }
      }
    }, {
      "username" : "bobby", ... 

Upvotes: 1

iCrus
iCrus

Reputation: 1248

Removing lombok's @Data annotation made all basic properties appear in the JSON response. Looks like a side effect of lombok.

Upvotes: 4

Related Questions