Skip to content

【企业微信】获取审批模板详情异常:Expected BEGIN_OBJECT but was BEGIN_ARRAY #3662

@binarywang

Description

@binarywang

问题来源:https://gitee.com/binary/weixin-java-tools/issues/ICPACT
原文:

简要描述

调用接口获取企业微信审批模板详情时,Gson转对象报错抛异常。

模块版本情况

WxJava 模块名: wx-java-cp-multi-spring-boot-starter
WxJava 版本号: 4.7.6.B
Java 版本号: jdk17

详细描述

@Resource
private WxCpMultiServices wxCpMultiServices;

public WxCpOaApprovalTemplateResult getTemplateDetail(@NotBlank String templateId) throws WxErrorException {
    log.info("调用企业微信接口 查询审批模板详情: templateId={}", templateId);
    return wxCpMultiServices.getWxCpService(wxCpProperties.getApprovalAgentId()).getOaService().getTemplateDetail(templateId);
}

代码Debug跟踪排查源码时,从企业微信接口返回的数据正常,但在fromJson转对象的时候抛了异常:

JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("template_id", templateId);
String url = this.mainService.getWxCpConfigStorage().getApiUrl("/cgi-bin/oa/gettemplatedetail");
String responseContent = this.mainService.post(url, jsonObject.toString());
return (WxCpOaApprovalTemplateResult)WxCpGsonBuilder.create().fromJson(responseContent, WxCpOaApprovalTemplateResult.class);

我把企业微信返回的模板详情responseContent数据,使用fastjson转换的时候, 可以正常转成WxCpOaApprovalTemplateResult

