最近开发基本都是在搬砖,补漏洞,今天正式部署到服务准备联调了,中间遇到了一些难点。
字符编码
由于时间紧迫没有来得及研究,只是根据日志和经验找到了解决方案。这里用到了非常基础的Java
知识,幸好我没有忘干净。
首先,在build.gradle
中要指定编码格式,如下:
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
其次呢,在过滤器里面我是读去request
和response
的ServletOutputStream
中的内容用来写入日志的,所以还需要会写到原来的对象中,这里就有一个坑,也需要指定编码格式。
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
ResponseWrapper responseWrapper = new ResponseWrapper(resp);
RequestWrapper requestWrapper = new RequestWrapper(req);
String url = requestWrapper.getRequestURI();
String queryArgs = requestWrapper.getQueryString();
queryArgs = queryArgs == null ? requestWrapper.getBody() : queryArgs;
long start = Time.getTimeStamp();
chain.doFilter(requestWrapper == null ? request : requestWrapper, responseWrapper);
long end = Time.getTimeStamp();
byte[] bytes = responseWrapper.getContent();
String respContent = new String(bytes, Constant.UTF_8);
logger.info("请求:{},耗时:{}ms,参数:{},响应:{}", url, end - start,queryArgs,respContent);
response.getOutputStream().write(respContent.getBytes(Constant.UTF_8));
}
数据库部分字段组合成json
还有一个难点,就是用例集运行结果详情的统计结果读取,数据库里面有四个字段,表示四种用例运行状态的用例数量。但是前端要求以json
格式返回。本来想读取完结果之后再去处理,但是这个方案比较麻烦,首先我得创建两个对象,区分从数据库读取的信息和返回给前端的信息,还得将第一个对象的运行结果转成json
格式set
到第二个对象中。想了很多只想到一个可行的方案:将结果用concat
拼接起来,然后使用resultMap
将这个拼接的结果转成json
格式,返回给前端。
typehandler
package com.okay.family.common.typehandler;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.okay.family.fun.frame.SourceCode;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
/**
* 从数据库里面读取的map运行结果转化成json格式
*/
@MappedTypes(JSONObject.class)
@MappedJdbcTypes(JdbcType.VARCHAR)
public class ResultArrayHandler extends BaseTypeHandler<JSONArray> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, JSONArray parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, String.valueOf(parameter.toJSONString()));
}
@Override
public JSONArray getNullableResult(ResultSet rs, String columnName) throws SQLException {
String sqlJson = rs.getString(columnName);
if (null != sqlJson) {
JSONArray result = new JSONArray();
List<String> asList = Arrays.asList(sqlJson.split(";"));
asList.stream().forEach(x -> {
String[] split = x.split("=", 2);
result.add(SourceCode.getJson("text=" + split[0], "amount=" + split[1]));
});
}
return null;
}
@Override
public JSONArray getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
String sqlJson = rs.getString(columnIndex);
if (null != sqlJson) {
JSONArray result = new JSONArray();
List<String> asList = Arrays.asList(sqlJson.split(";"));
asList.stream().forEach(x -> {
String[] split = x.split("=", 2);
result.add(SourceCode.getJson("text=" + split[0], "amount=" + split[1]));
});
}
return null;
}
@Override
public JSONArray getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
String sqlJson = cs.getString(columnIndex);
if (null != sqlJson) {
JSONArray result = new JSONArray();
List<String> asList = Arrays.asList(sqlJson.split(";"));
asList.stream().forEach(x -> {
String[] split = x.split("=", 2);
result.add(SourceCode.getJson("text=" + split[0], "amount=" + split[1]));
});
}
return null;
}
}
mapper配置
- resultMap
<resultMap type="com.okay.family.common.bean.casecollect.response.CollectionRunResultDetailBean"
id="CollectionRunResultDetailBean">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="envName" column="envName"/>
<result property="runId" column="runId"/>
<result property="caseNum" column="caseNum"/>
<result property="result" column="result"/>
<result property="startTime" column="start"/>
<result property="endTime" column="end"/>
<result property="list" column="list"
typeHandler="com.okay.family.common.typehandler.ResultArrayHandler"/>
</resultMap>
- SQL语句
<select id="getCollectionRunDetail" parameterType="java.lang.Integer"
resultMap="CollectionRunResultDetailBean">
select r.collectionId id,t.name,s.name envName,r.runId,r.caseNum,c.name result,start,end,concat("成功=",r.success,";失败=",r.fail,";无法运行=",r.unrun,";用户错误=",r.userError) list from
<include refid="run_record"/>
r left join
<include refid="collection_status"/>
c on r.result = c.id left join
<include refid="table"/>
t on r.collectionId = t.id left join
<include refid="env"/>
s on r.envId = s.id
where runId = #{0}
</select>
- 公众号FunTester首发,更多原创文章:FunTester410+原创文章,欢迎关注、交流,禁止第三方擅自转载。