parent
cef358aa3c
commit
a86f84e4a1
@ -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,11 @@
|
||||
package com.xjs.english.domain.vo.translation;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @desc 谷歌翻译实体
|
||||
* @create 2021-12-25
|
||||
*/
|
||||
public class GoogleTranslationVo extends TranslationVo {
|
||||
|
||||
|
||||
}
|
@ -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