Created
May 29, 2013 20:38
-
-
Save wvuong/5673644 to your computer and use it in GitHub Desktop.
Simple generic REST-ful Spring MVC controller, interops with Spring Data repositories
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This only work if ID is String.
Spring can't deduce the type of @PathVariable ID id
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the sharing. @ small erroes maybe related the the version of the libraries your using.
if using BeanUtils of spring, the copy order is reversed. its
BeanUtils.copyProperties(json, entity)
findOne might be replaced with findById.
Example with reactive streams