采用restful的好处就是可以跨越一切语言的障碍,遵循规范即可,还是以JAVA进行举例
pom.xml中包含的支持类
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>org.apache.log4j</groupId>
<artifactId>com.springsource.org.apache.log4j</artifactId>
<version>1.2.15</version>
</dependency>
最重要的代码非常简单。(倒是测试花了不少时间)
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.ObjectMapper;
import jdk.nashorn.api.scripting.JSObject;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.converter.BufferedImageHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import sun.java2d.cmm.Profile;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URI;
import java.util.*;
import java.util.List;
public class Driver {
public static void main(String[] args) {
RestTemplate rest2 = new RestTemplate();//这就是Spring提供的模板类,大大方面我们的书写
//下面两个分别是JSON转换类,和图片转换类,也是Spring提供
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
BufferedImageHttpMessageConverter bufferedImageHttpMessageConverter = new BufferedImageHttpMessageConverter();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
messageConverters.add(mappingJackson2HttpMessageConverter);
messageConverters.add(bufferedImageHttpMessageConverter);
rest2.setMessageConverters(messageConverters);
MyStruct myStruct = rest2.getForObject("http://localhost:9200/twitter/tweet/2", MyStruct.class); //这个可以直接把返回的JSON内容放到结构体了
Map<String, Object> vars = new HashMap<String, Object>();
vars = rest2.getForObject("http://localhost:9200/twitter/tweet/2", HashMap.class); //不想弄结构体也可以放map里
// 获取图片也是非常简单,下面一行搞定,后面都是显示
BufferedImage bufferedImage= rest2.getForObject("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg", BufferedImage.class);
JFrame frame = new JFrame( " photos");
frame.setLayout(new GridLayout(2, 1));
frame.add(new JLabel(new ImageIcon(bufferedImage)));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
MyStruct 内容
@SuppressWarnings("unused")
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyStruct {
public String _index;
public String _type;
public String _id;
public Integer _version;
public Boolean found;
public MySource _source;
}
public class MySource {
public String user;
public Date postDate;
public String message;
}
下面介绍一些细节
使用post 进行查询的代码如下
Map<String, Object> varsRequest = new HashMap<String, Object>();
Map<String, Object> queryContent = new HashMap<String, Object>();
Map<String, Object> matchContent = new HashMap<String, Object>();
queryContent.put("match_all", matchContent);
varsRequest.put("query", queryContent);
String strTemp4 = varsRequest.toString();
Map<String, Object> varsResponse = rest2.postForObject("http://localhost:9200/twitter/tweet/_search?pretty", varsRequest, HashMap.class);
完整的文档地址如下
https://www.elastic.co/guide/en/elasticsearch/reference/current/search.html