In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to create a Springboot project with Spring Initializr". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Brief introduction
Springboot and Spring MVC simplify RESTful development in web applications, but there is a simpler one, which is Spring Data REST. Spring Data REST is built on top of Data Repository, which can directly expose resository as a Web service in HATEOAS style, eliminating the need for a handwritten Controller layer.
HATEOAS, or Hypermedia as the Engine of Application State, is a more mature REST model, which contains link information in the expression of resources, and the client can discover executable actions according to the link.
Spring Data REST supports Spring Data JPA, Spring Data MongoDB, Spring Data Neo4j, Spring Data GenFire and Spring Data Cassandra. Choose the familiar JPA here.
2 give an example
Let's feel it with examples.
2.1 create a project
We use Spring Initializr to quickly create Springboot projects. The selected dependent components are as follows:
(1) Spring Web: provide Web service
(2) Rest Repositories: provide support for Spring Data REST
(3) Spring Data JPA: provide Repository data access through JPA
(4) H2 Database:H2 database, which is used for convenience and simplicity.
(recommended course: Spring tutorial)
The dependency in the corresponding pom.xml after import is as follows:
Org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-data-rest com.h3database h3 runtime
2.2 entity class
Create an entity class User, as follows:
Package com.pkslow.rest.entity
Import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;@Entitypublic class User {@ Id @ GeneratedValue (strategy = GenerationType.AUTO) private Integer id; private String name; private Integer age; private String email; / / getter & setter} 2.3 Repository interface definition
Define the Repository interface to manipulate the database, as follows:
Package com.pkslow.rest.repo
Import com.pkslow.rest.entity.User;import org.springframework.data.repository.CrudRepository;import org.springframework.data.rest.core.annotation.RepositoryRestResource;@RepositoryRestResource (path = "user") public interface UserRepository extends CrudRepository {}
Note that RepositoryRestResource is used by Data REST to expose Repository,path as the access path. If it is set to user, the access address is http://localhost:8080/user.
2.4 initiate access
With the above code ready, just start the Springboot application directly. We set the port to 8080.
3 explore more 3.1 paging and sorting functions
You can quickly implement paging and sorting functions by changing the parent interface of Repository to PagingAndSortingRepository, as shown below:
@ RepositoryRestResource (path = "user") public interface UserRepository extends PagingAndSortingRepository {}
In fact, there are two more methods, findAll (Sort var1) and findAll (Pageable var1), as follows:
Public interface PagingAndSortingRepository extends CrudRepository {Iterable findAll (Sort var1)
Page findAll (Pageable var1);}
Query http://localhost:8080/user?page=1&size=2&sort=id,desc, which indicates that the second page of the query, with 2 records per page, is displayed in reverse order of ID. As follows:
{"_ embedded": {"users": [{"name": "pkslow.com", "age": 18, "email": "pkslow@pkslow.com", "_ links": {"self": {"href": "http://localhost:8080/user/33"}," user ": {" href ":" http://localhost:8080/user/33"} " {"name": "pkslow.com", "age": 18, "email": "pkslow@pkslow.com", "_ links": {"self": {"href": "http://localhost:8080/user/32"}," user ": {" href ":" http://localhost:8080/user/32"}]} "_ links": {"first": {"href": "http://localhost:8080/user?page=0&size=2&sort=id,desc"}," prev ": {" href ":" http://localhost:8080/user?page=0&size=2&sort=id,desc"}, "self": {"href": "http://localhost:8080/user?page=1&size=2&sort=id,desc"}" "next": {"href": "http://localhost:8080/user?page=2&size=2&sort=id,desc"}," last ": {" href ":" http://localhost:8080/user?page=17&size=2&sort=id,desc"}, "profile": {"href": "http://localhost:8080/profile/user"}," page ": {" size ": 2," totalElements ": 35," totalPages ": 18 "number": 1}}
You can find that page starts at 0, and 1 represents the second page; the return result also provides links to the first page, previous page, this page, next page, and last page; and paging information.
(recommended micro-class: Spring micro-class)
3.2 event monitoring
REST provides eight Repository-based events, as follows:
BeforeCreateEvent
AfterCreateEvent
BeforeSaveEvent
AfterSaveEvent
BeforeLinkSaveEvent
AfterLinkSaveEvent
BeforeDeleteEvent
AfterDeleteEvent
Add a custom event as follows:
Package com.pkslow.rest.event
Import com.pkslow.rest.entity.User;import org.springframework.data.rest.core.event.AbstractRepositoryEventListener;import org.springframework.stereotype.Component;@Componentpublic class PkslowEventListener extends AbstractRepositoryEventListener {@ Override public void onBeforeCreate (User entity) {System.out.println ("pkslow creating:" + entity);} @ Override public void onBeforeSave (User entity) {System.out.println ("pkslow saving:" + entity) } @ Override public void onAfterDelete (User entity) {System.out.println ("pkslow deleted:" + entity);}}
After adding, modifying and deleting respectively, the log is as follows:
Pkslow creating:User {id=null, name='pkslow.com', age=18, email='pkslow@pkslow.com'} pkslow saving:User {id=32, name='pkslow.com', age=20, email='pkslow@pkslow.com'} pkslow deleted:User {id=14, name='pkslow.com', age=18, email='pkslow@pkslow.com'}
It shows that the event was executed successfully. Combined with this function, you can achieve a lot of business logic, such as recording the operation log after deletion and deleting other related data.
(recommended tutorial: things like Spring Boot)
3.3 path
The default base path is /, which can be configured through spring.data.rest.base-path=api, which becomes localhost:8080/api/user.
4 Integrated HAL Browser View
HAL Browser is a front-end tool dedicated to browsing JSON Hypertext Application Language-based tools. We have already provided a HATEOAS-style RESTful service, which can be easily viewed by HAL Browser.
Add dependencies:
Org.springframework.data spring-data-rest-hal-browser 3.3.2.RELEASE
Access http://localhost:8080/browser/index.html#/ after startup
This is the end of "how to create a Springboot Project with Spring Initializr". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.