Springboot (2.0.4.RELEASE) +elasticsearch (6.2.4) +gradle Simple integration

Source: Internet
Author: User

Record a small example of Springboot (2.0.4.RELEASE) +elasticsearch (6.2.4) +gradle integration.

1. Add dependencies to the relevant jar package within Gradle:

Compile (' Org.springframework.boot:spring-boot-starter-web ') compile (' Org.springframework.boot: Spring-boot-starter-thymeleaf ') compile (' ORG.SPRINGFRAMEWORK.BOOT:SPRING-BOOT-STARTER-DATA-JPA ')//Add Spring data Elasticsearch relies on compile (' org.springframework.boot:spring-boot-starter-data-elasticsearch ')//Add JNA dependency compile (' net.java.dev.jna:jna:4.3.0 ')

Compile (' Com.google.guava:guava:26.0-jre ')

2. Create the Entity object and add the relevant notes for Elasticsearch:

Package Com.wey.pojo.blog;import Java.io.serializable;import Org.springframework.data.annotation.id;import Org.springframework.data.elasticsearch.annotations.Document, @Document (indexname= "Blogcenter", type= "blog")// IndexName index name can be understood as the database name must be lowercase or it will be reported public class Blog implements serializable{private static final long Serialversionuid        = 1L;    @Id private String Id;    Private String title;    Private String Summary;        Private String content;    Protected Blog () {super ();        Public Blog (string title, string summary, string content) {this.title = title;        This.summary = Summary;    this.content = content;    } public String GetId () {return id;    } public void SetId (String id) {this.id = ID;    } public String GetTitle () {return title;    public void Settitle (String title) {this.title = title;    } public String Getsummary () {return summary; } public void SetSuMmary (String summary) {this.summary = summary;    } public String GetContent () {return content;    The public void SetContent (String content) {this.content = content; } @Override Public String toString () {return "Blog [id=" + ID + ", title=" + title + ", summary=" + Summary    + ", content=" + content + "]"; }}

3. Create Repository

Package Com.wey.repository;import Org.springframework.data.domain.page;import Org.springframework.data.domain.pageable;import Org.springframework.data.elasticsearch.repository.elasticsearchrepository;import Org.springframework.stereotype.component;import Com.wey.pojo.blog.blog;public Interface BlogRepository extends Elasticsearchrepository<blog, string> {}

4. Create a controller and simply implement add and query

@RestController @requestmapping ("/blogs") public class BlogController {     @Autowired     blogrepository Blogrepository;    @RequestMapping ("/add") Public    Blog Add (blog blog) {        return blogrepository.save (blog);    }           @GetMapping public    list<blog> findAll () {        iterable<blog> elements = Blogrepository.findall ();        arraylist<blog> list = lists.newarraylist (elements);        return list;    }        @GetMapping ("/delete/{id}") Public    string Remove (@PathVariable (name= "id") string id) {        Blogrepository.deletebyid (ID);        Return "Success";    }}

5. Open the Elasticsearch.bat file in the downloaded Elasticsearch (6.2.4) and wait a few moments until the boot is complete.

6. Start the Springboot application and simply test

Add a piece of data:

Query all data:

Springboot (2.0.4.RELEASE) +elasticsearch (6.2.4) +gradle Simple integration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.