2015-01-30 13 views
5

Hat jemand ein vollständiges Spring Boot REST CRUD Beispiel? Die site.spring.io hat nur ein RequestMapping für GET. Ich kann POST und DELETE arbeiten, aber nicht PUT.Spring Boot voll REST CRUD Beispiel

Ich vermute, es ist, wie ich versuche, die Parameter zu erhalten, wo die Trennung ist, aber ich habe kein Beispiel gesehen, wo jemand ein Update durchführt.

Ich verwende derzeit die SO iPhone App, sodass ich meinen aktuellen Code nicht einfügen kann. Jedes Arbeitsbeispiel wäre großartig!

Antwort

6

Wie Sie sehen können, habe ich zwei Möglichkeiten zur Aktualisierung implementiert. Der erste erhält einen JSON und der zweite die cusotmerId in der URL und JSON.

@RestController 
@RequestMapping("/customer") 
public class CustomerController { 


    @RequestMapping(value = "/{id}", method = RequestMethod.GET) 
    public Customer greetings(@PathVariable("id") Long id) { 
     Customer customer = new Customer(); 
     customer.setName("Eddu"); 
     customer.setLastname("Melendez"); 
     return customer; 
    } 

    @RequestMapping(value = "/", method = RequestMethod.GET) 
    public List<Customer> list() { 
     return Collections.emptyList(); 
    } 

    @RequestMapping(method = RequestMethod.POST) 
    public void add(@RequestBody Customer customer) { 

    } 

    @RequestMapping(method = RequestMethod.PUT) 
    public void update(@RequestBody Customer customer) { 

    } 

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT) 
    public void updateById(@PathVariable("id") Long id, @RequestBody Customer customer) { 

    } 

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) 
    public void delete() { 

    } 

    class Customer implements Serializable { 

     private String name; 

     private String lastname; 

     public String getName() { 
      return name; 
     } 

     public void setName(String name) { 
      this.name = name; 
     } 

     public void setLastname(String lastname) { 
      this.lastname = lastname; 
     } 

     public String getLastname() { 
      return lastname; 
     } 
    } 
} 
+0

Hallo Eddú. Ich denke, die Methodenliste sollte keine PathVariable enthalten, weil sie alle Kunden im System zurückgeben sollte. Siehe http://www.slideshare.net/stormpath/rest-jsonapis als Referenz. –

+0

Die zweite PUT war, was ich suchte. Es machte mir klar, dass ich die ID nicht im Körper der Anfrage verschickte, nur in der URI.Das funktioniert jetzt für mich. –

5

Oder alternativ:

@RepositoryRestResource 
public interface CustomerRepository extends JpaRepository<Customer, Long> { 
} 

So verwenden Sie die Anmerkung org.springframework.data.rest.core.annotation.RepositoryRestResource, können Sie die folgende Abhängigkeit zu Ihrem pom.xml hinzufügen müssen:

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-data-rest</artifactId> 
</dependency> 
+0

Das ist, wonach ich gesucht habe. – Kieveli

2

Eine alternative Update gibt ResponseEntity Objekt .

@RestController 
@RequestMapping("/fruits") 
public class FruitController { 

    private final Logger LOG = LoggerFactory.getLogger(FruitController.class); 

    @Autowired 
    private FruitService fruitService; 

    @RequestMapping(method = RequestMethod.GET) 
    public ResponseEntity<List<Fruit>> getAll(@RequestParam(value = "offset", defaultValue = "0") int index, 
      @RequestParam(value = "numberOfRecord", defaultValue = "10") int numberOfRecord) { 
     LOG.info("Getting all fruits with index: {}, and count: {}", index, numberOfRecord); 
     List<Fruit> fruits = fruitService.getAll(index, numberOfRecord); 

     if (fruits == null || fruits.isEmpty()) { 
      return new ResponseEntity<List<Fruit>>(HttpStatus.NO_CONTENT); 
     } 

     return new ResponseEntity<List<Fruit>>(fruits, HttpStatus.OK); 
    } 

