项目环境:spring-boot 2.0.9
开发工具:IntelliJ IDEA 2017.2.5 x64
1、引入依赖(有些项目不需要的)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jtpf</groupId>
<artifactId>es-svc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>es-svc</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR3</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--调远程服务-->
<!--spring cloud-->
<!--注册中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--调远程服务-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--服务熔断-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--健康检查-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- spring2.0集成redis需要加上 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<!--mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>RELEASE</version>
</dependency>
<!--mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>RELEASE</version>
</dependency>
<!--mysql数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<!--HikariCP-->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--springUtil等 工具包-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- aop 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 用于日志切面中,以 json 格式打印出入参 -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<!--swagger本身不支持spring mvc的,springfox把swagger包装了一下,让他可以支持springmvc-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.1</version>
</dependency>
<!--httpClient-->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.0.1</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-cache</artifactId>
<version>4.5</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.2</version>
</dependency>
<!--添加freeMarker-->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、启动文件加注解EnableSwagger2
package com.jtpf.es;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import tk.mybatis.spring.annotation.MapperScan;
@EnableSwagger2
@SpringBootApplication
public class EsSvcApplication {
public static void main(String[] args) {
SpringApplication.run(EsSvcApplication.class, args);
}
}
3、配置文件
package com.jtpf.es.config;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
/**
* @Version 1.0
* @Author:liji
* @Date:2020/4/3
* @Content:
*/
@Configuration
public class SwaggerConfig {
@Bean
public Docket swaggerSpringMvcPlugin() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.withMethodAnnotation
(ApiOperation.class)).build();
}
}
4、java文件使用
package com.jtpf.es.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jtpf.es.resp.GenericDataResponse;
import com.jtpf.es.util.HttpClientUtil;
import com.jtpf.es.util.UuidUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
/**
* @Version 1.0
* @Author:liji
* @Date:2020/3/16
* @Content:
*/
@Slf4j
@RestController
@RequestMapping("/es")
@Api(description = "es增删改查")
public class ElasticSearchController {
/**
* es服务器地址
*/
@Value("${url.elasticSearch.host}")
private String host;
@PostMapping("/{index}/_search")
@ApiOperation(value = "查询", notes = "按条件查询列表", httpMethod = "POST")
public ResponseEntity<GenericDataResponse<Object>> _search(
@PathVariable(value = "index") String index,
@RequestParam(value = "content", required = false) String content) throws IOException, URISyntaxException {
String url = host + "/" + index + "/_search";
String result = HttpClientUtil.httpURLConnectionPOST(content, url,"");
return GenericDataResponse.okWithData(JSONObject.parse(result));
}
@PostMapping("/{index}/{type}")
@ApiOperation(value = "指定索引添加文档", notes = "新增文档", httpMethod = "POST")
@ApiImplicitParams({
@ApiImplicitParam(name = "index", value = "索引", required = true, paramType = "query"),
@ApiImplicitParam(name = "type", value = "_doc", required = true, paramType = "query"),
@ApiImplicitParam(name = "content", value = "入参json", required = true, paramType = "query")
})
public Object addDoc(@PathVariable(value = "index") String index,
@PathVariable(value = "type") String type,
@RequestBody Object content) throws UnsupportedEncodingException {
String url = String.format("%s/%s/%s/%s", host, index, type, UuidUtil.getUuidStr());
return JSONObject.parse(HttpClientUtil.httpURLConnectionPOST(JSON.toJSONString(content), url,""));
}
@GetMapping("/{index}/{type}/{id}")
@ApiOperation(value = "指定索引获取文档", notes = "根据索引,type,id修改文档", httpMethod = "GET")
@ApiImplicitParams({
@ApiImplicitParam(name = "index", value = "索引", required = true, paramType = "query"),
@ApiImplicitParam(name = "type", value = "_doc", required = true, paramType = "query"),
@ApiImplicitParam(name = "id", value = "文档id", required = true, paramType = "query")
})
public Object getDoc(@PathVariable(value = "index") String index,
@PathVariable(value = "type") String type,
@PathVariable(value = "id", required = false) String id) {
String url = String.format("%s/%s/%s/%s", host, index, type, id);
return JSONObject.parse(HttpClientUtil.httpURLConectionGET(url));
}
@PutMapping("/{index}/{type}/{id}")
@ApiOperation(value = "指定索引修改文档", notes = "根据索引,type,id修改文档", httpMethod = "PUT")
@ApiImplicitParams({
@ApiImplicitParam(name = "index", value = "索引", required = true, paramType = "query"),
@ApiImplicitParam(name = "type", value = "_doc", required = true, paramType = "query"),
@ApiImplicitParam(name = "id", value = "文档id", required = true, paramType = "query"),
@ApiImplicitParam(name = "content", value = "入参json", required = true, paramType = "query")
})
public Object updateDoc(@PathVariable(value = "index") String index,
@PathVariable(value = "type") String type,
@PathVariable(value = "id", required = false) String id,
@RequestBody Object content) {
String url = String.format("%s/%s/%s/%s", host, index, type, id);
return JSONObject.parse(HttpClientUtil.httpURLConnectionPut(JSON.toJSONString(content), url));
}
@DeleteMapping("/{index}/{type}/{id}")
@ApiOperation(value = "按id 删除文档", notes = "根据索引,type,id删除文档", httpMethod = "DELETE")
@ApiImplicitParams({
@ApiImplicitParam(name = "index", value = "索引", required = true, paramType = "query"),
@ApiImplicitParam(name = "type", value = "_doc", required = true, paramType = "query"),
@ApiImplicitParam(name = "id", value = "文档id", required = true, paramType = "query")
})
public Object delDoc(@PathVariable(value = "index") String index,
@PathVariable(value = "type") String type,
@PathVariable(value = "id") String id) {
String url = String.format("%s/%s/%s/%s", host, index, type, id);
return JSONObject.parse(HttpClientUtil.DELETE(url, null));
}
}
6、
微信图片_20200416171954.png
网友评论