public static void main(String[] args) {
    String json = "{\"errcode\":0,\"errmsg\":\"ok\",\"template_names\":[{\"text\":\"智能印章\",\"lang\":\"zh_CN\"},{\"text\":\"Company Seal\",\"lang\":\"en\"}],\"template_content\":{\"controls\":[{\"property\":{\"control\":\"Text\",\"id\":\"Text-1747127819114\",\"title\":[{\"text\":\"用印事由\",\"lang\":\"zh_CN\"}],\"placeholder\":[{\"text\":\"\",\"lang\":\"zh_CN\"}],\"require\":1,\"un_print\":0,\"inner_id\":\"\",\"un_replace\":0,\"display\":1}},{\"property\":{\"control\":\"Selector\",\"id\":\"Selector-1747123508806\",\"title\":[{\"text\":\"用印类型\",\"lang\":\"zh_CN\"}],\"placeholder\":[{\"text\":\"\",\"lang\":\"zh_CN\"}],\"require\":1,\"un_print\":0,\"inner_id\":\"\",\"un_replace\":0,\"display\":1},\"config\":{\"selector\":{\"type\":\"single\",\"options\":[{\"key\":\"option-1747123508806\",\"value\":[{\"text\":\"一般事务性用印\",\"lang\":\"zh_CN\"}]},{\"key\":\"option-1747123508807\",\"value\":[{\"text\":\"对外事务性用印\",\"lang\":\"zh_CN\"}]},{\"key\":\"option-1747123530814\",\"value\":[{\"text\":\"重大事务性用印\",\"lang\":\"zh_CN\"}]}],\"op_relations\":[],\"external_option\":{\"use_external_option\":false,\"external_url\":\"\"}}}},{\"property\":{\"control\":\"Tips\",\"id\":\"Tips-1747123397470\",\"title\":[{\"text\":\"说明\",\"lang\":\"zh_CN\"}],\"placeholder\":[],\"require\":0,\"un_print\":0,\"inner_id\":\"\",\"un_replace\":0,\"display\":1},\"config\":{\"tips\":{\"tips_content\":[{\"text\":{\"sub_text\":[{\"type\":1,\"content\":{\"plain_text\":{\"content\":\"用印类型说明:1. 一般事务性用印:内部日常材料流转、常规业务报表报送、非对外承诺性质的证明文件,用印文件内容不得涉及经济、法律责任条款 \"}}}]},\"lang\":\"zh_CN\"}]}}},{\"property\":{\"control\":\"Table\",\"id\":\"Table-1746005041962\",\"title\":[{\"text\":\"印章明细\",\"lang\":\"zh_CN\"}],\"placeholder\":[{\"text\":\"\",\"lang\":\"zh_CN\"}],\"require\":0,\"un_print\":0,\"inner_id\":\"\",\"un_replace\":0,\"display\":1},\"config\":{\"table\":{\"children\":[{\"property\":{\"control\":\"Text\",\"id\":\"Text-1747127691499\",\"title\":[{\"text\":\"印章名称\",\"lang\":\"zh_CN\"}],\"placeholder\":[{\"text\":\"请输入“公章”\",\"lang\":\"zh_CN\"}],\"require\":1,\"un_print\":0,\"un_replace\":0,\"display\":1}},{\"property\":{\"control\":\"Number\",\"id\":\"Number-1746006598992\",\"title\":[{\"text\":\"普通用印\",\"lang\":\"zh_CN\"}],\"placeholder\":[{\"text\":\"请填写正文用印次数\",\"lang\":\"zh_CN\"}],\"require\":1,\"un_print\":0,\"un_replace\":0,\"display\":1}},{\"property\":{\"control\":\"Number\",\"id\":\"Number-1746006601002\",\"title\":[{\"text\":\"骑缝用印\",\"lang\":\"zh_CN\"}],\"placeholder\":[{\"text\":\"请填写骑缝用印次数\",\"lang\":\"zh_CN\"}],\"require\":1,\"un_print\":0,\"un_replace\":0,\"display\":1}},{\"property\":{\"control\":\"Selector\",\"id\":\"Selector-1746005136537\",\"title\":[{\"text\":\"是否外借\",\"lang\":\"zh_CN\"}],\"placeholder\":[{\"text\":\"\",\"lang\":\"zh_CN\"}],\"require\":0,\"un_print\":0,\"un_replace\":0,\"display\":1},\"config\":{\"selector\":{\"type\":\"single\",\"exp_type\":0,\"options\":[{\"key\":\"option-1746005136537\",\"value\":[{\"text\":\"是\",\"lang\":\"zh_CN\"}]},{\"key\":\"option-1746005136538\",\"value\":[{\"text\":\"否\",\"lang\":\"zh_CN\"}]}],\"op_relations\":[],\"external_option\":{\"use_external_option\":false,\"external_url\":\"\"}}}},{\"property\":{\"control\":\"Date\",\"id\":\"Date-1746005165574\",\"title\":[{\"text\":\"外借开始时间\",\"lang\":\"zh_CN\"}],\"placeholder\":[{\"text\":\"\",\"lang\":\"zh_CN\"}],\"require\":0,\"un_print\":0,\"un_replace\":0,\"display\":1},\"config\":{\"date\":{\"type\":\"day\"}}},{\"property\":{\"control\":\"Date\",\"id\":\"Date-1746005173386\",\"title\":[{\"text\":\"外借结束时间\",\"lang\":\"zh_CN\"}],\"placeholder\":[{\"text\":\"\",\"lang\":\"zh_CN\"}],\"require\":0,\"un_print\":0,\"un_replace\":0,\"display\":1},\"config\":{\"date\":{\"type\":\"day\"}}}],\"stat_field\":[],\"sum_field\":[],\"print_format\":0}}},{\"property\":{\"control\":\"File\",\"id\":\"item-1494250388062\",\"title\":[{\"text\":\"用印文件\",\"lang\":\"zh_CN\"},{\"text\":\"Attachment\",\"lang\":\"en\"}],\"placeholder\":[{\"text\":\"\",\"lang\":\"zh_CN\"}],\"require\":1,\"un_print\":0,\"inner_id\":\"\",\"un_replace\":0,\"display\":1}}]}}";
        
    WxCpOaApprovalTemplateResult templateDetail = JSON.parseObject(json, WxCpOaApprovalTemplateResult.class);
    // 使用Gson转换,抛异常报错
    templateDetail = WxCpGsonBuilder.create().fromJson(json, WxCpOaApprovalTemplateResult.class);
    System.out.println(templateDetail);
}

大佬可以使用返回的json数据进行测试复现一下该问题,是否跟Gson使用方式有关。

日志

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2464 path $.template_content.controls[3].config.table.children[3].config.selector.options[0].value
See [https://github.com/google/gson/blob/main/Troubleshooting.md#unexpected-json-structure](https://gitee.com/link?target=https%3A%2F%2Fgithub.com%2Fgoogle%2Fgson%2Fblob%2Fmain%2FTroubleshooting.md%23unexpected-json-structure)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions