{"id":2562,"date":"2016-01-06T20:22:56","date_gmt":"2016-01-06T18:22:56","guid":{"rendered":"https:\/\/adrhc.go.ro\/wordpress\/?p=2562"},"modified":"2019-05-29T10:33:39","modified_gmt":"2019-05-29T08:33:39","slug":"spring-data-rest","status":"publish","type":"post","link":"https:\/\/adrhc.go.ro\/blog\/spring-data-rest\/","title":{"rendered":"Spring Data REST"},"content":{"rendered":"<pre>\r\nThe core functionality of Spring Data REST is to export resources for Spring Data repositories.\r\nSpring Data REST exposes sub-resources of every item resource for each of the associations the item resource has. By default, Spring Data REST uses <a href=\"http:\/\/stateless.co\/hal_specification.html\" target=\"_blank\" rel=\"noopener noreferrer\">HAL (hypertext application language)<\/a> to render responses.\r\n\r\nRepositoryRestMvcConfiguration - the @Configuration for Spring Data REST\r\nRepositoryRestConfigurerAdapter - used to customize the configuration\r\n\r\nBy default, Spring Data REST serves up REST resources at the root URI, \"\/\". Register a @Bean which extends RepositoryRestConfigurerAdapter and overwrite configureRepositoryRestConfiguration in order to change it. With spring boot use <em>spring.data.rest.basePath<\/em>=\/new-base-path-for-spring-data-rest in application.properties.\r\nhttp:\/\/ip:port\/base-path-for-spring-data-rest returns a JSON with the first level of resources provided with Spring Data REST.\r\n\r\n@RepositoryRestResource - used on a Repository interface to customize the path and name of the resource\r\n@RestResource - used on a method to customize the path and name of the exported resource; use exported = false in order not to export the resource\r\n\r\nAnnotation org.springframework.data.rest.core.config.Projection used to define altered views for entities (e.g.: not showing the password for user).\r\nURI would look like: http:\/\/localhost:8080\/persons\/1?projection=noPassword\r\n@Projection can be used with SpEL expression to create new properties.\r\nAn excerpt is a projection that is applied to a resource collection automatically, e.g.:\r\n@RepositoryRestResource(excerptProjection = NoPassword.class)\r\ninterface PersonRepository extends CrudRepository<Person, Long> {}\r\n\r\nValidation is achived using a registered org.springframework.validation.Validator instance:\r\na. <em>beforeCreate<\/em>PersonValidator -> prefix the bean name with the Data REST event name\r\nb. assigning Validators manually -> override RepositoryRestMvcConfiguration.configureValidatingRepositoryEventListener and add the validators\r\n\r\nEvents: BeforeCreateEvent, AfterCreateEvent, BeforeSaveEvent, AfterSaveEvent, BeforeLinkSaveEvent, AfterLinkSaveEvent, BeforeDeleteEvent, AfterDeleteEvent. Writing an ApplicationListener:\r\na. extend AbstractRepositoryEventListener (onBeforeSave, etc) -> makes no distinction based on the type of the entity\r\nb. write an annotated handler -> @RepositoryEventHandler on class and @HandleBeforeSave on method (first parameter determines the domain type whose events you\u2019re interested)\r\n\r\nCreating links from Spring MVC controllers to Spring Data REST resources is possible with injectable RepositoryEntityLinks (equivalent to EntityLinks from Spring HATEOAS): entityLinks.linkToCollectionResource(Person.class).\r\n\r\nALPS (Application-Level Profile Semantics) is a data format for defining simple descriptions of application-level semantics.\r\nALPS (application\/alps+json or application\/schema+json) -> used for metadata provided by a Spring Data REST-based application.\r\nhttp:\/\/ip:port\/base-path-for-spring-data-rest\/profile -> metadata location\r\nALPS list first entity's attributes (+ projections) then operations.\r\n\r\nSpring security works fine with Spring Data REST; just use @PreAuthorize as usual.\r\n\r\nThe developer of the HAL spec has a useful application: <a href=\"https:\/\/github.com\/mikekelly\/hal-browser\" target=\"_blank\" rel=\"noopener noreferrer\">the HAL Browser<\/a>.\r\n\r\nWrite a custom handler (overriding Spring Data REST response handlers) for a specific resource using @RepositoryRestController (instead of @RestController). There's a custom HandlerMapping instance that responds only to the RepositoryRestController which is considered before the one for @Controller or @RestController.\r\n\r\nUse ResourceProcessor interface in order to provide links to other resources from a particular entity. You declare spring beans implementing ResourceProcessor for a specific entity.\r\nIf you register your own ConversionService in the ApplicationContext and register your own Converter<Person, Resource>, then you can return a Resource implementation of your choosing.\r\n\r\n<strong>Maven dependency<\/strong>\r\n&lt;dependency>\r\n  &lt;groupId>org.springframework.data&lt;\/groupId>\r\n  &lt;artifactId>spring-data-rest-webmvc&lt;\/artifactId>\r\n  &lt;version>2.4.2.RELEASE&lt;\/version>\r\n&lt;\/dependency>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The core functionality of Spring Data REST is to export resources for Spring Data repositories. Spring Data REST exposes sub-resources of every item resource for each of the associations the item resource has. By default, Spring Data REST uses HAL [&hellip;]<\/p>\n<div class=\"link-more\"><a href=\"https:\/\/adrhc.go.ro\/blog\/spring-data-rest\/#more-2562\" class=\"more-link\">Continue reading &#10142; <span class=\"screen-reader-text\">Spring Data REST<\/span><\/a><\/div>","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36,10],"tags":[70,69,68],"class_list":["post-2562","post","type-post","status-publish","format-standard","hentry","category-java","category-programming","tag-hal","tag-rest","tag-spring"],"_links":{"self":[{"href":"https:\/\/adrhc.go.ro\/blog\/wp-json\/wp\/v2\/posts\/2562","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/adrhc.go.ro\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/adrhc.go.ro\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/adrhc.go.ro\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/adrhc.go.ro\/blog\/wp-json\/wp\/v2\/comments?post=2562"}],"version-history":[{"count":0,"href":"https:\/\/adrhc.go.ro\/blog\/wp-json\/wp\/v2\/posts\/2562\/revisions"}],"wp:attachment":[{"href":"https:\/\/adrhc.go.ro\/blog\/wp-json\/wp\/v2\/media?parent=2562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/adrhc.go.ro\/blog\/wp-json\/wp\/v2\/categories?post=2562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/adrhc.go.ro\/blog\/wp-json\/wp\/v2\/tags?post=2562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}