parent
c782855cec
commit
02ca4bd22a
@ -0,0 +1,40 @@
|
||||
package com.xjs.ai.controller;
|
||||
|
||||
import com.ruoyi.common.core.web.domain.AjaxResult;
|
||||
import com.ruoyi.common.security.annotation.RequiresLogin;
|
||||
import com.xjs.ai.factory.AssociationFactory;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 联想controller
|
||||
*
|
||||
* @author xiejs
|
||||
* @since 2022-02-24
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("association")
|
||||
@Api(tags = "业务模块-联想管理")
|
||||
@Log4j2
|
||||
public class AssociationController {
|
||||
|
||||
@Autowired
|
||||
private AssociationFactory<List<String>> baiduAssociationFactory;
|
||||
|
||||
|
||||
@GetMapping("getAssociation")
|
||||
@ApiOperation("获取联想词汇")
|
||||
@RequiresLogin
|
||||
public AjaxResult getAssociation(@RequestParam("content") String content) {
|
||||
List<String> data = baiduAssociationFactory.getData(content);
|
||||
return AjaxResult.success(data);
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.xjs.ai.factory;
|
||||
|
||||
/**
|
||||
* 联想 工厂
|
||||
* @author xiejs
|
||||
* @since 2022-02-24
|
||||
*/
|
||||
public interface AssociationFactory<T> {
|
||||
|
||||
/**
|
||||
* 获取数据
|
||||
* @param content 输入内容
|
||||
* @return T
|
||||
*/
|
||||
T getData(String content);
|
||||
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package com.xjs.ai.factory.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.ruoyi.common.core.utils.StringUtils;
|
||||
import com.xjs.ai.factory.AssociationFactory;
|
||||
import com.xjs.common.client.api.baidu.BaiduAssociationFeignClient;
|
||||
import com.xjs.exception.ApiException;
|
||||
import com.xjs.exception.BusinessException;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 百度联想工厂实现
|
||||
* @author xiejs
|
||||
* @since 2022-02-24
|
||||
*/
|
||||
@Component
|
||||
@Log4j2
|
||||
public class BaiduAssociationFactory implements AssociationFactory<List<String>> {
|
||||
|
||||
public static final String filter= "window.baidu.sug";
|
||||
|
||||
@Autowired
|
||||
private BaiduAssociationFeignClient baiduAssociationFeignClient;
|
||||
|
||||
@Override
|
||||
public List<String> getData(String content) {
|
||||
|
||||
String data = baiduAssociationFeignClient.associationApi(content);
|
||||
if (StringUtils.isEmpty(data)) {
|
||||
throw new ApiException("百度联想api接口调用异常");
|
||||
}
|
||||
|
||||
if (data.contains(filter)) {
|
||||
String substring = data.substring(filter.length() + 1, data.length() - 2);
|
||||
try {
|
||||
JSONObject jsonObject = JSONObject.parseObject(substring);
|
||||
JSONArray jsonArray = jsonObject.getJSONArray("s");
|
||||
return jsonArray.toJavaList(String.class);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("json格式转换异常!!!");
|
||||
throw new BusinessException("json格式转换异常");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.xjs.common.client.api.baidu;
|
||||
|
||||
import com.xjs.annotation.ApiLog;
|
||||
import com.xjs.common.client.factory.BaiduAssociationFeignFactory;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import static com.xjs.consts.ApiConst.BAIDU_ASSOCIATION;
|
||||
import static com.xjs.consts.ApiConst.BAIDU_ASSOCIATION_URL;
|
||||
|
||||
/**
|
||||
*
|
||||
* 百度语义联想api
|
||||
* @author xiejs
|
||||
* @since 2022-02-24
|
||||
*/
|
||||
@FeignClient(name = "baiduAssociation", url = BAIDU_ASSOCIATION_URL, fallbackFactory = BaiduAssociationFeignFactory.class)
|
||||
public interface BaiduAssociationFeignClient {
|
||||
|
||||
@GetMapping
|
||||
@ApiLog(name = BAIDU_ASSOCIATION,
|
||||
url = BAIDU_ASSOCIATION_URL,
|
||||
method = "Get")
|
||||
String associationApi(@RequestParam("wd") String wd);
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.xjs.common.client.factory;
|
||||
|
||||
import com.xjs.common.client.api.baidu.BaiduAssociationFeignClient;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @since 2022-02-24
|
||||
*/
|
||||
@Component
|
||||
@Log4j2
|
||||
public class BaiduAssociationFeignFactory implements FallbackFactory<BaiduAssociationFeignClient> {
|
||||
|
||||
@Override
|
||||
public BaiduAssociationFeignClient create(Throwable cause) {
|
||||
log.error("百度联想服务调用失败:{},执行降级处理", cause.getMessage());
|
||||
return (wd ->"");
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.xjs.ai.factory.impl;
|
||||
|
||||
import com.xjs.XjsOpenApiApp;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @since 2022-02-24
|
||||
*/
|
||||
@SpringBootTest(classes = XjsOpenApiApp.class)
|
||||
class BaiduAssociationFactoryTest {
|
||||
|
||||
@Autowired
|
||||
BaiduAssociationFactory baiduAssociationFactory;
|
||||
|
||||
@Test
|
||||
void getData() {
|
||||
List<String> xx = baiduAssociationFactory.getData("哈哈");
|
||||
|
||||
for (String s : xx) {
|
||||
System.out.println(s);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue