20
The application uses Spring Rest in which the paths of a CRUD are automatically generated for each entity, I have the entities Vehicle, Contact and Agency and each one with its respective repository.
Vehicle has as attribute an agency list and a contact list, paths are generated:
daniela.morais@tusk:~$ curl http://localhost:8181/api
{
"_links" : {
"agencias" : {
"href" : "http://localhost:8181/api/agencias{?page,size,sort}",
"templated" : true
},
"veiculos" : {
"href" : "http://localhost:8181/api/veiculos{?page,size,sort}",
"templated" : true
},
"contatos" : {
"href" : "http://localhost:8181/api/contatos{?page,size,sort}",
"templated" : true
},
"profile" : {
"href" : "http://localhost:8181/api/alps"
}
}
}
I created an object Vehicle but I can only get through nome
and tipo
daniela.morais@tusk:~$ curl http://localhost:8181/api/veiculos/55a50d42ccf2bc55501419d6
{
"nome" : "veiculo",
"tipo" : "tipo",
"_links" : {
"self" : {
"href" : "http://localhost:8181/api/veiculos/55a50d42ccf2bc55501419d6"
},
"contatos" : {
"href" : "http://localhost:8181/api/veiculos/55a50d42ccf2bc55501419d6/contatos"
},
"agencias" : {
"href" : "http://localhost:8181/api/veiculos/55a50d42ccf2bc55501419d6/agencias"
}
}
}
This occurs when I try to send an Array:
daniela.morais@tusk:~$ curl -X POST -H "Content-Type:application/json" -d '{"nome": "teste", "tipo": "tipo", "agencias": [{"nome": "agencia"}]}' http://localhost:8181/api/veiculos
{"cause":{"cause":{"cause":null,"message":"Template must not be null or empty!"},"message":"Template must not be null or empty! (through reference chain: oknok.entities.Veiculo[\"agencias\"]->java.util.ArrayList[0])"},"message":"Could not read JSON: Template must not be null or empty! (through reference chain: oknok.entities.Veiculo[\"agencias\"]->java.util.ArrayList[0]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Template must not be null or empty! (through reference chain: oknok.entities.Veiculo[\"agencias\"]->java.util.ArrayList[0])"}
I can execute all requests to /api/vehicle/{id} however I don’t know how to insert a list of agencies and contacts.
My question is: by default REST, all CRUD referring to contacts and agencies must be done in this path, api/vehicles/{id}/contacts and api/vehicles/{id}/agencies, which was generated because it will redirect all requests to their respective repositories, correct? Therefore, how do I create my Agency and Contact list?
I can’t put, but only POST and GET on these paths, I tried to send a POST with a JSON that has an Array, but when I give GET it is not displayed
daniela.morais@tusk:~$ curl http://localhost:8181/api/veiculos/55a50d42ccf2bc55501419d6/agencias
{
"_links" : {
"self" : {
"href" : "http://localhost:8181/api/veiculos/55a50d42ccf2bc55501419d6/agencias"
}
},
"_embedded" : {
"agencias" : [ ]
}
}
daniela.morais@tusk:~$ curl-i -X PUT -H "Content-Type: application/json" -d '{"agencias": [{"nome": "um"}]}' http://localhost:8181/api/veiculos/55a50d42ccf2bc55501419d6/agencias
HTTP/1.1 204 No Content
Server: Apache-Coyote/1.1
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Date: Tue, 14 Jul 2015 14:34:11 GMT
daniela.morais@tusk:~$ curl http://localhost:8181/api/veiculos/55a50d42ccf2bc55501419d6/agencias
{
"_links" : {
"self" : {
"href" : "http://localhost:8181/api/veiculos/55a50d42ccf2bc55501419d6/agencias"
}
},
"_embedded" : {
"agencias" : [ ]
}
}
My entities are these (hid the getters and setters) with their repositories:
Veiculo
@Document
public class Veiculo {
@Id
private String id;
@Indexed(unique = true)
private String nome;
private String tipo;
@DBRef
List<Contato> contatos;
@DBRef
List<Agencia> agencias;
}
Veiculo Repository
@RepositoryRestResource(collectionResourceRel = "veiculos", path = "veiculos")
public interface VeiculoRepository extends MongoRepository<Veiculo, String> {
Veiculo save(Veiculo veiculo);
List<Veiculo> findAll();
}
Agencia
@Document
public class Agencia {
@Id
String id;
String nome;
@CreatedBy
String createdBy;
@LastModifiedBy
String lastModifiedBy;
@CreatedDate
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
Date createdAt;
@LastModifiedDate
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
Date lastModified;
}
Agencia Repository
@RepositoryRestResource(collectionResourceRel = "agencias", path = "agencias")
public interface AgenciaRepository extends MongoRepository<Agencia, String> {
@PreAuthorize("hasRole('ADMIN')")
Agencia save(Agencia t);
List<Agencia> findAll();
}
Contato
@Document
public class Contato {
@Id
private String id;
String nome;
List<Info> dados;
@DBRef
Agencia agencia;
}
Contato Repository
@RepositoryRestResource(collectionResourceRel = "contatos", path = "contatos")
public interface ContatoRepository extends MongoRepository<Contato, String> {
List<Contato> findByNome(@Param("nome") String nome);
List<Contato> findByAgencia(@Param("agencia") String agencia);
}
**UPDATE
I was able to send an Array by adding the annotation @RestResource(exported = false)
attributes that are listed in Vehicle, but:
I. this data is not "relational"
II. If I set up an agency in api/agencies and then get this ID to update the agencies on api/vehicles he does not recognize the reference.
How do I fix this?
daniela.morais@tusk:~$ curl -X POST -H "Content-Type:application/json" -d '{"nome": "teste", "tipo": "tipo", "agencias": [{"nome": "agencia"}]}' http://localhost:8181/api/veiculos
{"timestamp":1437133673823,"status":500,"error":"Internal Server Error","exception":"org.springframework.data.mapping.model.MappingException","message":"Cannot create a reference to an object with a NULL id.","path":"/api/veiculos"}daniela.morais@tusk:~$ ^C
daniela.morais@tusk:~$ curl -X POST -H "Content-Type:application/json" -d '{"nome": "teste", "tipo": "tipo", "agencias": [{"nome": "agencia", "id": "1"}]}' http://localhost:8181/api/veiculos
{
"nome" : "teste",
"tipo" : "tipo",
"contatos" : null,
"agencias" : [ {
"nome" : "agencia",
"createdBy" : null,
"lastModifiedBy" : null,
"createdAt" : null,
"lastModified" : null
} ],
"_links" : {
"self" : {
"href" : "http://localhost:8181/api/veiculos/55a8eb8544ae13951d3f2b6f"
},
"agencia" : {
"href" : "http://localhost:8181/api/veiculos/55a8eb8544ae13951d3f2b6f/agencia"
}
}
}
daniela.morais@tusk:~$ curl http://localhost:8181/api/agencias
{
"_links" : {
"self" : {
"href" : "http://localhost:8181/api/agencias"
}
},
"_embedded" : {
"agencias" : [ ]
},
"page" : {
"size" : 20,
"totalElements" : 0,
"totalPages" : 0,
"number" : 0
}
}