POST Spring Data REST does not work with relationships

4

I'm having a project using Spring Boot and Spring Data Rest to serve a Rest API.

When I'm serving an entity without relationships, it works without problems.

The problem is when I use an entity with relationships. I can not add new entities via POST. I tested it with my AngularJS app and Chrome extensions like Yet Another REST Client, but POST does not add the entity correctly. It edits an existing record causing confusion.

Follow the code:

@Entity
public class City {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private boolean capital;

    @ManyToOne(targetEntity=State.class, fetch = FetchType.EAGER)
    @JoinColumn(name = "state_id")
    private State state;
    // getters e setters omitidos
}

@RepositoryRestResource(excerptProjection = CityProjection.class)
public interface Cities extends PagingAndSortingRepository<City, Long>{

}

If necessary put more parts of the code. I still do not know what I did wrong or I stopped doing it to work correctly.

All queries work correctly.

I made the following test, I sent a Request as POST with the following content:

{
    "name": "Aparecida de Goiânia",
    "capital": false,
    "state": {
        "id": "1",
        "name": "Goiás",
        "initials": "GO"
    }
}

And I received the return and what appears in the database:

{
  "id": 1,
  "name": "Goiás",
  "capital": false,
  "_links": {
    "self": {
       "href": "http://localhost:8080/rest/cities/1"
     },
    "city": {
      "href": "http://localhost:8080/rest/cities/1{?projection}",
      "templated": true
    },
    "state": {
      "href": "http://localhost:8080/rest/cities/1/state"
    }
  }
}

The same happens if I add "id": null in the city.

    
asked by anonymous 28.05.2016 / 16:18

1 answer

1

First you must make the post in State and then in City by sending in the corresponding address. (I could not find any way to send the state as a child object)

Example:

POST http://localhost:8080/states
{ 
  "id": "1",
  "name": "Goiás",
  "initials": "GO"
}

POST http://localhost:8080/cities
{
  "name": "Aparecida de Goiânia",
  "capital": false,
  "state": "http://localhost:8080/states/1"
}

POST http://localhost:8080/cities
{
  "name": "Aparecida de Goiânia 2",
  "capital": false,
  "state": "http://localhost:8080/states/1"
}

Response to calling GET link

{
  "_embedded" : {
    "cities" : [ {
      "name" : "Aparecida de Goiânia",
      "id" : 1,
      "state" : {
        "name" : "Goiás",
        "initials" : "GO"
      },
      "capital" : false,
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/cities/1"
        },
        "city" : {
          "href" : "http://localhost:8080/cities/1{?projection}",
          "templated" : true
        },
        "state" : {
          "href" : "http://localhost:8080/cities/1/state"
        }
      }
    }, {
      "name" : "Aparecida de Goiânia 2",
      "id" : 2,
      "state" : {
        "name" : "Goiás",
        "initials" : "GO"
      },
      "capital" : false,
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/cities/2"
        },
        "city" : {
          "href" : "http://localhost:8080/cities/2{?projection}",
          "templated" : true
        },
        "state" : {
          "href" : "http://localhost:8080/cities/2/state"
        }
      }
    } ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/cities"
    },
    "profile" : {
      "href" : "http://localhost:8080/profile/cities"
    }
  },
  "page" : {
    "size" : 20,
    "totalElements" : 2,
    "totalPages" : 1,
    "number" : 0
  }
}
    
02.06.2016 / 03:50