4 changed files with 263 additions and 3 deletions
@ -0,0 +1,59 @@ |
|||
package com.epmet.utils; |
|||
|
|||
import lombok.Builder; |
|||
import lombok.Data; |
|||
|
|||
import java.io.Serializable; |
|||
import java.util.List; |
|||
|
|||
@Data |
|||
@Builder |
|||
public class ElasticsearchArticleDTO implements Serializable { |
|||
|
|||
private static final long serialVersionUID = 1L; |
|||
/** |
|||
* 文章摘要 |
|||
*/ |
|||
private String abstracts; |
|||
/** |
|||
* 文章作者 |
|||
*/ |
|||
private String author; |
|||
/** |
|||
* 资源类型 :APPLICATION(应用),SERVICE(服务), ARTICLE(文章资讯), PRODUCT(产品) |
|||
*/ |
|||
private String category; |
|||
/** |
|||
* 文章内容 |
|||
*/ |
|||
private String content; |
|||
/** |
|||
* ID |
|||
*/ |
|||
private String id; |
|||
/** |
|||
* 资源来源: SELF(系统本身), HUI_SHENG_HUO(惠生活),WU_YE(物业), ZHAO_PIN(招聘 ), SHE_QU(社区) |
|||
*/ |
|||
private String source; |
|||
/** |
|||
* 缩略图 |
|||
*/ |
|||
private String thumbnail; |
|||
/** |
|||
* 文章标题 |
|||
*/ |
|||
private String title; |
|||
/** |
|||
* 更新日期 |
|||
*/ |
|||
private String updateDate; |
|||
/** |
|||
* 资源定位符 |
|||
*/ |
|||
private String uri; |
|||
/** |
|||
* 发布范围IDs |
|||
*/ |
|||
private List<String> gridIdList; |
|||
|
|||
} |
@ -0,0 +1,129 @@ |
|||
package com.epmet.utils; |
|||
|
|||
import com.fasterxml.jackson.databind.ObjectMapper; |
|||
import lombok.SneakyThrows; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import okhttp3.*; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
@Slf4j |
|||
@Component |
|||
public class ElasticsearchUtils { |
|||
private static final String ElasticsearchTestURL = "http://172.16.9.20:9000"; |
|||
private static final String ElasticsearchProdURL = "http://172.16.9.20:9000"; |
|||
private static final String ElasticsearchURL = ElasticsearchTestURL; |
|||
|
|||
private static final String ElasticsearchArticleDelete = "/SysElasticsearch/article/delete/"; |
|||
private static final String ElasticsearchArticleAdd = "/SysElasticsearch/article/add"; |
|||
private static final String ElasticsearchArticleUpdate = "/SysElasticsearch/article/update"; |
|||
|
|||
|
|||
@SneakyThrows |
|||
public Boolean addArticle(ElasticsearchArticleDTO bean) { |
|||
ObjectMapper objectMapper = new ObjectMapper(); |
|||
String json = objectMapper.writeValueAsString(bean); |
|||
|
|||
OkHttpClient client = new OkHttpClient().newBuilder() |
|||
.build(); |
|||
MediaType mediaType = MediaType.parse("application/json"); |
|||
RequestBody body = RequestBody.create(mediaType, json); |
|||
Request request = new Request.Builder() |
|||
.url(ElasticsearchURL+ElasticsearchArticleAdd) |
|||
.method("POST", body) |
|||
.addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)") |
|||
.addHeader("Content-Type", "application/json") |
|||
.build(); |
|||
Response response = client.newCall(request).execute(); |
|||
ResponseBody responseBody = response.body(); |
|||
// 获取响应的状态码
|
|||
int statusCode = response.code(); |
|||
|
|||
if (response.isSuccessful()) { |
|||
// 请求成功,状态码为2xx
|
|||
if (responseBody != null) { |
|||
// 处理响应内容
|
|||
String responseString = responseBody.string(); |
|||
log.info("接口请求成功,响应内容:" + responseString); |
|||
responseBody.close(); |
|||
return true; |
|||
} |
|||
} else { |
|||
// 请求失败,状态码不是2xx
|
|||
log.info("接口请求失败,状态码:" + statusCode); |
|||
return false; |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
@SneakyThrows |
|||
public Boolean updateArticle(ElasticsearchArticleDTO bean) { |
|||
ObjectMapper objectMapper = new ObjectMapper(); |
|||
String json = objectMapper.writeValueAsString(bean); |
|||
|
|||
OkHttpClient client = new OkHttpClient().newBuilder() |
|||
.build(); |
|||
MediaType mediaType = MediaType.parse("application/json"); |
|||
RequestBody body = RequestBody.create(mediaType, json); |
|||
Request request = new Request.Builder() |
|||
.url(ElasticsearchURL+ElasticsearchArticleUpdate) |
|||
.method("POST", body) |
|||
.addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)") |
|||
.addHeader("Content-Type", "application/json") |
|||
.build(); |
|||
Response response = client.newCall(request).execute(); |
|||
ResponseBody responseBody = response.body(); |
|||
// 获取响应的状态码
|
|||
int statusCode = response.code(); |
|||
|
|||
if (response.isSuccessful()) { |
|||
// 请求成功,状态码为2xx
|
|||
if (responseBody != null) { |
|||
// 处理响应内容
|
|||
String responseString = responseBody.string(); |
|||
log.info("接口请求成功,响应内容:" + responseString); |
|||
responseBody.close(); |
|||
return true; |
|||
} |
|||
} else { |
|||
// 请求失败,状态码不是2xx
|
|||
log.info("接口请求失败,状态码:" + statusCode); |
|||
return false; |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
@SneakyThrows |
|||
public Boolean deleteArticle(int id) { |
|||
|
|||
|
|||
OkHttpClient client = new OkHttpClient().newBuilder() |
|||
.build(); |
|||
MediaType mediaType = MediaType.parse("text/plain"); |
|||
RequestBody body = RequestBody.create(mediaType, ""); |
|||
Request request = new Request.Builder() |
|||
.url(ElasticsearchURL+ElasticsearchArticleDelete+id) |
|||
.method("DELETE", body) |
|||
.addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)") |
|||
.build(); |
|||
Response response = client.newCall(request).execute(); |
|||
ResponseBody responseBody = response.body(); |
|||
// 获取响应的状态码
|
|||
int statusCode = response.code(); |
|||
|
|||
if (response.isSuccessful()) { |
|||
// 请求成功,状态码为2xx
|
|||
if (responseBody != null) { |
|||
// 处理响应内容
|
|||
String responseString = responseBody.string(); |
|||
log.info("接口请求成功,响应内容:" + responseString); |
|||
responseBody.close(); |
|||
return true; |
|||
} |
|||
} else { |
|||
// 请求失败,状态码不是2xx
|
|||
log.info("接口请求失败,状态码:" + statusCode); |
|||
return false; |
|||
} |
|||
return false; |
|||
} |
|||
} |
Loading…
Reference in new issue