Pages enfant
  • 3.12.1 Ecrire le service REST à exposer

Sommaire :


Introduction

Esup-commons V2 utilise la norme JAX-RS pour exposer la couche métier en REST. L'implémentation utilisée est celle offerte pour CXF.

Utilisation

En JAX-RS il est très simple d'exposer la couche métier sous forme d'un service REST. Cela revient à utiliser des annotations au niveau de l'interface DomainService :

@Path("/domainService/")
@Produces("application/json")
public interface DomainService extends Serializable {
.../...

@GET
@Path("/users/{id}")
User getUser(@PathParam("id") String id) throws UserNotFoundException;
.../...

@GET
@Path("/users")
List<User> getUsers();
.../...

@DELETE
@Path("/users/{id}")
void deleteUser(@PathParam("id") String id);
.../...

@PUT
@Path("/users")
void addUser(User user);
.../...

Pour avoir plus d'information sur les annotations JAX-RS Cf.JAX-RS : Understanding the Basics

Exemple d'utilisation

Ajout

curl -X PUT \
     -H 'Content-Type: application/json' \
     -d '{"language":"fr","id":"colmant","displayName":"Y Colmant","informations":[{"informationKey":"TRUC","informationValue":"machin"},{"informationKey":"TRUC2","informationValue":"machin2"}],"admin":false }' \
     http://localhost:8080/cxf/rest/domainService/users

Lecture

curl -X GET \
     -H 'Content-Type: application/json' \
     http://localhost:8080/cxf/rest/domainService/users

Renvoie

[
 {"language": "fr",
  "id": "colmant",
  "displayName": "Y Colmant",
  "admin": false,
  "informations": [
    {"id": 22,
     "informationValue": "machin",
     "informationKey": "TRUC"
    },
    {"id": 23,
     "informationValue": "machin2",
     "informationKey": "TRUC2"
    },
    {"id": 24,
     "informationValue": "2013/03/01 17:57:01",
     "informationKey": "INSERT_DATE"
    }
  ]
 }
]

On note que l'on a un item supplémentaire pour informations. C'est le code métier de l'application qui l'a ajouté

Suppression

 curl -X DELETE \
     -H 'Content-Type: application/json' \
     http://localhost:8080/cxf/rest/domainService/users/colmant
  • Aucune étiquette