ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

8.8 SpringBoot集成ElasticSearch之指定返回字段

2022-01-05 14:58:00  阅读:160  来源: 互联网

标签:JsonProperty String 8.8 private name ElasticSearch hobby null SpringBoot


1.接口实现方式
(1).condition开发
在项目目录“/src/main/java/com/example/es/condition”的EmployeeCondition类中实现SourceProvider接口,具体代码如下。

@Data
public class EmployeeCondition extends SampleEmployeeCondition implements RoutingProvider, ScoreFunctionProvider, SourceProvider {
    private static final String[] INCLUDE_FIELDS = {"employeeId", "name","age","job","salary","hobby"};
    private static final String[] EXCLUDE_FIELDS = {"birthday","profile","relative"};
    /**
     * 获取包含的字段列表
     *
     * @return 字段列表
     */
    @Override
    public String[] getIncludeFields() {
        return INCLUDE_FIELDS;
    }

    /**
     * 获取排除的字段列表
     *
     * @return 字段列表
     */
    @Override
    public String[] getExcludeFields() {
        return EXCLUDE_FIELDS;
    }
}

(2).测试
启动项目,然后在postman中请求“http://localhost:8080/employee/queryEmployeePage”,成功后返回对应的信息。

[
    {
        "id": null,
        "employeeId": "10000002",
        "name": "Stephen Curry",
        "age": 27,
        "birthday": null,
        "job": "Java engineer",
        "salary": 20000.0,
        "hobby": [
            "tennis",
            "football"
        ],
        "profile": null,
        "relative": null
    },
    {
        "id": null,
        "employeeId": "10000001",
        "name": "James Harden",
        "age": 31,
        "birthday": null,
        "job": "Java engineer",
        "salary": 30000.0,
        "hobby": [
            "swimming",
            "running",
            "basketball",
            "football"
        ],
        "profile": null,
        "relative": null
    }
]

2.实体类实现方式
(1).entity开发
在项目目录“/src/main/java/com/example/es”的EmployeeEntity类中注释掉不需要展示的字段,具体代码如下。

@Data
public class EmployeeEntity implements IdProvider{
    @JsonProperty("id")
    private String id;

    @JsonProperty("employeeId")
    private String employeeId;

    @JsonProperty("name")
    private String name;

    @JsonProperty("age")
    private Integer age;

//    @JsonProperty("birthday")
//    private String birthday;

    @JsonProperty("job")
    private String job;

    @JsonProperty("salary")
    private Float salary;

    @JsonProperty("hobby")
    private List<String> hobby;

//    @JsonProperty("profile")
//    private Profile profile;

//    @JsonProperty("relative")
//    private List<Relative> relativeList;

    @Override
    public String getId() {
        return id;
    }
}

(2).测试
启动项目,然后在postman中请求“http://localhost:8080/employee/queryEmployeePage”,成功后返回对应的信息。

[
    {
        "id": "10000002",
        "employeeId": "10000002",
        "name": "Stephen Curry",
        "age": 27,
        "job": "Java engineer",
        "salary": 20000.0,
        "hobby": [
            "tennis",
            "football"
        ]
    },
    {
        "id": "10000001",
        "employeeId": "10000001",
        "name": "James Harden",
        "age": 31,
        "job": "Java engineer",
        "salary": 30000.0,
        "hobby": [
            "swimming",
            "running",
            "basketball",
            "football"
        ]
    }
]

标签:JsonProperty,String,8.8,private,name,ElasticSearch,hobby,null,SpringBoot
来源: https://blog.csdn.net/Jgx1214/article/details/122322721

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有