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 /? |
Iterable<T> |
findAllXml()
XML findAll is currently unimplemented, see https://github.com/FasterXML/jackson-dataformat-xml/issues/38 for more details
|
T |
findById(ID id)
Find a resource by its identifier
REST webservice published : GET /{id} |
org.springframework.data.domain.Page<T> |
findPaginated(Integer page,
Integer size)
Find all resources, and return a paginated 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> findAllXml()
findAllXml
in interface RestController<T,ID extends Serializable>
public 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)
findPaginated
in interface RestController<T,ID extends Serializable>
page
- Page number starting from 0. default to 0size
- Number of resources by pages. default to 10public T findById(@PathVariable ID id)
findById
in interface RestController<T,ID extends Serializable>
id
- The identifier of the resouce to findpublic 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-2012. All Rights Reserved.