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.