• 【Json/ObjectMapper】将限制用户名密码的Elasticsearch9200端口返回的默认内容转化为类


    【192.168.32.128的用户密码】

    elastic/123456

    【192.168.32.128上ES默认返回的内容】

    {
      "name" : "node-1",
      "cluster_name" : "liangshan",
      "cluster_uuid" : "h6AscLaBS0qyOKdfq1DO8w",
      "version" : {
        "number" : "7.11.1",
        "build_flavor" : "default",
        "build_type" : "tar",
        "build_hash" : "ff17057114c2199c9c1bbecc727003a907c0db7a",
        "build_date" : "2021-02-15T13:44:09.394032Z",
        "build_snapshot" : false,
        "lucene_version" : "8.7.0",
        "minimum_wire_compatibility_version" : "6.8.0",
        "minimum_index_compatibility_version" : "6.0.0-beta1"
      },
      "tagline" : "You Know, for Search"
    }

    【目标】

    把上述内容转化为对象

    【实体类Elastic】

    public class Elastic {
        private String name;
        private String cluster_name;
        private String cluster_uuid;
        private String tagline;
    
        private Version version;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getCluster_name() {
            return cluster_name;
        }
    
        public void setCluster_name(String cluster_name) {
            this.cluster_name = cluster_name;
        }
    
        public String getCluster_uuid() {
            return cluster_uuid;
        }
    
        public void setCluster_uuid(String cluster_uuid) {
            this.cluster_uuid = cluster_uuid;
        }
    
        public String getTagline() {
            return tagline;
        }
    
        public void setTagline(String tagline) {
            this.tagline = tagline;
        }
    
        public Version getVersion() {
            return version;
        }
    
        public void setVersion(Version version) {
            this.version = version;
        }
    
        public String toString(){
            return name+"_"+cluster_name+"_"+cluster_uuid+"_"+tagline+"_"+version.getBuild_date();
        }
    }

    【实体类Version】

    public class Version {
        private String number;
        private String build_flavor;
        private String build_type;
        private String build_hash;
        private String build_date;
        private boolean build_snapshot;
        private String lucene_version;
        private String minimum_wire_compatibility_version;
        private String minimum_index_compatibility_version;
    
        public String getNumber() {
            return number;
        }
    
        public void setNumber(String number) {
            this.number = number;
        }
    
        public String getBuild_flavor() {
            return build_flavor;
        }
    
        public void setBuild_flavor(String build_flavor) {
            this.build_flavor = build_flavor;
        }
    
        public String getBuild_type() {
            return build_type;
        }
    
        public void setBuild_type(String build_type) {
            this.build_type = build_type;
        }
    
        public String getBuild_hash() {
            return build_hash;
        }
    
        public void setBuild_hash(String build_hash) {
            this.build_hash = build_hash;
        }
    
        public String getBuild_date() {
            return build_date;
        }
    
        public void setBuild_date(String build_date) {
            this.build_date = build_date;
        }
    
        public boolean isBuild_snapshot() {
            return build_snapshot;
        }
    
        public void setBuild_snapshot(boolean build_snapshot) {
            this.build_snapshot = build_snapshot;
        }
    
        public String getLucene_version() {
            return lucene_version;
        }
    
        public void setLucene_version(String lucene_version) {
            this.lucene_version = lucene_version;
        }
    
        public String getMinimum_wire_compatibility_version() {
            return minimum_wire_compatibility_version;
        }
    
        public void setMinimum_wire_compatibility_version(String minimum_wire_compatibility_version) {
            this.minimum_wire_compatibility_version = minimum_wire_compatibility_version;
        }
    
        public String getMinimum_index_compatibility_version() {
            return minimum_index_compatibility_version;
        }
    
        public void setMinimum_index_compatibility_version(String minimum_index_compatibility_version) {
            this.minimum_index_compatibility_version = minimum_index_compatibility_version;
        }
    }

    【访问及转化代码】

    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.apache.catalina.authenticator.BasicAuthenticator;
    import org.apache.http.auth.AuthScope;
    import org.apache.http.auth.UsernamePasswordCredentials;
    import org.apache.http.client.HttpClient;
    import org.apache.http.impl.client.BasicCredentialsProvider;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.springframework.http.client.ClientHttpRequestFactory;
    import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
    import org.springframework.web.client.RestTemplate;
    
    public class AccessCredentialEs {
        public static void main(String[] args) throws Exception{
            BasicCredentialsProvider provider=new BasicCredentialsProvider();
            provider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials("elastic","123456"));
            CloseableHttpClient httpClient= HttpClients.custom().setDefaultCredentialsProvider(provider).useSystemProperties().build();
    
            ClientHttpRequestFactory factory=new HttpComponentsClientHttpRequestFactory(httpClient);
            RestTemplate tplt=new RestTemplate();
            tplt.setRequestFactory(factory);
    
            String rsps=tplt.getForObject("http://192.168.32.128:9200",String.class);
            System.out.println(rsps);
    
            ObjectMapper mapper = new ObjectMapper();
            Elastic elastic = mapper.readValue(rsps, Elastic.class);
            System.out.println(elastic);
        }
    }

    【输出】

    从System.out.println(elastic);开始的内容

    node-1_liangshan_h6AscLaBS0qyOKdfq1DO8w_You Know, for Search_2021-02-15T13:44:09.394032Z

    可以看到,如果存在类与Json文的对应关系,要转化还是很方便的。

    END

  • 相关阅读:
    Java多线程(3) Volatile的实现原理
    Java 多线程(2)-Executor
    Java 多线程(1)-Thread和Runnable
    nginx+php部署
    MySQL的慢查询分析
    MySQL 错误
    log4j.properties配置详解
    Windows下Nginx的安装与配置(转)
    Java 字符的验证
    Spring MVC3返回JSON数据中文乱码问题解决(转)
  • 原文地址:https://www.cnblogs.com/heyang78/p/15960824.html
Copyright © 2020-2023  润新知