T
- Your resource class to manage, maybe an entity or DTO classID
- Resource id type, usually Long or StringR
- The repository classpublic abstract class RepositoryBasedRestController<T,ID extends Serializable,R extends org.springframework.data.repository.PagingAndSortingRepository> extends Object implements RestController<T,ID>
You should extend this class when you want to use a 2 layers pattern : Repository and Controller. This is the default controller implementation to use if you have no service (also called business) layer. You will be able to transform it to a ServiceBasedRestController later easily if needed.
Default implementation uses "id" field (usually a Long) in order to identify resources in web request. If your want to identity resources by a slug (human readable identifier), your should override findById() method with for example :
@Override
public Sample findById(@PathVariable String id) {
Sample sample = this.repository.findByName(id);
if (sample == null) {
throw new NotFoundException();
}
return sample;
}
ServiceBasedRestController
Modifier and Type | Field and Description |
---|---|
protected org.slf4j.Logger |
logger |
protected R |
repository |
Constructor and Description |
---|
RepositoryBasedRestController() |
Modifier and Type | Method and Description |
---|---|
T |
create(T resource)
Create a new resource
REST webservice published : POST / |
void |
delete()
Delete all resources
REST webservice published : DELETE / Return No Content http status code if the request has been correctly processed |
void |
delete(ID id)
Delete a resource by its identifier
REST webservice published : DELETE /{id} Return No Content http status code if the request has been correctly processed |
Iterable<T> |
findAll()
Find all resources, and return the full collection (plain list not paginated)
REST webservice published : GET /? |
T |
findById(ID id)
Find a resource by its identifier
REST webservice published : GET /{id} |
Iterable<T> |
findByIds(Set<ID> ids)
Find multiple resources by their identifiers
REST webservice published : GET /? |
org.springframework.data.domain.Page<T> |
findPaginated(Integer page,
Integer size,
String direction,
String properties)
Find all resources, and return a paginated and optionaly sorted collection
REST webservice published : GET /search? |
void |
setRepository(R repository)
You should override this setter in order to inject your repository with @Inject annotation
|
T |
update(ID id,
T resource)
Update an existing resource
REST webservice published : PUT /{id} |
protected R extends org.springframework.data.repository.PagingAndSortingRepository repository
protected org.slf4j.Logger logger
public void setRepository(R repository)
repository
- The repository to be injectedpublic T create(@RequestBody T resource)
create
in interface RestController<T,ID extends Serializable>
resource
- The resource to createpublic T update(@PathVariable ID id, @RequestBody T resource)
update
in interface RestController<T,ID extends Serializable>
id
- The identifier of the resource to update, usually a Long or String identifier. It is explicitely provided in order to handle cases where the identifier could be changed.resource
- The resource to updatepublic Iterable<T> findAll()
findAll
in interface RestController<T,ID extends Serializable>
public org.springframework.data.domain.Page<T> findPaginated(@RequestParam(value="page",required=false,defaultValue="1") Integer page, @RequestParam(value="size",required=false,defaultValue="10") Integer size, @RequestParam(value="direction",required=false,defaultValue="") String direction, @RequestParam(value="properties",required=false) String properties)
findPaginated
in interface RestController<T,ID extends Serializable>
page
- Page number starting from 0. default to 0size
- Number of resources by pages. default to 10direction
- Optional sort direction, could be "asc" or "desc"properties
- Ordered list of comma separeted properies used for sorting resulats. At least one property should be provided if direction is specifiedpublic T findById(@PathVariable ID id)
findById
in interface RestController<T,ID extends Serializable>
id
- The identifier of the resouce to findpublic Iterable<T> findByIds(@RequestParam(value="ids[]") Set<ID> ids)
findByIds
in interface RestController<T,ID extends Serializable>
ids
- List of ids to retrievepublic void delete()
delete
in interface RestController<T,ID extends Serializable>
public void delete(@PathVariable ID id)
delete
in interface RestController<T,ID extends Serializable>
id
- The identifier of the resource to deleteCopyright © 2009–2014. All rights reserved.