您当前的位置: 首页 >  搜索

java持续实践

暂无认证

  • 2浏览

    0关注

    746博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

黑马十次方项目day04-11之索引库中搜索文章

java持续实践 发布时间:2019-01-20 16:55:54 ,浏览量:2

文章目录
      • dao
      • service
      • Controller
      • 测试
      • 待解决的问题

dao
package com.tensquare.search.dao;

import com.tensquare.search.pojo.Article;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

/**
 * 类名称:ArticleDao
 * 类描述:文章的dao层
 *
 * @author: taohongchao
 * 创建时间:2019/1/20 15:32
 * Version 1.0
 */
public interface ArticleDao extends ElasticsearchRepository {

    /**
     * 方法名: findByTitleOrContentLike
     * 方法描述: 根据文章的标题 内容 进行模糊分页查询
     * 修改日期: 2019/1/20 16:16
      * @param title
     * @param content
     * @param pageable
     * @return org.springframework.data.domain.Page
     * @author taohongchao
     * @throws
     */
    public Page findByTitleOrContentLike(String title, String content, Pageable pageable);

}


service
/**
     * 方法名: searchPageData
     * 方法描述: 关键词分页查询
     * 修改日期: 2019/1/20 16:25
      * @param keyword
     * @param page
     * @param size
     * @return org.springframework.data.domain.Page
     * @author taohongchao
     * @throws
     */
    public Page searchPageData(String keyword, int page, int size) {
        //创建分页对象
        Pageable pageAble = PageRequest.of(page-1,size);

        //调用dao层,进行返回
        return articleDao.findByTitleOrContentLike(keyword,keyword,pageAble);
    }
Controller
/**
     * 方法名: searchByKeyWords
     * 方法描述: 根据搜索的关键词分页查询
     * 修改日期: 2019/1/20 16:22
      * @param keyword 搜索的关键词
     * @param page 当前页
     * @param size   每页显示条数
     * @return entity.Result
     * @author taohongchao
     * @throws
     */
    @RequestMapping(value = "/{page}/{size}/{keyword}",method = RequestMethod.GET)
    public Result searchByKeyWords(@PathVariable String keyword,
                                   @PathVariable int page,
                                   @PathVariable int size) {

        Page pageDate =  articleService.searchPageData(keyword, page, size);
        return new Result(true, StatusCode.OK, "查询成功", new PageResult(pageDate.getTotalElements(), pageDate.getContent()));
    }

测试

在postman中发送如下的get请求 http://localhost:9007/article/1/10/spring 返回的数据如下

{
    "flag": true,
    "code": 20000,
    "message": "查询成功",
    "data": {
        "total": 1,
        "rows": [
            {
                "id": "AWhqS0ytQ4-ZJZrBuY-b",
                "title": "测试新增索引",
                "content": "spring项目教程",
                "state": null
            }
        ]
    }
}
待解决的问题

当搜索的关键词为中文的时候, 例如 http://localhost:9007/article/1/10/项目 请求的状态码为400了 后台报错如下

java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986 at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:476) ~[tomcat-embed-core-8.5.29.jar:8.5.29

暂未找到解决方案

关注
打赏
1658054974
查看更多评论
立即登录/注册

微信扫码登录

0.0376s