public class RepositoryRestController extends RepositoryExporterSupport<RepositoryRestController> implements org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.InitializingBean
RepositoryRestExporterServlet
in your web.xml. For example, to send all requests through the REST exporter, add the
following to your web.xml:
<servlet>
<servlet-name>exporter</servlet-name>
<servlet-class>org.springframework.data.rest.webmvc.RepositoryRestExporterServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>exporter</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
One can also deploy this controller into an existing Spring MVC application. In general, one should be able to
simply create an instance of the RepositoryRestMvcConfiguration
bean in your ApplicationContext
or in JavaConfig.
If you wish to alter the way the REST exporter functions, you don't configure the controller directly. Instead there
is a RepositoryRestConfiguration
helper class that you create in your ApplicationContext. If a feature is
configurable in Spring Data REST, there is a property on this helper to configure it.Modifier and Type | Field and Description |
---|---|
static java.lang.String |
LOCATION |
static java.lang.String |
SELF |
repositoryExporters
Constructor and Description |
---|
RepositoryRestController() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet() |
org.springframework.http.ResponseEntity<?> |
clearLinks(org.springframework.http.server.ServletServerHttpRequest request,
java.lang.String repository,
java.lang.String id,
java.lang.String property)
Clear all linked entities of a specific property.
|
org.springframework.core.convert.ConversionService |
conversionService() |
RepositoryRestController |
conversionServices(java.util.List<org.springframework.core.convert.ConversionService> conversionServices) |
org.springframework.http.ResponseEntity<?> |
create(org.springframework.http.server.ServletServerHttpRequest request,
java.net.URI baseUri,
java.lang.String repository)
Create a new entity by reading the incoming data and calling
CrudRepository.save(Object) and letting the
ID be auto-generated. |
org.springframework.http.ResponseEntity<?> |
createOrUpdate(org.springframework.http.server.ServletServerHttpRequest request,
java.net.URI baseUri,
java.lang.String repository,
java.lang.String id)
Create an entity with a specific ID or update an existing entity.
|
org.springframework.http.ResponseEntity<?> |
deleteEntity(org.springframework.http.server.ServletServerHttpRequest request,
java.lang.String repository,
java.lang.String id)
Delete an entity.
|
org.springframework.http.ResponseEntity<?> |
deleteLink(org.springframework.http.server.ServletServerHttpRequest request,
java.lang.String repository,
java.lang.String id,
java.lang.String property,
java.lang.String linkedId)
Delete a specific relationship between a child entity and its parent.
|
org.springframework.http.ResponseEntity<?> |
entity(org.springframework.http.server.ServletServerHttpRequest request,
java.net.URI baseUri,
java.lang.String repository,
java.lang.String id)
Retrieve a specific entity.
|
org.springframework.core.convert.ConversionService |
getConversionService()
Get the
ConversionService in use by the controller. |
UriToDomainObjectUriResolver |
getDomainObjectResolver() |
java.util.List<org.springframework.http.converter.HttpMessageConverter> |
getHttpMessageConverters()
Get the list of default
HttpMessageConverter s. |
RepositoryAwareMappingHttpMessageConverter |
getMappingHttpMessageConverter() |
RepositoryRestConfiguration |
getRepositoryRestConfig()
Get the configuration currently in use.
|
org.springframework.http.ResponseEntity |
handleConflict(java.lang.Exception ex,
org.springframework.http.server.ServletServerHttpRequest request)
Send a 409 Conflict in case of concurrent modification.
|
org.springframework.http.ResponseEntity |
handleJsr303ValidationFailure(javax.validation.ConstraintViolationException ex,
org.springframework.http.server.ServletServerHttpRequest request)
Send a 400 Bad Request in case of a validation failure.
|
org.springframework.http.ResponseEntity |
handleMessageConversionFailure(java.lang.Exception ex,
javax.servlet.http.HttpServletRequest request)
Send a 400 Bad Request in case no converter was found to process the input or output.
|
org.springframework.http.ResponseEntity |
handleMiscFailures(java.lang.Throwable t,
org.springframework.http.server.ServletServerHttpRequest request)
Handle failures commonly thrown from code tries to read incoming data and convert or cast it to the right type.
|
org.springframework.http.ResponseEntity |
handleNPE(java.lang.NullPointerException e,
org.springframework.http.server.ServletServerHttpRequest request)
Handle NPEs as a regular 500 error.
|
org.springframework.http.ResponseEntity |
handleRepositoryNotFoundFailure(RepositoryNotFoundException e,
org.springframework.http.server.ServletServerHttpRequest request)
Send a 404 if no repository was found.
|
org.springframework.http.ResponseEntity |
handleValidationFailure(RepositoryConstraintViolationException ex,
org.springframework.http.server.ServletServerHttpRequest request)
Send a 400 Bad Request in case of a validation failure.
|
java.util.List<org.springframework.http.converter.HttpMessageConverter> |
httpMessageConverters() |
RepositoryRestController |
httpMessageConverters(java.util.List<org.springframework.http.converter.HttpMessageConverter> httpMessageConverters) |
org.springframework.http.ResponseEntity<?> |
linkedEntity(org.springframework.http.server.ServletServerHttpRequest request,
java.net.URI baseUri,
java.lang.String repository,
java.lang.String id,
java.lang.String property,
java.lang.String linkedId)
Retrieve a linked entity from a parent entity.
|
org.springframework.http.ResponseEntity<?> |
listEntities(org.springframework.http.server.ServletServerHttpRequest request,
PagingAndSorting pageSort,
java.net.URI baseUri,
java.lang.String repository)
List entities of a
CrudRepository by invoking
CrudRepository.findAll()
and applying any available paging parameters. |
org.springframework.http.ResponseEntity<?> |
listQueryMethods(org.springframework.http.server.ServletServerHttpRequest request,
java.net.URI baseUri,
java.lang.String repository)
List the URIs of query methods found on this repository interface.
|
org.springframework.http.ResponseEntity<?> |
listRepositories(org.springframework.http.server.ServletServerHttpRequest request,
java.net.URI baseUri)
List available
CrudRepository s that are being exported. |
org.springframework.http.ResponseEntity<?> |
propertyOfEntity(org.springframework.http.server.ServletServerHttpRequest request,
java.net.URI baseUri,
java.lang.String repository,
java.lang.String id,
java.lang.String property)
Retrieve the property of an entity.
|
org.springframework.http.ResponseEntity<?> |
query(org.springframework.http.server.ServletServerHttpRequest request,
PagingAndSorting pageSort,
java.net.URI baseUri,
java.lang.String repository,
java.lang.String query)
Invoke a custom query method on a repository and page the results based on URL parameters supplied by the user or
the default page size.
|
void |
setApplicationContext(org.springframework.context.ApplicationContext applicationContext) |
void |
setConversionServices(java.util.List<org.springframework.core.convert.ConversionService> conversionServices)
Add these
ConversionService s to the list of those being delegated to by the internal DelegatingConversionService . |
RepositoryRestController |
setDomainObjectResolver(UriToDomainObjectUriResolver domainObjectResolver) |
void |
setHttpMessageConverters(java.util.List<org.springframework.http.converter.HttpMessageConverter> httpMessageConverters)
Set the list of available
HttpMessageConverter s, clobbering the defaults. |
RepositoryRestController |
setMappingHttpMessageConverter(RepositoryAwareMappingHttpMessageConverter mappingHttpMessageConverter) |
RepositoryRestController |
setRepositoryRestConfig(RepositoryRestConfiguration config)
Set the configuration this controller will use to inflence its behavior.
|
org.springframework.http.ResponseEntity<?> |
updatePropertyOfEntity(org.springframework.http.server.ServletServerHttpRequest request,
java.net.URI baseUri,
java.lang.String repository,
java.lang.String id,
java.lang.String property)
Update the property of an entity if that property is also managed by a
CrudRepository . |
getRepositoryExporters, hasRepositoryMetadataFor, hasRepositoryMetadataFor, repositoryExporters, repositoryExporters, repositoryExporters, repositoryMetadataFor, repositoryMetadataFor, repositoryMetadataFor, setRepositoryExporters
public static final java.lang.String LOCATION
public static final java.lang.String SELF
public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) throws org.springframework.beans.BeansException
setApplicationContext
in interface org.springframework.context.ApplicationContextAware
org.springframework.beans.BeansException
public org.springframework.core.convert.ConversionService getConversionService()
ConversionService
in use by the controller.ConversionService
s.@Autowired(required=false) public void setConversionServices(java.util.List<org.springframework.core.convert.ConversionService> conversionServices)
ConversionService
s to the list of those being delegated to by the internal DelegatingConversionService
. Although this method does an 'add', it is called 'set' to make it JavaBean-friendly.conversionServices
- public org.springframework.core.convert.ConversionService conversionService()
ConversionService
.getConversionService()
public RepositoryRestController conversionServices(java.util.List<org.springframework.core.convert.ConversionService> conversionServices)
conversionServices
- setConversionServices(java.util.List)
public java.util.List<org.springframework.http.converter.HttpMessageConverter> getHttpMessageConverters()
HttpMessageConverter
s.public void setHttpMessageConverters(java.util.List<org.springframework.http.converter.HttpMessageConverter> httpMessageConverters)
HttpMessageConverter
s, clobbering the defaults. This does not, however, affect
those user-defined converters that come from the RepositoryRestConfiguration
.httpMessageConverters
- public java.util.List<org.springframework.http.converter.HttpMessageConverter> httpMessageConverters()
getHttpMessageConverters()
public RepositoryRestController httpMessageConverters(java.util.List<org.springframework.http.converter.HttpMessageConverter> httpMessageConverters)
httpMessageConverters
- setHttpMessageConverters(java.util.List)
public RepositoryRestConfiguration getRepositoryRestConfig()
@Autowired(required=false) public RepositoryRestController setRepositoryRestConfig(RepositoryRestConfiguration config)
config
- public RepositoryAwareMappingHttpMessageConverter getMappingHttpMessageConverter()
@Autowired public RepositoryRestController setMappingHttpMessageConverter(RepositoryAwareMappingHttpMessageConverter mappingHttpMessageConverter)
public UriToDomainObjectUriResolver getDomainObjectResolver()
@Autowired public RepositoryRestController setDomainObjectResolver(UriToDomainObjectUriResolver domainObjectResolver)
public void afterPropertiesSet() throws java.lang.Exception
afterPropertiesSet
in interface org.springframework.beans.factory.InitializingBean
java.lang.Exception
@RequestMapping(value="/", method=GET) @ResponseBody public org.springframework.http.ResponseEntity<?> listRepositories(org.springframework.http.server.ServletServerHttpRequest request, java.net.URI baseUri) throws java.io.IOException
CrudRepository
s that are being exported.request
- baseUri
- java.io.IOException
@RequestMapping(value="/{repository}", method=GET) @ResponseBody public org.springframework.http.ResponseEntity<?> listEntities(org.springframework.http.server.ServletServerHttpRequest request, PagingAndSorting pageSort, java.net.URI baseUri, @PathVariable java.lang.String repository) throws java.io.IOException
CrudRepository
by invoking
CrudRepository.findAll()
and applying any available paging parameters.request
- pageSort
- baseUri
- repository
- java.io.IOException
@RequestMapping(value="/{repository}/search", method=GET) @ResponseBody public org.springframework.http.ResponseEntity<?> listQueryMethods(org.springframework.http.server.ServletServerHttpRequest request, java.net.URI baseUri, @PathVariable java.lang.String repository) throws java.io.IOException
request
- baseUri
- repository
- java.io.IOException
@RequestMapping(value="/{repository}/search/{query}", method=GET) @ResponseBody public org.springframework.http.ResponseEntity<?> query(org.springframework.http.server.ServletServerHttpRequest request, PagingAndSorting pageSort, java.net.URI baseUri, @PathVariable java.lang.String repository, @PathVariable java.lang.String query) throws java.lang.reflect.InvocationTargetException, java.lang.IllegalAccessException, java.io.IOException
request
- pageSort
- baseUri
- repository
- query
- java.lang.reflect.InvocationTargetException
java.lang.IllegalAccessException
java.io.IOException
@RequestMapping(value="/{repository}", method=POST) @ResponseBody public org.springframework.http.ResponseEntity<?> create(org.springframework.http.server.ServletServerHttpRequest request, java.net.URI baseUri, @PathVariable java.lang.String repository) throws java.io.IOException
CrudRepository.save(Object)
and letting the
ID be auto-generated.
To get the entity back in the body of the response, simpy add the URL parameter returnBody=true.
request
- baseUri
- repository
- java.io.IOException
@RequestMapping(value="/{repository}/{id}", method=GET) @ResponseBody public org.springframework.http.ResponseEntity<?> entity(org.springframework.http.server.ServletServerHttpRequest request, java.net.URI baseUri, @PathVariable java.lang.String repository, @PathVariable java.lang.String id) throws java.io.IOException
request
- baseUri
- repository
- id
- java.io.IOException
@RequestMapping(value="/{repository}/{id}", method=PUT) @ResponseBody public org.springframework.http.ResponseEntity<?> createOrUpdate(org.springframework.http.server.ServletServerHttpRequest request, java.net.URI baseUri, @PathVariable java.lang.String repository, @PathVariable java.lang.String id) throws java.io.IOException, java.lang.IllegalAccessException, java.lang.InstantiationException
request
- baseUri
- repository
- id
- java.io.IOException
java.lang.IllegalAccessException
java.lang.InstantiationException
@RequestMapping(value="/{repository}/{id}", method=DELETE) @ResponseBody public org.springframework.http.ResponseEntity<?> deleteEntity(org.springframework.http.server.ServletServerHttpRequest request, @PathVariable java.lang.String repository, @PathVariable java.lang.String id) throws java.io.IOException
request
- repository
- id
- java.io.IOException
@RequestMapping(value="/{repository}/{id}/{property}", method=GET) @ResponseBody public org.springframework.http.ResponseEntity<?> propertyOfEntity(org.springframework.http.server.ServletServerHttpRequest request, java.net.URI baseUri, @PathVariable java.lang.String repository, @PathVariable java.lang.String id, @PathVariable java.lang.String property) throws java.io.IOException
request
- baseUri
- repository
- id
- property
- java.io.IOException
@RequestMapping(value="/{repository}/{id}/{property}", method={PUT,POST}) @ResponseBody public org.springframework.http.ResponseEntity<?> updatePropertyOfEntity(org.springframework.http.server.ServletServerHttpRequest request, java.net.URI baseUri, @PathVariable java.lang.String repository, @PathVariable java.lang.String id, @PathVariable java.lang.String property) throws java.io.IOException
CrudRepository
.request
- baseUri
- repository
- id
- property
- java.io.IOException
@RequestMapping(value="/{repository}/{id}/{property}", method=DELETE) @ResponseBody public org.springframework.http.ResponseEntity<?> clearLinks(org.springframework.http.server.ServletServerHttpRequest request, @PathVariable java.lang.String repository, @PathVariable java.lang.String id, @PathVariable java.lang.String property) throws java.io.IOException
request
- repository
- id
- property
- java.io.IOException
@RequestMapping(value="/{repository}/{id}/{property}/{linkedId}", method=GET) @ResponseBody public org.springframework.http.ResponseEntity<?> linkedEntity(org.springframework.http.server.ServletServerHttpRequest request, java.net.URI baseUri, @PathVariable java.lang.String repository, @PathVariable java.lang.String id, @PathVariable java.lang.String property, @PathVariable java.lang.String linkedId) throws java.io.IOException
request
- baseUri
- repository
- id
- property
- linkedId
- java.io.IOException
@RequestMapping(value="/{repository}/{id}/{property}/{linkedId}", method=DELETE) @ResponseBody public org.springframework.http.ResponseEntity<?> deleteLink(org.springframework.http.server.ServletServerHttpRequest request, @PathVariable java.lang.String repository, @PathVariable java.lang.String id, @PathVariable java.lang.String property, @PathVariable java.lang.String linkedId) throws java.io.IOException
request
- repository
- id
- property
- linkedId
- java.io.IOException
@ExceptionHandler(value=RepositoryNotFoundException.class) @ResponseBody public org.springframework.http.ResponseEntity handleRepositoryNotFoundFailure(RepositoryNotFoundException e, org.springframework.http.server.ServletServerHttpRequest request) throws java.io.IOException
e
- request
- java.io.IOException
@ExceptionHandler(value=java.lang.NullPointerException.class) @ResponseBody public org.springframework.http.ResponseEntity handleNPE(java.lang.NullPointerException e, org.springframework.http.server.ServletServerHttpRequest request) throws java.io.IOException
e
- request
- java.io.IOException
@ExceptionHandler(value={java.lang.reflect.InvocationTargetException.class,java.lang.IllegalArgumentException.class,java.lang.ClassCastException.class,org.springframework.core.convert.ConversionFailedException.class}) @ResponseBody public org.springframework.http.ResponseEntity handleMiscFailures(java.lang.Throwable t, org.springframework.http.server.ServletServerHttpRequest request) throws java.io.IOException
t
- request
- java.io.IOException
@ExceptionHandler(value={org.springframework.dao.OptimisticLockingFailureException.class,org.springframework.dao.DataIntegrityViolationException.class}) @ResponseBody public org.springframework.http.ResponseEntity handleConflict(java.lang.Exception ex, org.springframework.http.server.ServletServerHttpRequest request) throws java.io.IOException
ex
- request
- java.io.IOException
@ExceptionHandler(value=RepositoryConstraintViolationException.class) @ResponseBody public org.springframework.http.ResponseEntity handleValidationFailure(RepositoryConstraintViolationException ex, org.springframework.http.server.ServletServerHttpRequest request) throws java.io.IOException
ex
- request
- java.io.IOException
@ExceptionHandler(value=javax.validation.ConstraintViolationException.class) @ResponseBody public org.springframework.http.ResponseEntity handleJsr303ValidationFailure(javax.validation.ConstraintViolationException ex, org.springframework.http.server.ServletServerHttpRequest request) throws java.io.IOException
ex
- request
- java.io.IOException
@ExceptionHandler(value={org.springframework.http.converter.HttpMessageNotReadableException.class,org.springframework.http.converter.HttpMessageNotWritableException.class}) @ResponseBody public org.springframework.http.ResponseEntity handleMessageConversionFailure(java.lang.Exception ex, javax.servlet.http.HttpServletRequest request) throws java.io.IOException
ex
- request
- java.io.IOException