Pages enfant
  • 3.12.1 Ecrire le service REST à exposer

Comparaison des versions

Légende

  • Ces lignes ont été ajoutées. Ce mot a été ajouté.
  • Ces lignes ont été supprimées. Ce mot a été supprimé.
  • La mise en forme a été modifiée.
Commentaire: Migrated to Confluence 5.3

Sommaire :

Sommaire
maxLevel3

...

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.

...

Bloc de code
@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

Bloc de code
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

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

Renvoie

Bloc de code
languagejavascript
[
 {
Bloc de code
langjavascript
{
 "language": "fr",
  "id": "colmant",
  "displayName": "Y Colmant",
  "informationsadmin": false,
  "informations": [
    {"id": 22,
     "informationValue": "machin",
     "informationKey": "TRUC"
    },
    {"id": 23,
     "informationValue":53 "machin2",
     "informationKey":"INSERT_DATE",
 "TRUC2"
    },
    {"id": 24,
     "informationValue": "20112013/0603/1001 1217:57:27:01",
     "informationKey": "INSERT_DATE"
    }
  ],
 "admin":false
}
 }
]

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

Bloc de code
 curl -X DELETE \
     -H 'Content-Type: application/json' \
     http://localhost:8080/cxf/rest/domainService/users/colmant