T
- Your resource class to manage, maybe an entity or DTO classID
- Resource id type, usually Long or StringS
- The service classpublic abstract class ServiceBasedRestController<T,ID extends Serializable,S extends CrudService> extends Object implements RestController<T,ID>
You should extend this class when you want to use a 3 layers pattern : Repository, Service and Controller If you don't have a real service (also called business layer), consider using RepositoryBasedRestController
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.service.findByName(id);
if (sample == null) {
throw new NotFoundException();
}
return sample;
}
RepositoryBasedRestController
Constructor and Description |
---|
ServiceBasedRestController() |
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 |
setService(S service)
You should override this setter in order to inject your service with @Inject annotation
|
T |
update(ID id,
T resource)
Update an existing resource
REST webservice published : PUT /{id} |
protected S extends CrudService service
public void setService(S service)
service
- The service 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.