2015-01-30 13 views
5

Có ai có ví dụ REST CRUD khởi động đầy đủ không? Trang web spring.io chỉ có một RequestMapping cho GET. Tôi có thể nhận được POST và DELETE nhưng không phải PUT.Khởi động mùa xuân đầy đủ ví dụ REST CRUD

Tôi nghi ngờ đó là cách tôi đang cố gắng để có được các params nơi ngắt kết nối được, nhưng tôi đã không nhìn thấy một ví dụ mà ai đó đang thực hiện một bản cập nhật.

Tôi hiện đang sử dụng ứng dụng SO dành cho iPhone nên tôi không thể dán mã hiện tại của mình. Bất kỳ ví dụ làm việc nào cũng sẽ tuyệt vời!

Trả lời

6

Như bạn có thể thấy tôi đã triển khai hai cách để cập nhật. Người đầu tiên sẽ nhận được một json, và người thứ hai sẽ nhận được cusotmerId trong URL và 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

Hi Eddú. Tôi nghĩ rằng danh sách phương pháp không nên mang theo một PathVariable bởi vì nó sẽ trả lại tất cả các khách hàng trong hệ thống phải không? Xem http://www.slideshare.net/stormpath/rest-jsonapis làm tài liệu tham khảo. –

+0

PUT thứ hai là thứ tôi đang tìm kiếm. Nó làm cho tôi nhận ra rằng tôi đã không gửi id trong cơ thể của yêu cầu, chỉ trong URI.Điều này bây giờ làm việc cho tôi. –

5

Hoặc cách khác:

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

Để sử dụng chú thích org.springframework.data.rest.core.annotation.RepositoryRestResource, bạn cần phải thêm phụ thuộc sau đây để pom.xml của bạn:

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

Đây là những gì tôi đã tìm kiếm. – Kieveli

2

Bản cập nhật thay thế trả về đối tượng ResponseEntity .

@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); 
    } 
} 

Từ Spring MVC RESTFul Web Service CRUD Example

0

Bạn có thể nhận được đầy đủ máy chủ RESTful tôi và ứng dụng client sử dụng SpringBoot tại Spring RESTFul Examples at github

+0

Bạn đã xóa kho lưu trữ của mình chưa? Bạn có thể cập nhật nó ở đây hoặc xóa câu trả lời của bạn không? – Tom

+0

Xin lỗi Tom, đã sửa liên kết ngay bây giờ – Anand

0

Tôi đã chuẩn bị một bộ hướng dẫn về mùa xuân Boot CRUD hoạt động. Sau đây là nội dung của hướng dẫn:

  1. Làm thế nào để tạo ra dự án khởi động mùa xuân sử dụng Spring Tool Suite
  2. Làm thế nào để thực hiện phương thức GET & POST trong khởi động mùa xuân dịch vụ web yên tĩnh
  3. Làm thế nào để thực hiện PUT & phương pháp DELETE trong mùa xuân khởi động dịch vụ web yên tĩnh
  4. Tích hợp cơ sở dữ liệu PostgreSQL sử dụng JPA khởi động mùa xuân với khởi động mùa xuân dịch vụ web yên tĩnh
  5. sử dụng CURL lệnh

Youtube Hướng dẫn:

  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

Visit Blog để biết thêm chi tiết.

Các vấn đề liên quan