`
sillycat
  • 浏览: 2566770 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Spring Boot and RESTful API(2)SOLR Repository

    博客分类:
  • JAVA
 
阅读更多
Spring Boot and RESTful API(2)SOLR Repository


Install SOLR on local
>wget http://apache.mirrors.tds.net/lucene/solr/6.6.0/solr-6.6.0.zip

Unzip and place it in the right directory, Start the Server on Local
>bin/solr start -p 8984

Visit page
http://localhost:8983/solr/#/

But there is no core there, so I follow this link and try to create one core named collection1
https://stackoverflow.com/questions/38424574/how-to-create-solr-6-cores

>cd /opt/solr/server/solr
>mkdir job
>cp -r configsets/basic_configs/conf ./job/conf

Visit the UI - click on Add Core — Put name and instanceDir ‘job'
http://localhost:8983/solr/#/~cores

Click on Schema and ‘Add Field’ to add filed.

Fetch Content from SOLR
query documents
https://github.com/spring-projects/spring-data-solr

Some Dependencies on pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
</dependency>

POJO Job.java

package com.sillycat.jobsmonitorapi.domain;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.solr.client.solrj.beans.Field;
import org.springframework.data.annotation.Id;
import org.springframework.data.solr.core.mapping.SolrDocument;

@SolrDocument(solrCoreName = "job")
public class Job {

@Id
@Field
private String id;

@Field("source_id")
private Integer sourceID;

@Field("job_reference")
private String jobReference;

public Job() {
}

public Job(String id, Integer sourceID, String jobReference) {
super();
this.id = id;
this.sourceID = sourceID;
this.jobReference = jobReference;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public Integer getSourceID() {
return sourceID;
}

public void setSourceID(Integer sourceID) {
this.sourceID = sourceID;
}

public String getJobReference() {
return jobReference;
}

public void setJobReference(String jobReference) {
this.jobReference = jobReference;
}

public String toString() {
return ToStringBuilder.reflectionToString(this);
}

}

JobRepositorySolr.java which is a interface
package com.sillycat.jobsmonitorapi.repository;

import java.util.List;

import org.springframework.data.solr.repository.Query;
import org.springframework.data.solr.repository.SolrCrudRepository;

import com.sillycat.jobsmonitorapi.domain.Job;

public interface JobRepositorySolr extends SolrCrudRepository<Job, String> {

@Query(fields = { "job_reference" })
List<Job> findBySourceID(Integer sourceID);
}

Unit Tests for that Repository, JobRepositorySolrTest.java
package com.sillycat.jobsmonitorapi.repository;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.IteratorUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.Assert;

import com.sillycat.jobsmonitorapi.domain.Job;

@RunWith(SpringRunner.class)
@SpringBootTest
public class JobRepositorySolrTest {
@Autowired
    JobRepositorySolr jobRepositorySolr;
@Test
    public void save() throws Exception {
        Job job1 = new Job("1", 9527, "9527_java");
        Job job2 = new Job("2", 9527, "9527_nodejs");
        jobRepositorySolr.save(job1);
        jobRepositorySolr.save(job2);
    }
@Test
public void queryBasic() throws Exception{
Iterator<Job> jobsIt = jobRepositorySolr.findAll().iterator();
@SuppressWarnings("unchecked")
List<Job> jobs = IteratorUtils.toList(jobsIt);
Assert.notEmpty(jobs, "Fail to fetch anything from SOLR");
}
@Test
public void queryBySourceID() throws Exception{
List<Job> jobs = jobRepositorySolr.findBySourceID(9527);
Assert.notEmpty(jobs, "Fail to queryBySourceID from SOLR");
List<Job> jobEmptys = jobRepositorySolr.findBySourceID(9529);
Assert.isTrue(jobEmptys.isEmpty(), "Fail to filter out other sourceID");
}

}



References:
http://projects.spring.io/spring-data-solr/
https://github.com/spring-projects/spring-data-solr
http://www.jianshu.com/p/e21fe5f3bd8c
http://www.kailing.pub/article/index/arcid/150.html
http://www.kailing.pub/article/index/arcid/150.html
http://docs.spring.io/spring-data/solr/docs/3.0.0.M3/reference/html/


分享到:
评论

相关推荐

    Spring Boot编写RESTful API.txt

    免费的学习视频,比网上骗人那些强多了,希望对大家有帮助。如果没有账号的注册一下就可以了

    Spring Boot开发实战:基于Spring Boot的RESTful API服务的实验心得与案例解析

    ### Spring Boot开发实战:基于Spring Boot的RESTful API服务的实验心得与案例解析 #### 一、引言 Spring Boot自发布以来,以其强大的自动配置能力、简洁的开发模式以及丰富的社区支持,迅速成为了Java开发者构建...

    Spring boot restful api demo

    **Spring Boot RESTful API Demo** 在现代Web开发中,RESTful API已经成为构建可扩展、松耦合服务的主要方式。Spring Boot作为Java生态系统中的一个强大框架,简化了创建生产级的基于Spring的应用程序。本示例将...

    Spring Boot中使用Swagger2构建强大的RESTful API文档

    为了解决上面这样的问题,本文将介绍RESTful API的重磅好伙伴Swagger2,它可以轻松的整合到Spring Boot中,并与Spring MVC程序配合组织出强大RESTful API文档。它既可以减少我们创建文档的工作量,同时说明内容又...

    SpringBoot+Mybatis+CXF框架,实现Restful api与 WebService api接口的大实验

    3. 接下来,我们需要实现Restful API和WebService API接口,使用Spring Boot的Restful API和CXF框架来实现学生信息的增删改查操作。 4. 最后,我们需要测试Restful API和WebService API接口,确保其正常工作。 结论...

    基于Java与Spring Boot的RESTful API设计与调用源码

    该项目提供基于Java和Spring Boot框架构建的RESTful API设计与调用源码,涵盖17个Java源文件、9个XML配置文件、6个JavaScript文件、4个CSS样式文件、2个HTML文件,共计46个文件。项目旨在实现Spring Boot的API发布与...

    Maven Archetype与Spring Boot搭建RESTful API实例(包含详细的完整的程序和数据)

    本文主要介绍了通过使用 Maven Archetype 与 Spring Boot 能够迅速搭建起一个支持CRUD的基本RESTful API项目框架,并提供了从创建初始项目到完成简易数据操作(如增删查)的具体指南和代码样例。 适合具有一定经验但...

    一个基于 Spring Boot 的RESTful API项目示例:图书管理系统

    内容概要:本文详述了如何利用Spring Boot技术搭建一套简易图书管理系统的RESTful API接口流程,涵盖从初始创建到实现完整CRUD(增删改查)功能的整体步骤,并提供H2内存数据库作为数据存储支持。此外介绍了系统主要...

    Pro Spring Boot 2第2版-2009-EPUB版

    Pro Spring Boot 2: An Authoritative Guide to Building Microservices, Web and Enterprise Applications, and Best Practices Quickly and productively develop complex Spring applications and microservices...

    Spring Boot + Mybatis 整合实现RESTful API

    Spring Boot 整合 Mybatis 实现RESTful API ,具体可以查看博客: http://blog.csdn.net/yaozhiqi1905658804/article/details/70820892

    Spring Boot Swagger2 构建RESTful API

    在Spring Boot项目中集成Swagger2,可以帮助我们快速地构建和维护高质量的RESTful API。以下将详细讲解如何利用Spring Boot与Swagger2进行集成,并展示其主要功能和步骤。 **一、集成Swagger2** 1. 添加依赖:首先...

    Spring Boot-RESTfull API入门.rar

    在 Spring Boot 中构建 RESTful API,主要涉及以下几个关键知识点: 1. **MVC 模式**:Spring Boot 基于 Spring MVC 框架提供了一种简洁的 Web 开发方式。你可以使用 `@RestController` 注解标记控制器类,而 `@...

    基于Spring Boot与Java的图书管理系统RESTful API实战

    内容概要:详细介绍使用Java、Spring Boot以及相关技术和工具如Maven、H2 database搭建简单的图书管理RESTful API系统全过程,覆盖了从项目的建立、实体类定义、接口编写一直到API的功能测试。项目主要提供了增加、...

    spring boot restful 接口示例项目

    在这个“Spring Boot RESTful 接口示例项目”中,我们可以学习到如何使用 Spring Boot 创建 RESTful 风格的 API。RESTful API 通常通过 HTTP 协议提供服务,利用 GET、POST、PUT、DELETE 等方法操作资源,实现客户端...

    swagger整合Spring Boot生成Restful接口文档

    而Swagger是目前最流行的接口文档解决方案,本文主要通过代码实战的方式讲解Spring Boot 和Swagger集成生成Restful接口文档。教程参见 http://blog.csdn.net/zjx2016/article/details/74407832

    2023最新《Spring Boot基础教程》

    Spring Boot 2.x基础教程:构建RESTful API与单元测试 Spring Boot 2.x基础教程:使用Swagger2构建强大的API文档 Spring Boot 2.x基础教程:JSR-303实现请求参数校验 Spring Boot 2.x基础教程:Swagger接口分类与各...

    spring boot3+jpa+lombok+mapstruct实现的restful api例子

    在本项目中,"spring boot3+jpa+lombok+mapstruct实现的restful api例子"是一个集成多种技术的示例,旨在展示如何高效地构建RESTful API服务。下面将详细介绍这些关键技术及其相互间的配合。 1. **Spring Boot 3**:...

    基于Spring Boot,采用RESTful风格架构的微信点餐系统源码(高分毕设).zip

    基于Spring Boot,采用RESTful风格架构的微信点餐系统源码(高分毕设).zip 基于Spring Boot,采用RESTful风格架构的微信点餐系统源码(高分毕设).zip 基于Spring Boot,采用RESTful风格架构的微信点餐系统源码...

    Spring Boot 博客 RESTful API项目含CRUD 功能,用于博客开发.zip

    2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; ...

    基于 Spring Boot 的 Restful 风格实现增删改查.docx

    基于 Spring Boot 的 Restful 风格实现增删改查

Global site tag (gtag.js) - Google Analytics