3

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.

iCrus
  • 1,210
  • 16
  • 30
  • Is it normal that you trying to update (via PUT) the entity id=3 with id=1? What /users/ gives you? – nKognito May 10 '15 at 15:56
  • @nKognito Please ignore the screenshot, I have run it with correct values also a few times already (method=Post, id=1, entityid=1) with the same result. /users/ gives me the JSON with "links" to all elements but no name,email properties. – iCrus May 10 '15 at 16:45
  • Try to enable exposeIds option through configuration. Let's see if it will return at least entity's identifier – nKognito May 10 '15 at 18:21
  • 1
    And try to remove lombok's @Data - maybe it somehow affects... – nKognito May 10 '15 at 18:24
  • @nKognito Removing lombok's Data did the trick. Thanks for the pointer. That is sad however that lombok has this issue. Raising a bug on lombok (didn't find it as a known issue). – iCrus May 11 '15 at 10:35

2 Answers2

3

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

iCrus
  • 1,210
  • 16
  • 30
  • 1
    I had a similar issue in Intelli J, however, enabling annotation processing fixed the issue. I thought that there would be a slightly more "spectacular" error thrown, but it just failed silently. – Benjamin Slabbert Apr 21 '17 at 14:36
1

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", ... 
JonAar Livernois
  • 344
  • 2
  • 12
  • Thank you for this. As someone new to Spring and relatively unskilled with Java, this was the answer that helped me – TheHanna Mar 16 '22 at 01:04