解决wiremock中velocity脚本(.vm)中文编码乱码问题
WireMock 是一个轻量级的服务器,可以快速的实现接口服务和部署。在前端开发中,如果服务接口未实现,可以使用这个工具来模拟接口。关于wiremock的使用网上又不少文章了,可以自行搜索,有时间我会出一篇详细的使用教程。
在wiremock中可以使用velocity脚本来编写数据文件(.vm),这样可以生产动态的数据。
但是wiremock中如果在velocity脚本中存在中文,就会出现编码错乱。而直接使用静态的json数据文本(.json)就不会出问题。
这是因为.vm文件在读取后需要进行转换生产最终的数据,所以wiremock需要wiremock-velocity-transformer-x.x.jar和wiremock-velocity-transformer-standalone-x.x.jar这两个jar包。
就是在velocity转换的过程中出现了编码问题。
实际上,velocity转换后的数据返回的编码不是utf-8,所以我们用utf-8来处理就会有问题,我们在拿到这种数据可用单独做些处理就能得到正常的数据,比如:
new String(text.getBytes(Charsets.ISO_8859_1), Charsets.UTF_8)
注意:在我的电脑上velocity转换后的数据的编码是ISO_8859_1,这个编码是否固定和是否依赖终端类型还不确定,所以可能在其他机器上又是另外一个编码,如gbk。
但是由于我们服务api基本上都使用的utf-8,所以如果wiremock不能提供utf-8的数据,那么我们在代码中就要根据环境来做一些特殊处理。所以最好的办法就是让velocity转换后的数据使用utf-8编码。
转换的代码在wiremock-velocity-transformer-x.x.jar和wiremock-velocity-transformer-standalone-x.x.jar这两个jar包中,其中wiremock-velocity-transformer-x.x.jar只有一个类VelocityResponseTransformer,而wiremock-velocity-transformer-standalone-x.x.jar很大包含了很多不同的包。
为了修改我们要拿到源码,在GitHub上可以找到wiremock-velocity-transformer的源码
https://github.com/adamyork/wiremock-velocity-transformer
下载源码后打开项目,这时要注意当前一定是最新版本的代码,而自己使用的wiremock未必是最新版本的,所以要将代码切到正确的tag下,比如使用的是wiremock-velocity-transformer-1.2.jar和wiremock-velocity-transformer-standalone-1.2.jar,那么checkout到1.2-release的tag上。否则会因为代码的不同导致运行出错。
打开项目后可能会有一些依赖的问题,因为这个build.gradle将所有层次依赖都列出的,而不是利用gradle来自动管理依赖,这样当依赖版本不同时会出现依赖问题。解决方法就是去掉不必要的依赖,只保留velocity-tools和wiremock这两个依赖即可(也要保留junit用于测试),最终如下:
dependencies {
// compile group: "org.apache.velocity", name: "velocity", version: "1.7"compile group: "org.apache.velocity", name: "velocity-tools", version: "2.0"compile group: "com.github.tomakehurst", name: "wiremock", version: "1.57"
// compile group: "org.mortbay.jetty", name: "jetty", version: "6.1.26"
// compile group: "com.google.guava", name: "guava", version: "18.0"
// compile group: "com.fasterxml.jackson.core", name: "jackson-core", version: "2.4.2"
// compile group: "com.fasterxml.jackson.core", name: "jackson-annotations", version: "2.4.2"
// compile group: "com.fasterxml.jackson.core", name: "jackson-databind", version: "2.4.2"
// compile group: "org.apache.httpcomponents", name: "httpclient", version: "4.3.5"
// compile group: "org.skyscreamer", name: "jsonassert", version: "1.2.3"
// compile group: "xmlunit", name: "xmlunit", version: "1.5"
// compile group: "com.jayway.jsonpath", name: "json-path", version: "0.8.1"
// compile group: "org.slf4j", name: "slf4j-api", version: "1.7.6"
// compile group: "net.sf.jopt-simple", name: "jopt-simple", version: "4.7"compile ("junit:junit:4.11") {exclude group: "org.hamcrest", module: "hamcrest-core"}testCompile "org.hamcrest:hamcrest-all:1.3"testCompile ("org.jmock:jmock:2.5.1") {exclude group: "junit", module: "junit-dep"exclude group: "org.hamcrest", module: "hamcrest-core"exclude group: "org.hamcrest", module: "hamcrest-library"}testCompile ("org.jmock:jmock-junit4:2.5.1") {exclude group: "junit", module: "junit-dep"exclude group: "org.hamcrest", module: "hamcrest-core"exclude group: "org.hamcrest", module: "hamcrest-library"}testCompile "net.sf.json-lib:json-lib:2.4:jdk15"testCompile "com.googlecode.jarjar:jarjar:1.3"testCompile "commons-io:commons-io:2.4"
}
在这个项目中也只有VelocityResponseTransformer类,转换就是在这里进行的,关键方法如下:
private void transformResponse(final ResponseDefinition response) throws Exception {final String templatePath = fileSource.getPath().concat("/" + response.getBodyFileName());final Template template = Velocity.getTemplate(templatePath);StringWriter writer = new StringWriter();template.merge(context, writer);final byte[] fileBytes = String.valueOf(writer.getBuffer()).getBytes();response.setBody(fileBytes);response.setBodyFileName(null);
}
问题就出现在Velocity.getTemplate(templatePath)这句
这里没有指定编码,则会使用默认编码,实际上Velocity提供了附带编码的方法,所以修改为
Velocity.getTemplate(templatePath, "utf-8")
即可,在项目的resource下的文件中添加中文,运行测试用例VelocityResponseTransformerTest就会发现可以获得正常的中文了。
最后就是要打包jar,通过测试发现wiremock-velocity-transformer-x.x.jar和wiremock-velocity-transformer-standalone-x.x.jar这两个jar包中都有VelocityResponseTransformer类,所以这两个jar包都需要替换。但是项目中只有一个类,打出wiremock-velocity-transformer-x.x.jar这个jar包还比较容易,但是wiremock-velocity-transformer-standalone-x.x.jar很难手动打出。
其实在build.gradle中已经有了打包的task,本来的目的是为了打包上传到仓库。代码如下:
fatJar {archiveName = "wiremock-velocity-transformer-standalone-" + fatJar.version + ".jar"manifest {attributes "Implementation-Title" : "wiremock-velocity-transformer-standalone","Implementation-Version" : version}
}task cleanFunctional(type: Delete) {delete fileTree(dir: "functional", include: "*-velocity-transformer-*.jar", exclude: "wiremock-1.55-standalone.jar")
}task copyFunctional(type: Copy) {from "build/libs/"include "*.jar"exclude "*-sources.jar","*-javadoc.jar"into "functional/"
}task javadocJar(type: Jar, dependsOn: javadoc) {classifier = "javadoc"from "build/docs/javadoc"
}task sourcesJar(type: Jar) {from sourceSets.main.allSourceclassifier = "sources"
}jar {dependsOn fatJardependsOn cleanFunctionaldependsOn copyFunctionalcleanFunctional.shouldRunAfter fatJarcopyFunctional.dependsOn cleanFunctionalcopyFunctional.shouldRunAfter cleanFunctionalarchiveName = "wiremock-velocity-transformer-" + jar.version + ".jar"manifest {attributes "Implementation-Title" : "wiremock-velocity-transformer","Implementation-Version" : version}
}artifacts {archives jararchives javadocJararchives sourcesJar
}
我们只需要打包,而不需要上传,所以要在uploadArchives中做手脚,代码如下
uploadArchives {repositories {mavenDeployer {beforeDeployment {MavenDeployment deployment -> signing.signPom(deployment)}repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {authentication(userName: "", password: "")}pom.project {name "wiremock-velocity-transformer"packaging "jar"description "transformer used to render velocity templates for stubbed responses."url "https://github.com/radAdam/wiremock-velocity-transformer"scm {url "scm:git@github.com:radAdam/wiremock-velocity-transformer.git"connection "scm:git@github.com:radAdam/wiremock-velocity-transformer.git"developerConnection "scm:git@github.com:radAdam/wiremock-velocity-transformer.git"}licenses {license {name "The Apache Software License, Version 2.0"url "http://www.apache.org/licenses/LICENSE-2.0.txt"distribution "repo"}}developers {developer {id "adamcyork"name "Adam York"}}}}}
}
在uploadArchives这个task下,我们将仓库的账号和密码随便修改(本来也没有账号和密码,但是之前的两个变量如果不删掉则gradle无法成功),这样在上传时就会失败停下,但是前面的步骤都会完成。
运行uploadArchives这个task,完成后在项目中build/libs下就可以看到打好的jar包,将wiremock-velocity-transformer-x.x.jar和wiremock-velocity-transformer-standalone-x.x.jar替换掉wiremock中原有的。
在.vm文件中添加中文,启动wiremock,访问对应接口就会发现中文数据不再乱码了。
解决wiremock中velocity脚本(.vm)中文编码乱码问题相关推荐
- 15、解决14中csv用excel打开乱码的问题 open('zhihu.csv','w',newline='',encoding='utf-8-sig')...
解决14中csv用excel打开乱码的问题 ,其实就是在写csv的时候把 utf-8 改成 utf-8-sig open('zhihu.csv','w',newline='',encoding='ut ...
- Activiti保存.png 流程图片文件且解决idea中保存图片时显示中文乱码的解决方法
Activiti保存.png 流程图片文件且解决idea中保存图片时显示中文乱码的解决方法 Eclipse 工具中的操作 流程图片生成的两种方式: 使用 activiti-designer 设计流程图 ...
- 解决matlab打开批量脚本文件[中文乱码]问题
问题: 在电脑A中matlab创建的脚本程序,拷贝后去电脑B上matlab软件打开,发现脚本文件中的中文注释是乱码的. 原因: 打开同一文件的matlab软件版本不一,matlab编码方式不同,在A上 ...
- 解决eclipse中java代码注释变成乱码的问题
Eclipse JAVA文件注释乱码将别人的项目或JAVA文件导入到自己的Eclipse中时,常常会出现JAVA文件的中文注释变成乱码的情况,主要原因就是别人的IDE编码格式和自己的Eclipse编码 ...
- 解决ubuntu中zabbix 4.2图形显示乱码
Zabbix安装好之后,监控图形页面出现字符集乱码 解决办法 右击 电脑,点击属性,点击控制面板 ,点击外观和个性化 ,点击字体, 字体文件目录: zabbix 4.2:/usr/share/zabb ...
- 解决Eclipse中SVN版本比较中文乱码问题
如果是UTF-8编码的文件,历史版本比较时中文会显示为乱码,解决方法见下图:
- 如何解决Bat脚本中包含中文,运行乱码
如何解决Bat脚本中包含中文,运行乱码 转载于:https://jingyan.baidu.com/article/a3f121e4d84a5dfc9152bb55.html bat脚本在批处理的时候 ...
- php 中文截断,PHP中实现中文字串截取无乱码的解决方法
在PHP中,substr()函数截取带有中文字符串的话,可能会出现乱码,这是因为中西文一个字节所占有的字节数不一样,而substr的长度参数是按照字节去算的,在GB2312编码时,一个中文占2个字节, ...
- mysql脚本执行中文乱码_MySQL从命令行导入SQL脚本时出现中文乱码的解决方法
本文实例讲述了MySQL从命令行导入SQL脚本时出现中文乱码的解决方法.分享给大家供大家参考,具体如下: 在图形界面管理工具 MySql Query Browser中打开脚本(脚本包括建库.建表.添加 ...
最新文章
- python add argument list_python模块介绍- argparse:命令行选项及参数解析
- PyQt5 笔记3 -- 信号与槽
- 【视频】网易杭州研究院副院长汪源2016QCon大会专访
- 麻省理工计算机科学录取条件,2018美国留学:麻省理工学院托福分数最低录取要求...
- JVM调优总结(八)-典型配置举例2
- LoRa、蓝牙、技术在电子显示牌上的应用
- 文科出身敲出 Instagram,被小札“挤”走,建新冠追踪网站
- netcore redis 存储集合_Redis的简单入门
- php怎么做群聊,workerman实现群聊
- 上传文件_Spring Boot文件上传
- word2013+endnotex8参考文献导入
- 盈利稳定增长:盈利收益率法
- vue3+vant Failed to resolve import “E:/code3/jianmu-user-yd/node_modules/vant/lib/vant/es/icon/style
- 数据库04—约束条件
- java bean 首字母大写_javaBean命名规范 get / set 后的首字母大写
- 在IDEA里gradle配置和使用
- GFP-GAN学习笔记
- 「信号机制」Python信号处理—signal模块
- pycharm 在 Clash模式下无法联网的解决办法
- 《计算机组成原理》课程学习(7)——第3篇 中央处理器——第7章 指令系统
热门文章
- 关于重复接收NSNotificationCenter发送的通知的问题
- javascript谜题
- 由于启动用户实例的进程时出错,导致无法生成 SQL Server 的用户实例。该连接将关闭...
- 利用python中的xlrd和xlwt操作excel
- 微信公众号的搭建-第五天-自定义菜单
- Codeigniter 获取当前的控制器名称和方法名称
- Js计算间隔天数和Date对象
- MVC中helper的用法。
- HDU_4014 Discont (water~)
- 安装Rational Enterprise Suite(Robot...)时遇到的问题及解决办法!