    @RequestMapping(value = "{id}", method = RequestMethod.GET) 
    public ResponseEntity<Fruit> get(@PathVariable("id") int id) { 
     LOG.info("Getting fruit with id: {}", id); 
     Fruit fruit = fruitService.findById(id); 

     if (fruit == null) { 
      return new ResponseEntity<Fruit>(HttpStatus.NOT_FOUND); 
     } 

     return new ResponseEntity<Fruit>(fruit, HttpStatus.OK); 
    } 

    @RequestMapping(method = RequestMethod.POST) 
    public ResponseEntity<Void> create(@RequestBody Fruit fruit, UriComponentsBuilder ucBuilder) { 
     LOG.info("Creating fruit: {}", fruit); 

     if (fruitService.exists(fruit)) { 
      return new ResponseEntity<Void>(HttpStatus.CONFLICT); 
     } 

     fruitService.create(fruit); 

     HttpHeaders headers = new HttpHeaders(); 
     headers.setLocation(ucBuilder.path("/fruit/{id}").buildAndExpand(fruit.getId()).toUri()); 
     return new ResponseEntity<Void>(headers, HttpStatus.CREATED); 
    } 

    @RequestMapping(value = "{id}", method = RequestMethod.PUT) 
    public ResponseEntity<Fruit> update(@PathVariable int id, @RequestBody Fruit fruit) { 
     LOG.info("Updating fruit: {}", fruit); 
     Fruit currentFruit = fruitService.findById(id); 

     if (currentFruit == null) { 
      return new ResponseEntity<Fruit>(HttpStatus.NOT_FOUND); 
     } 

     currentFruit.setId(fruit.getId()); 
     currentFruit.setName(fruit.getName()); 

     fruitService.update(fruit); 
     return new ResponseEntity<Fruit>(currentFruit, HttpStatus.OK); 
    } 

    @RequestMapping(value = "{id}", method = RequestMethod.DELETE) 
    public ResponseEntity<Void> delete(@PathVariable("id") int id) { 
     LOG.info("Deleting fruit with id: {}", id); 
     Fruit fruit = fruitService.findById(id); 

     if (fruit == null) { 
      return new ResponseEntity<Void>(HttpStatus.NOT_FOUND); 
     } 

     fruitService.delete(id); 
     return new ResponseEntity<Void>(HttpStatus.OK); 
    } 
} 

Von Spring MVC RESTFul Web Service CRUD Example

0

Sie können meinen vollen RESTful-Server und Client-Anwendung mit SpringBoot bei Spring RESTFul Examples at github

+0

Haben Sie Ihr Repository entfernt? Kannst du es hier aktualisieren oder deine Antwort entfernen? – Tom

+0

Sorry, Tom, korrigiere den Link jetzt – Anand

0

erhalten Ich habe eine Reihe von Tutorials auf Frühling Boot-CRUD Operationen vorbereitet. Im Folgenden sind die Inhalte von Tutorials:

  1. So erstellen Frühling Boot-Projekt mit Spring Tool Suite
  2. Wie implementieren GET & POST-Methode im Frühjahr Boot erholsamen Web-Service
  3. Wie implementieren PUT & DELETE-Methode in Federverschluß erholsamen Web-Service
  4. geruhsamen mit Feder Boot Service-Web-PostgreSQL-Datenbank mit Federverschluß JPA Integrieren
  5. Verwendung von CURL Befehle

Youtube Tutorials:

  1. Spring Boot Restful Web Service Tutorial | Tutorial 1 - Introduction
  2. Spring Boot Restful Web Services CRUD Example GET & POST | Tutorial - 2
  3. Spring boot CRUD Operations example PUT & DELETE | Tutorial - 3
  4. Spring Boot JPA | In 5 Simple Steps Integrate PostgreSQL Database | Tuorial - 4

Besuchen Blog für weitere Details.

Verwandte Themen