mardi 19 mai 2015

REST- Services GET/PUT/POST/DELETE

Hallo,

mir ist gurndsätlich klar, wie man RESTful - Services schreibt:

Java Code:

  1.  
  2. @RequestMapping(value = "{id}", method = RequestMethod.GET, produces = "application/json")
  3. public @ResponseBody User get(@PathVariable
  4. final String id) {
  5. ...
  6. }
  7.  
  8. @RequestMapping(value = "", method = { RequestMethod.POST }, produces = "application/json")
  9. public @ResponseBody User create(final @RequestBody User user) throws BusinessException {
  10. ...
  11. }
  12.  
  13. @RequestMapping(value = "{id}", method = RequestMethod.PUT, consumes = "application/json", produces = "application/json")
  14. public @ResponseBody User update(@PathVariable String id, @RequestBody User user) throws BusinessException {
  15. ...
  16. }


Mir ist nur nicht klar, wie man "getAllUsers" RESTful macht,
entweder so:
Java Code:

  1.  
  2. @RequestMapping(value = "/getAll", method = RequestMethod.GET, produces = "application/json")
  3. public @ResponseBody List<User> getAll() throws BusinessException {
  4. ...
  5. }


oder so wie unten die oben angeführte Methode verwenden und indem ich id=null lasse und dies in dieser Methode unten überprüfe.
Code:

@RequestMapping(value = "{id}", method = RequestMethod.GET, produces = "application/json")
    public @ResponseBody User get(@PathVariable
    final String id)  {
        if (id == null) {
            // alle User zurückgeben
        }
      ...
    }

Ich wär dankbar für einen Tip in diese Richtung!
Danke!
Poller


REST- Services GET/PUT/POST/DELETE

0 commentaires:

Enregistrer un commentaire