1、roll平台汉语字典api实现
This commit is contained in:
parent
adbd6075af
commit
fec2fc4391
|
|
@ -64,6 +64,8 @@ public class ApiConst {
|
|||
|
||||
public static final String ROLL_SIMPLE_COMPLEX = "ROLL-简繁转换";
|
||||
|
||||
public static final String ROLL_CHINESE_DICT = "ROLL-汉语字典";
|
||||
|
||||
|
||||
//-------------------url------------------------------
|
||||
|
||||
|
|
@ -143,6 +145,11 @@ public class ApiConst {
|
|||
*/
|
||||
public static final String ROLL_SIMPLE_COMPLEX_URL = "https://www.mxnzp.com/api/convert/zh";
|
||||
|
||||
/**
|
||||
* 接口文档:https://www.mxnzp.com/doc/detail?id=22
|
||||
*/
|
||||
public static final String ROLL_CHINESE_DICT_URL = "https://www.mxnzp.com/api/convert/dictionary";
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -99,10 +99,22 @@ public class ApiToolsController {
|
|||
if (b) {
|
||||
return R.ok(apiToolsService.getSimpleComplex(content));
|
||||
|
||||
}else {
|
||||
} else {
|
||||
return R.fail("请输入中文!!!");
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("chinesedict/{content}")
|
||||
@ApiOperation("获取汉语字典信息")
|
||||
@Log(title = "获取汉语字典")
|
||||
public R<ApiChineseDict> getChineseDictApiData(@PathVariable("content") String content) {
|
||||
boolean b1 = ChineseUtils.checkNameChese(content);
|
||||
if (b1 && content.length() == 1) {
|
||||
return R.ok(apiToolsService.getChineseDict(content));
|
||||
} else {
|
||||
return R.fail("请输入单个中文!!!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,47 @@
|
|||
package com.xjs.apitools.domain;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* api汉语字典实体
|
||||
* @author xiejs
|
||||
* @since 2022-01-20
|
||||
*/
|
||||
@Data
|
||||
public class ApiChineseDict implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 原内容
|
||||
*/
|
||||
private String word;
|
||||
|
||||
/**
|
||||
* 繁体
|
||||
*/
|
||||
private String traditional;
|
||||
|
||||
/**
|
||||
* 拼音
|
||||
*/
|
||||
private String pinyin;
|
||||
|
||||
/**
|
||||
* 偏旁部首
|
||||
*/
|
||||
private String radicals;
|
||||
|
||||
/**
|
||||
* 汉字释义
|
||||
*/
|
||||
private String explanation;
|
||||
|
||||
/**
|
||||
* 汉字笔画数
|
||||
*/
|
||||
private String strokes;
|
||||
|
||||
}
|
||||
|
|
@ -44,7 +44,8 @@ public class RequestBody {
|
|||
|
||||
|
||||
/**
|
||||
* 待转换的内容
|
||||
* 待转换的内容 <br>
|
||||
* 被查询的汉字内容
|
||||
*/
|
||||
private String content;
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,45 @@
|
|||
package com.xjs.apitools.factory.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.xjs.apitools.domain.ApiChineseDict;
|
||||
import com.xjs.apitools.domain.RequestBody;
|
||||
import com.xjs.apitools.factory.ApiToolsFactory;
|
||||
import com.xjs.common.client.api.roll.RollChineseDictFeignClient;
|
||||
import com.xjs.config.RollProperties;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
|
||||
import static com.xjs.consts.ApiConst.ROLL_CODE_SUCCESS;
|
||||
|
||||
/**
|
||||
* roll平台获取汉语字典api工厂实现
|
||||
* @author xiejs
|
||||
* @since 2022-01-20
|
||||
*/
|
||||
@Component
|
||||
@Log4j2
|
||||
public class RollChineseDictFactory implements ApiToolsFactory<ApiChineseDict, RequestBody> {
|
||||
|
||||
@Autowired
|
||||
private RollProperties rollProperties;
|
||||
@Autowired
|
||||
private RollChineseDictFeignClient rollChineseDictFeignClient;
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public ApiChineseDict apiData(RequestBody req) {
|
||||
req.setApp_id(rollProperties.getApp_id());
|
||||
req.setApp_secret(rollProperties.getApp_secret());
|
||||
JSONObject jsonObject = rollChineseDictFeignClient.chineseDictApi(req);
|
||||
if (!jsonObject.containsKey(DEMOTE_ERROR) && jsonObject.getInteger("code") == ROLL_CODE_SUCCESS.intValue()) {
|
||||
JSONArray jsonArrayData = jsonObject.getJSONArray("data");
|
||||
JSONObject jsonData = jsonArrayData.getJSONObject(0);
|
||||
return jsonData.toJavaObject(ApiChineseDict.class);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -71,5 +71,13 @@ public interface ApiToolsService {
|
|||
ApiSimpleComplex getSimpleComplex(String content);
|
||||
|
||||
|
||||
/**
|
||||
* 获取汉语字典数据
|
||||
* @param content 被查询的汉字内容
|
||||
* @return ApiChineseDict
|
||||
*/
|
||||
ApiChineseDict getChineseDict(String content);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,6 +44,10 @@ public class ApiToolsServiceImpl implements ApiToolsService {
|
|||
@Qualifier("rollSimpleComplexFactory")
|
||||
private ApiToolsFactory<ApiSimpleComplex,RequestBody> simpleComplexFactory;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("rollChineseDictFactory")
|
||||
private ApiToolsFactory<ApiChineseDict,RequestBody> chineseDictFactory;
|
||||
|
||||
@Autowired
|
||||
public void setHolidayFactory(RollHolidayFactory rollHolidayFactory) {
|
||||
this.holidayFactory = rollHolidayFactory;
|
||||
|
|
@ -158,4 +162,11 @@ public class ApiToolsServiceImpl implements ApiToolsService {
|
|||
requestBody.setContent(content);
|
||||
return simpleComplexFactory.apiData(requestBody);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApiChineseDict getChineseDict(String content) {
|
||||
RequestBody requestBody = new RequestBody();
|
||||
requestBody.setContent(content);
|
||||
return chineseDictFactory.apiData(requestBody);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
package com.xjs.common.client.api.roll;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.xjs.annotation.ApiLog;
|
||||
import com.xjs.apitools.domain.RequestBody;
|
||||
import com.xjs.common.client.factory.RollChineseDictFeignFactory;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.cloud.openfeign.SpringQueryMap;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
import static com.xjs.consts.ApiConst.ROLL_CHINESE_DICT;
|
||||
import static com.xjs.consts.ApiConst.ROLL_CHINESE_DICT_URL;
|
||||
|
||||
/**
|
||||
* roll 汉语字典api接口feign远程调用
|
||||
* @author xiejs
|
||||
* @since 2022-01-20
|
||||
*/
|
||||
@FeignClient(name = "rollChineseDict", url = ROLL_CHINESE_DICT_URL, fallbackFactory = RollChineseDictFeignFactory.class)
|
||||
public interface RollChineseDictFeignClient {
|
||||
|
||||
@GetMapping()
|
||||
@ApiLog(name = ROLL_CHINESE_DICT,
|
||||
url = ROLL_CHINESE_DICT_URL,
|
||||
method = "Get")
|
||||
JSONObject chineseDictApi(@SpringQueryMap RequestBody requestBody);
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
package com.xjs.common.client.factory;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.xjs.common.client.api.roll.RollChineseDictFeignClient;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
|
||||
|
||||
/**
|
||||
* roll 汉语字典api降级处理
|
||||
* @author xiejs
|
||||
* @since 2022-01-20
|
||||
*/
|
||||
@Component
|
||||
@Log4j2
|
||||
public class RollChineseDictFeignFactory implements FallbackFactory<RollChineseDictFeignClient> {
|
||||
|
||||
@Override
|
||||
public RollChineseDictFeignClient create(Throwable cause) {
|
||||
log.error("api模块roll 汉语字典服务调用失败:{},执行降级处理", cause.getMessage());
|
||||
return requestBody -> {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put(DEMOTE_ERROR, R.FAIL);
|
||||
return jsonObject;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -10,6 +10,7 @@ import org.springframework.stereotype.Component;
|
|||
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
|
||||
|
||||
/**
|
||||
* roll 简繁转换api降级处理
|
||||
* @author xiejs
|
||||
* @since 2022-01-20
|
||||
*/
|
||||
|
|
|
|||
Loading…
Reference in New Issue