Last active
December 8, 2019 21:44
-
-
Save iznenad/424d49696f78cd7563e11ff5d51f21f4 to your computer and use it in GitHub Desktop.
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.example.iznenad; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.data.mapping.context.PersistentEntities; | |
import org.springframework.data.projection.SpelAwareProxyProjectionFactory; | |
import org.springframework.data.rest.core.config.RepositoryRestConfiguration; | |
import org.springframework.data.rest.core.projection.ProjectionDefinitions; | |
import org.springframework.data.rest.core.support.SelfLinkProvider; | |
import org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler; | |
import org.springframework.data.rest.webmvc.mapping.Associations; | |
import org.springframework.data.rest.webmvc.support.PersistentEntityProjector; | |
import org.springframework.data.rest.webmvc.support.Projector; | |
import org.springframework.web.context.request.NativeWebRequest; | |
/** | |
* @author Nenad Nikolic | |
* | |
* | |
*/ | |
public class DefaultSpringDataRestMagic { | |
@Autowired | |
private Associations associations; | |
@Autowired | |
private RepositoryRestConfiguration repositoryRestConfiguration; | |
@Autowired | |
private ApplicationContext applicationContext; | |
@Autowired | |
private SelfLinkProvider linkProvider; | |
@Autowired | |
private PersistentEntities entities; | |
@Autowired | |
private NativeWebRequest nativeWebRequest; | |
public PersistentEntityResourceAssembler buildDefaultAssemblerInstance() { | |
SpelAwareProxyProjectionFactory projectionFactory = new SpelAwareProxyProjectionFactory(); | |
projectionFactory.setBeanFactory(applicationContext); | |
projectionFactory.setResourceLoader(applicationContext); | |
ProjectionDefinitions projectionDefinitions = repositoryRestConfiguration.getProjectionConfiguration(); | |
Projector projector = new PersistentEntityProjector(projectionDefinitions, projectionFactory, | |
nativeWebRequest.getParameter(projectionDefinitions.getParameterName()), associations.getMappings()); | |
return new PersistentEntityResourceAssembler( | |
entities, projector, associations, linkProvider); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Overriding a spring-data-rest endpoint and using the default
ResourceAssembler
Lets say you have the following repository:
and you need to override one of the methods.
For this example we'll override:
GET /users
(which is the defaultfindAll
) so that we can take a parameter from a header.Provide the
RepositoryRestController
While this works, it will lack some of the hateoas goodies that spring-data-rest gives us by default.
So lets reuse the default
ResourceAssembler
to get them back.Create the
DefaultSpringDataRestMagic
class and register as beanJust copy the class from this gist, change the package and register it as a bean like this:
It needs to be request scoped because it injects an instance of
NativeWebRequest
.Change the
RepositoryRestController
to use the default assemblyThat's it.