Does anyone have a full spring boot REST CRUD example? The spring.io site just has a RequestMapping for GET. I'm able to get POST and DELETE working but not PUT.
I suspect it's how I'm trying to get the params where the disconnect is, but I have not seen an example where someone is performing an update.
I'm currently using the SO iPhone app so I can't paste my current code. Any working examples would be great!
CrudRepository: provides CRUD functions. PagingAndSortingRepository: provides methods to do pagination and sort records. JpaRepository: provides JPA related methods such as flushing the persistence context and delete records in a batch.
Spring RestController annotation is a convenience annotation that is itself annotated with @Controller and @ResponseBody . This annotation is applied to a class to mark it as a request handler. Spring RestController annotation is used to create RESTful web services using Spring MVC.
As you can see I have implemented two way to update. The first one will receive a json, and the second one will receive the cusotmerId in the URL and json also.
@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;
}
}
}
An alternative update returns ResponseEntity object.
@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);
}
}
From Spring MVC RESTFul Web Service CRUD Example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With