Skip to content

Instantly share code, notes, and snippets.

@lamjar
Forked from wvuong/PlacesRESTController.java
Created July 3, 2017 13:04
Show Gist options
  • Save lamjar/94b34a7d4abeb3a44aa91f9ae3e70bb3 to your computer and use it in GitHub Desktop.
Save lamjar/94b34a7d4abeb3a44aa91f9ae3e70bb3 to your computer and use it in GitHub Desktop.
Simple generic REST-ful Spring MVC controller, interops with Spring Data repositories
package com.willvuong.foodie.controller;
import com.willvuong.foodie.dao.PlaceRepository;
import com.willvuong.foodie.domain.Place;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/rest/places")
public class PlacesRESTController extends RESTController<Place, String> {
@Autowired
public PlacesRESTController(PlaceRepository repo) {
super(repo);
}
}
package com.willvuong.foodie.controller;
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.repository.CrudRepository;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
public abstract class RESTController<T, ID extends Serializable> {
private Logger logger = LoggerFactory.getLogger(RESTController.class);
private CrudRepository<T, ID> repo;
public RESTController(CrudRepository<T, ID> repo) {
this.repo = repo;
}
@RequestMapping
public @ResponseBody List<T> listAll() {
Iterable<T> all = this.repo.findAll();
return Lists.newArrayList(all);
}
@RequestMapping(method=RequestMethod.POST, consumes={MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody Map<String, Object> create(@RequestBody T json) {
logger.debug("create() with body {} of type {}", json, json.getClass());
T created = this.repo.save(json);
Map<String, Object> m = Maps.newHashMap();
m.put("success", true);
m.put("created", created);
return m;
}
@RequestMapping(value="/{id}", method=RequestMethod.GET)
public @ResponseBody T get(@PathVariable ID id) {
return this.repo.findOne(id);
}
@RequestMapping(value="/{id}", method=RequestMethod.POST, consumes={MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody Map<String, Object> update(@PathVariable ID id, @RequestBody T json) {
logger.debug("update() of id#{} with body {}", id, json);
logger.debug("T json is of type {}", json.getClass());
T entity = this.repo.findOne(id);
try {
BeanUtils.copyProperties(entity, json);
}
catch (Exception e) {
logger.warn("while copying properties", e);
throw Throwables.propagate(e);
}
logger.debug("merged entity: {}", entity);
T updated = this.repo.save(entity);
logger.debug("updated enitity: {}", updated);
Map<String, Object> m = Maps.newHashMap();
m.put("success", true);
m.put("id", id);
m.put("updated", updated);
return m;
}
@RequestMapping(value="/{id}", method=RequestMethod.DELETE)
public @ResponseBody Map<String, Object> delete(@PathVariable ID id) {
this.repo.delete(id);
Map<String, Object> m = Maps.newHashMap();
m.put("success", true);
return m;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment