parent
cef358aa3c
commit
a86f84e4a1
|
|
@ -17,7 +17,7 @@ public @interface EnableRyFeignClients
|
|||
{
|
||||
String[] value() default {};
|
||||
|
||||
String[] basePackages() default { "com.ruoyi" };
|
||||
String[] basePackages() default { "com.ruoyi" ,"com.xjs"};
|
||||
|
||||
Class<?>[] basePackageClasses() default {};
|
||||
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@
|
|||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<mybatisplus.version>3.4.3.4</mybatisplus.version>
|
||||
<hutool.version>5.7.17</hutool.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
|
@ -92,6 +93,25 @@
|
|||
<version>${mybatisplus.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--lombok-->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--测试-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--工具类-->
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>${hutool.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
package com.xjs.english.client;
|
||||
|
||||
import com.xjs.english.domain.qo.translation.BaiDuTranslationQo;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @desc
|
||||
* @create 2021-12-25
|
||||
*/
|
||||
@FeignClient(name = "baidu",url = "http://api.fanyi.baidu.com/api/trans/vip/translate?")
|
||||
public interface BaiduFeignClient {
|
||||
|
||||
@PostMapping(headers = {"Content-Type=application/x-www-form-urlencoded"})
|
||||
String translationApi(BaiDuTranslationQo qo);
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
package com.xjs.english.client;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.xjs.english.domain.qo.translation.YouDaoTranslationQo;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.cloud.openfeign.SpringQueryMap;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @desc
|
||||
* @create 2021-12-25
|
||||
*/
|
||||
@FeignClient(name = "youdao",url = "http://fanyi.youdao.com/translate?")
|
||||
public interface YouDaoFeignClient {
|
||||
|
||||
@GetMapping( headers ={ "Accept-Encoding=''"})
|
||||
JSONObject translationApi(@SpringQueryMap YouDaoTranslationQo qo);
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
package com.xjs.english.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @desc
|
||||
* @create 2021-12-25
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = "baidu.open")
|
||||
@Component
|
||||
public class BaiduProperties {
|
||||
|
||||
/**
|
||||
* APP ID
|
||||
*/
|
||||
private String appId;
|
||||
|
||||
/**
|
||||
* 密钥
|
||||
*/
|
||||
private String key;
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
package com.xjs.english.config;
|
||||
|
||||
import feign.Logger;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @desc
|
||||
* @create 2021-12-25
|
||||
*/
|
||||
@Configuration
|
||||
public class FeignConfig{
|
||||
/**
|
||||
* 配置日志输出
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
Logger.Level feignLoggerLevel() {
|
||||
return Logger.Level.FULL;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package com.xjs.english.consts;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @desc
|
||||
* @create 2021-12-25
|
||||
*/
|
||||
public interface TranslationTypeConst {
|
||||
Integer BAIDU = 1;
|
||||
Integer YOUDAO = 2;
|
||||
Integer GOOGLE = 3;
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
package com.xjs.english.controller;
|
||||
|
||||
import com.ruoyi.common.core.web.domain.AjaxResult;
|
||||
import com.xjs.english.domain.qo.translation.TranslationQo;
|
||||
import com.xjs.english.domain.vo.translation.TranslationVo;
|
||||
import com.xjs.english.service.TranslationService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import static com.xjs.english.consts.TranslationTypeConst.BAIDU;
|
||||
import static com.xjs.english.consts.TranslationTypeConst.YOUDAO;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @desc
|
||||
* @create 2021-12-25
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("translation")
|
||||
public class TranslationController {
|
||||
|
||||
@Autowired
|
||||
private TranslationService youDaoTranslationServiceImpl;
|
||||
@Autowired
|
||||
private TranslationService baiDuTranslationServiceImpl;
|
||||
|
||||
@PostMapping
|
||||
public AjaxResult translation(@Validated @RequestBody TranslationQo translationQo) {
|
||||
TranslationVo translationVo=new TranslationVo();
|
||||
if (BAIDU.equals(translationQo.getTranslationType())) {
|
||||
translationVo = baiDuTranslationServiceImpl.translationApi(translationQo);
|
||||
}
|
||||
if (YOUDAO.equals(translationQo.getTranslationType())) {
|
||||
translationVo = youDaoTranslationServiceImpl.translationApi(translationQo);
|
||||
}
|
||||
return AjaxResult.success(translationVo);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
package com.xjs.english.domain.qo.translation;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @desc 百度翻译条件
|
||||
* @create 2021-12-25
|
||||
*/
|
||||
@Data
|
||||
public class BaiDuTranslationQo {
|
||||
/**
|
||||
* appid
|
||||
*/
|
||||
private String appid;
|
||||
|
||||
/**
|
||||
* 密钥
|
||||
*/
|
||||
private String key;
|
||||
|
||||
/**
|
||||
* 需要翻译的词
|
||||
*/
|
||||
private String q;
|
||||
|
||||
/**
|
||||
* 翻译目标语言
|
||||
*/
|
||||
private String to ;
|
||||
|
||||
|
||||
/**
|
||||
* 翻译源语言
|
||||
*/
|
||||
private String from = "auto";
|
||||
|
||||
|
||||
/**
|
||||
* 盐(随机数)
|
||||
*/
|
||||
private String salt = "xjsisyourfatter";
|
||||
|
||||
/**
|
||||
* 签名(appid+query+salt+key的MD5值)
|
||||
*/
|
||||
private String sign;
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
package com.xjs.english.domain.qo.translation;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @desc 翻译条件
|
||||
* @create 2021-12-25
|
||||
*/
|
||||
@Data
|
||||
public class TranslationQo implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 需要翻译的词(前端可以指定翻译词)
|
||||
*/
|
||||
@NotBlank(message = "翻译内容不能为空")
|
||||
private String q="你傻吗,大傻逼?嗯,哈哈哈";
|
||||
|
||||
/**
|
||||
* 翻译目标语言(前端可以指定目标语言)
|
||||
*/
|
||||
private String to = "auto";
|
||||
|
||||
/**
|
||||
* 翻译api类型(例如:有道、谷歌、百度等)
|
||||
* 1、百度 2、有道 3、谷歌 4 ...
|
||||
*/
|
||||
@NotNull(message = "翻译api类型不能为空")
|
||||
private Integer translationType;
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
package com.xjs.english.domain.qo.translation;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @desc 有道翻译条件实体
|
||||
* @create 2021-12-25
|
||||
*/
|
||||
@Data
|
||||
public class YouDaoTranslationQo{
|
||||
|
||||
/**
|
||||
* 响应类型
|
||||
*/
|
||||
private String doctype="json";
|
||||
|
||||
/**
|
||||
* 目标语言(有道的目标语言无效,中转英、英转中)
|
||||
*/
|
||||
private String type;
|
||||
|
||||
/**
|
||||
* 翻译的内容
|
||||
*/
|
||||
private String i ;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
package com.xjs.english.domain.vo.translation;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @desc 谷歌翻译实体
|
||||
* @create 2021-12-25
|
||||
*/
|
||||
public class GoogleTranslationVo extends TranslationVo {
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
package com.xjs.english.domain.vo.translation;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @desc 翻译实体类VO
|
||||
* @create 2021-12-25
|
||||
*/
|
||||
@Data
|
||||
public class TranslationVo implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 源语言
|
||||
*/
|
||||
private String from;
|
||||
|
||||
/**
|
||||
* 目标语言
|
||||
*/
|
||||
private String to;
|
||||
|
||||
/**
|
||||
* 翻译结果
|
||||
*/
|
||||
private List<Map<String, String>> transResult;
|
||||
|
||||
/**
|
||||
* 是否错误
|
||||
*/
|
||||
private Long errorCode;
|
||||
|
||||
|
||||
/**
|
||||
* 运行时间,单位毫秒
|
||||
*/
|
||||
private Long elapsedTime;
|
||||
|
||||
|
||||
/**
|
||||
* 语言类型
|
||||
*/
|
||||
private String type;
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package com.xjs.english.exception;
|
||||
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @desc
|
||||
* @create 2021-12-25
|
||||
*/
|
||||
@Log4j2
|
||||
public class BusinessException extends RuntimeException{
|
||||
public BusinessException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public BusinessException(String message) {
|
||||
super(message);
|
||||
log.error("业务异常----{}",message);
|
||||
}
|
||||
|
||||
public BusinessException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
package com.xjs.english.service;
|
||||
|
||||
import com.xjs.english.domain.qo.translation.TranslationQo;
|
||||
import com.xjs.english.domain.vo.translation.TranslationVo;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @desc 翻译统一调用接口
|
||||
* @create 2021-12-25
|
||||
*/
|
||||
public interface TranslationService {
|
||||
|
||||
/**
|
||||
* 调用百度翻译接口
|
||||
* @param translationQo 翻译条件封装
|
||||
* @return 翻译结果封装
|
||||
*/
|
||||
TranslationVo translationApi(TranslationQo translationQo);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
package com.xjs.english.service.impl;
|
||||
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.xjs.english.client.BaiduFeignClient;
|
||||
import com.xjs.english.config.BaiduProperties;
|
||||
import com.xjs.english.domain.qo.translation.BaiDuTranslationQo;
|
||||
import com.xjs.english.domain.qo.translation.TranslationQo;
|
||||
import com.xjs.english.domain.vo.translation.TranslationVo;
|
||||
import com.xjs.english.exception.BusinessException;
|
||||
import com.xjs.english.service.TranslationService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @desc
|
||||
* @create 2021-12-25
|
||||
*/
|
||||
@Service
|
||||
public class BaiDuTranslationServiceImpl implements TranslationService {
|
||||
|
||||
@Autowired
|
||||
private BaiduProperties baiduProperties;
|
||||
@Autowired
|
||||
private BaiduFeignClient baiduFeignClient;
|
||||
|
||||
|
||||
@Override
|
||||
public TranslationVo translationApi(TranslationQo translationQo) {
|
||||
String appId = baiduProperties.getAppId();
|
||||
BaiDuTranslationQo baiDuTranslationQo = new BaiDuTranslationQo();
|
||||
baiDuTranslationQo.setAppid(appId);
|
||||
String key = baiduProperties.getKey();
|
||||
//生成签名(appid+q+salt+密钥的MD5值)
|
||||
String append = appId + translationQo.getQ() + baiDuTranslationQo.getSalt() + key;
|
||||
String sign = SecureUtil.md5(append);
|
||||
baiDuTranslationQo.setSign(sign);
|
||||
baiDuTranslationQo.setQ(translationQo.getQ());
|
||||
baiDuTranslationQo.setTo(translationQo.getTo());
|
||||
String translationStr = baiduFeignClient.translationApi(baiDuTranslationQo);
|
||||
JSONObject jsonObject = JSONObject.parseObject(translationStr);
|
||||
if(Objects.nonNull(jsonObject.getString("error_code"))){
|
||||
throw new BusinessException("百度翻译接口调用异常");
|
||||
}
|
||||
TranslationVo translationVo = new TranslationVo();
|
||||
String from = jsonObject.getString("from");
|
||||
String to = jsonObject.getString("to");
|
||||
String transResultStr = jsonObject.getString("trans_result");
|
||||
JSONArray jsonArray = JSONObject.parseArray(transResultStr);
|
||||
HashMap<String, String> map = new HashMap<>();
|
||||
List<Map<String, String>> maps = new ArrayList<>();
|
||||
map.put("src", jsonArray.getJSONObject(0).getString("src"));
|
||||
map.put("dst", jsonArray.getJSONObject(0).getString("dst"));
|
||||
maps.add(map);
|
||||
translationVo.setFrom(from);
|
||||
translationVo.setTo(to);
|
||||
translationVo.setTransResult(maps);
|
||||
return translationVo;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
package com.xjs.english.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.xjs.english.client.BaiduFeignClient;
|
||||
import com.xjs.english.client.YouDaoFeignClient;
|
||||
import com.xjs.english.domain.qo.translation.TranslationQo;
|
||||
import com.xjs.english.domain.qo.translation.YouDaoTranslationQo;
|
||||
import com.xjs.english.domain.vo.translation.TranslationVo;
|
||||
import com.xjs.english.exception.BusinessException;
|
||||
import com.xjs.english.service.TranslationService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @desc
|
||||
* @create 2021-12-25
|
||||
*/
|
||||
@Service
|
||||
public class YouDaoTranslationServiceImpl implements TranslationService {
|
||||
|
||||
@Autowired
|
||||
private YouDaoFeignClient youDaoFeignClient;
|
||||
|
||||
@Override
|
||||
public TranslationVo translationApi(TranslationQo translationQo) {
|
||||
YouDaoTranslationQo youDaoTranslationQo = new YouDaoTranslationQo();
|
||||
youDaoTranslationQo.setI(translationQo.getQ());
|
||||
youDaoTranslationQo.setType(translationQo.getTo());
|
||||
|
||||
JSONObject translationApi = youDaoFeignClient.translationApi(youDaoTranslationQo);
|
||||
if(!"0".equals(translationApi.getString("errorCode"))){
|
||||
throw new BusinessException("有道翻译接口调用异常");
|
||||
}
|
||||
String type = translationApi.getString("type");
|
||||
TranslationVo translationVo = new TranslationVo();
|
||||
translationVo.setType(type);
|
||||
Long errorCode =Long.parseLong(translationApi.getString("errorCode"));
|
||||
translationVo.setErrorCode(errorCode);
|
||||
long elapsedTime = Long.parseLong(translationApi.getString("elapsedTime"));
|
||||
translationVo.setElapsedTime(elapsedTime);
|
||||
JSONArray translateResult = translationApi.getJSONArray("translateResult");
|
||||
JSONArray jsonArray = translateResult.getJSONArray(0);
|
||||
System.out.println(jsonArray);
|
||||
ArrayList<Map<String, String>> maps = new ArrayList<>();
|
||||
if (jsonArray.size() > 0) {
|
||||
for (int i = 0; i < jsonArray.size(); i++) {
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
map.put("src", jsonArray.getJSONObject(i).getString("src"));
|
||||
map.put("tgt", jsonArray.getJSONObject(i).getString("tgt"));
|
||||
maps.add(map);
|
||||
}
|
||||
}
|
||||
translationVo.setTransResult(maps);
|
||||
return translationVo;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
package com.xjs.english.service.impl;
|
||||
|
||||
import com.xjs.english.XjsEnglishApp;
|
||||
import com.xjs.english.domain.qo.translation.BaiDuTranslationQo;
|
||||
import com.xjs.english.domain.qo.translation.TranslationQo;
|
||||
import com.xjs.english.domain.vo.translation.TranslationVo;
|
||||
import com.xjs.english.service.TranslationService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @desc
|
||||
* @create 2021-12-25
|
||||
*/
|
||||
@SpringBootTest(classes = XjsEnglishApp.class)
|
||||
class BaiDuTranslationServiceImplTest {
|
||||
@Resource(name = "baiDuTranslationServiceImpl")
|
||||
TranslationService translationService;
|
||||
|
||||
@org.junit.jupiter.api.Test
|
||||
void handlerTranslationApi() {
|
||||
TranslationVo translationVo = translationService.translationApi(new TranslationQo());
|
||||
System.out.println(translationVo);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
package com.xjs.english.service.impl;
|
||||
|
||||
import com.xjs.english.XjsEnglishApp;
|
||||
import com.xjs.english.domain.qo.translation.TranslationQo;
|
||||
import com.xjs.english.domain.vo.translation.TranslationVo;
|
||||
import com.xjs.english.service.TranslationService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @desc
|
||||
* @create 2021-12-25
|
||||
*/
|
||||
@SpringBootTest(classes = XjsEnglishApp.class)
|
||||
class YouDaoTranslationServiceImplTest {
|
||||
|
||||
@Resource(name = "youDaoTranslationServiceImpl")
|
||||
TranslationService translationService;
|
||||
|
||||
@Test
|
||||
void translationApi() {
|
||||
TranslationVo translationVo = translationService.translationApi(new TranslationQo());
|
||||
System.out.println(translationVo);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue