前言 因为最近经常使用百度翻译,点击去了好几次它的那个翻译api介绍页面,居然还有免费版本的,那咱不得体验体验嘛!百度在这点上做得就挺好的,其它的同类翻译api,我似乎没有看见对个人免费的(虽然有限制吧,当时总归是满足可用性的。)这里我就是把百度翻译的api集成到SpringBoot中,作为一个翻译服务来提供。
注:这里吐槽一下,官方提供的Java代码示例,似乎无法编译通过!因为有一句代码:
byte[] inputByteArray = input.getBytes("UTF-8");
这里这个方法会抛出一个异常(抛出异常的原因是这里传如的字符集是硬编码的形式,你可能会传入一个错误的字符集,所以它需要处理可能发生的异常!),但是程序并没有捕获,也没有继续抛出!虽然,demo是使用mac系统编写的,但是这里应该没有关系。
解决办法也很简单:
1.选择捕获或者抛出异常。
2.改成下面这样的方式,虽然功能和上面是等价的,但是它不会抛出异常(字符集成常量了!)。
byte[] inputByteArray = input.getBytes(StandardCharsets.UTF_8);
示例
demo结构
这里工程报错了,似乎显示pom文件有问题,但是我也没找到原因,而且运行也没有问题。可能是Eclipse对于SpringBoot支持不够好吧,甚至这个yml文件都没变色(需要安装插件,但是网络太慢了,就凑合着用吧)。
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <!-- 这里这几条配置根据自己的修改 -->
<groupId>top.dragon</groupId>
<artifactId>translate</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.11.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
TranslateApplication
项目的启动类
package top.dragon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TranslateApplication {
public static void main(String[] args) {
SpringApplication.run(TranslateApplication.class, args);
}
}
RestTemplateConfig
配置一下RestTemplate这个bean,用于调用百度翻译的api,官方demo是使用的JDK原生的网络库实现的 ,这里换成SpringBoot提供的吧,感觉使用起来更加简洁(特别坑!)。
package top.dragon.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
// 我不熟悉RestTemplate的配置,就这样简单的搞一下好了。
return new RestTemplate();
}
}
MD5
官方demo中的工具类,这里直接复制使用,不做修改。
package top.dragon.config.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* MD5编码相关的类
*
* @author wangjingtao
*
*/
public class MD5 {
// 首先初始化一个字符数组,用来存放每个16进制字符
private static final char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
'e', 'f' };
/**
* 获得一个字符串的MD5值
*
* @param input 输入的字符串
* @return 输入字符串的MD5值
*
*/
public static String md5(String input) {
if (input == null)
return null;
try {
// 拿到一个MD5转换器(如果想要SHA1参数换成”SHA1”)
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
// 输入的字符串转换成字节数组
byte[] inputByteArray = input.getBytes(StandardCharsets.UTF_8);
// inputByteArray是输入字符串转换得到的字节数组
messageDigest.update(inputByteArray);
// 转换并返回结果,也是字节数组,包含16个元素
byte[] resultByteArray = messageDigest.digest();
// 字符数组转换成字符串返回
return byteArrayToHex(resultByteArray);
} catch (NoSuchAlgorithmException e) {
return null;
}
}
/**
* 获取文件的MD5值
*
* @param file
* @return
*/
public static String md5(File file) {
try {
if (!file.isFile()) {
System.err.println("文件" + file.getAbsolutePath() + "不存在或者不是文件");
return null;
}
FileInputStream in = new FileInputStream(file);
String result = md5(in);
in.close();
return result;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static String md5(InputStream in) {
try {
MessageDigest messagedigest = MessageDigest.getInstance("MD5");
byte[] buffer = new byte[1024];
int read = 0;
while ((read = in.read(buffer)) != -1) {
messagedigest.update(buffer, 0, read);
}
in.close();
String result = byteArrayToHex(messagedigest.digest());
return result;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static String byteArrayToHex(byte[] byteArray) {
// new一个字符数组,这个就是用来组成结果字符串的(解释一下:一个byte是八位二进制,也就是2位十六进制字符(2的8次方等于16的2次方))
char[] resultCharArray = new char[byteArray.length * 2];
// 遍历字节数组,通过位运算(位运算效率高),转换成字符放到字符数组中去
int index = 0;
for (byte b : byteArray) {
resultCharArray[index++] = hexDigits[b >>> 4 & 0xf];
resultCharArray[index++] = hexDigits[b & 0xf];
}
// 字符数组组合成字符串返回
return new String(resultCharArray);
}
}
TransApi
官方demo提供的TransApi类,做了一些修改。这个类的可改动性,其实是很大的,但是我想把它当成一个服务来调用,就没怎么改动了。
package top.dragon.service;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import top.dragon.config.util.MD5;
@Service
public class TransApi {
private static final String TRANS_API_HOST = "http://api.fanyi.baidu.com/api/trans/vip/translate";
@Value("${baidu_translate.appid}")
private String appid;
@Value("${baidu_translate.securityKey}")
private String securityKey;
// 添加了一个无参构造器,不然无法注入这个bean,否则需要额外的配置。
public TransApi() {}
@Autowired
private RestTemplate restTemplate;
public TransApi(String appid, String securityKey) {
this.appid = appid;
this.securityKey = securityKey;
}
public String getTransResult(String query, String from, String to) {
Map<String, String> params = buildParams(query, from, to);
MultiValueMap<String, String> requestParams = new LinkedMultiValueMap<>();
requestParams.setAll(params);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(TRANS_API_HOST);
URI uri = builder.queryParams(requestParams).build().encode().toUri(); // 这里不能进行 encode 了,编码就错误了
System.out.println("uri: " + uri);
return restTemplate.getForObject(uri, String.class);
}
private Map<String, String> buildParams(String query, String from, String to) {
Map<String, String> params = new HashMap<String, String>();
params.put("q", query);
params.put("from", from);
params.put("to", to);
params.put("appid", appid);
// 随机数
String salt = String.valueOf(System.currentTimeMillis());
params.put("salt", salt);
// 签名
String src = appid + query + salt + securityKey; // 加密前的原文
params.put("sign", MD5.md5(src));
return params;
}
}
TranslateController
这里就是刚开始接触,只提供一个get方式的服务用来请求。
package top.dragon.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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 top.dragon.service.TransApi;
@RestController
@RequestMapping("/translate")
public class TranslateController {
@Autowired
private TransApi transApi;
@GetMapping(value = "/query", produces = MediaType.APPLICATION_JSON_VALUE)
public String query(@RequestParam("query") String query,
@RequestParam("from") String from, @RequestParam("to") String to) {
return transApi.getTransResult(query, from, to);
}
}
properties.yml
这里只有一个自己的自定义配置,没有其它内容。
Postman 测试
I love you yesterday and today
It’s our choice that make us who we are
你也想要起舞吗
注:火影名言
有时候,最痛苦的不是失去,而是得到以后并不快乐
总结
我测试了几个英翻汉和汉翻英,感觉效果还是很不错的。再次为百度打Call,真的挺不错的!不过,个人版本的QPS太低了,也只能个人使用了,有时间再丰富一下功能,看看能不能做些好玩的东西出来吧。
PS:这里来提一下这个RestTemplate的坑!
使用它传递map参数,必须使用如下的形式:
String paramsUrl = "?q={q}&from={from}&to={to}&appid={appid}&salt={salt}&sign={sign}";
return restTemplate.getForObject(TRANS_API_HOST + paramsUrl, String.class, params);
第一个参数中需要使用:{appid} 这种形式的占位符+关键字,否则map中的参数不会生效,具体原因可以去debug看一下。我一开始是直接使用,发现api调用返回 Invalid sgin,即无效签名。最后才发现,原来一个参数都没有传递进去!
或者使用这种更加优雅的方式:
这样可以规避掉那些显式硬编码可能导致的错误,比如我上面今天使用时把from拼错成了form,导致了调用失败。
Map<String, String> params = buildParams(query, from, to);
MultiValueMap<String, String> requestParams = new LinkedMultiValueMap<>();
requestParams.setAll(params);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(TRANS_API_HOST);
URI uri = builder.queryParams(requestParams).build().encode().toUri();
System.out.println("uri: " + uri);
return restTemplate.getForObject(uri, String.class);