简体   繁体   中英

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:

在此处输入图像描述

REST GET Call to get the JSON response for the User object (the problem) 在此处输入图像描述

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

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

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", ... 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM