Introduction to Spring Data Jpa
Go Back to Course Outline Repositories https://github.com/terawarehouse/terawarehouse-catalog https://github.com/terawarehouse/...
https://www.czetsuyatech.com/2019/08/spring-data-jpa-introduction.html
Repositories
- https://github.com/terawarehouse/terawarehouse-catalog
- https://github.com/terawarehouse/terawarehouse-react
We are now in the final video for this set on REST API. And this will be about the Spring Data JPA. Which is a Spring dependency that lets us exposed REST services from the repositories we have in our project. Remember, in our earlier presentations we have created an endpoint for the category at /v1/categories.
Before opening the HAL browser, it’s common to change the URL by adding an API suffix, so let’s open the application.properties file and add the configuration below.
[open application.properties]
spring.data.rest.base-path=api
[open the browser, navigate to http://localhost:8080/api]
We should be able to see the list of available repository endpoints from our project like categories, products, dealers, etc. And you can play with these endpoints in the HAL browser, figuring out the parameters.
Another important thing to remember is that we can exclude a repository in the auto-generated endpoints if we want to, we just need to annotate the repository with @RepositoryRestResource(exported = false). In case it’s the method we want to exclude, just override that method and annotate with @RestResource(exported = false).
[open BrandRepository class]
And finally, to enable Swagger documentation we need to add the following dependencies in the project’s pom file.
<properties> <springfox.version>3.0.0-SNAPSHOT</springfox.version> </properties> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${springfox.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${springfox.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-data-rest</artifactId> <version>${springfox.version}</version> </dependency>
Unfortunately, the previous version of Swagger (2) doesn’t work on the version of Spring booth we are using so we will be making some changes on the Swagger config class.
[open SwaggerConfig class]
Change the annotation from Swagger2 to
@EnableSwagger2WebMvc
@Import({ SpringDataRestConfiguration.class })
And that’s it for this section. Next, we will start working on a microservice architecture or possibly our client app which will be in React. Which do you prefer? I hope you and joy and keep learning!
Post a Comment