bean中属性名和json不一致解决方案(请求和响应)


此时@RequestBody、@ResponseBody需要与@JsonProperty结合使用,才能做到请求正常解析,响应按要求格式返回。
注意@JsonProperty注解的位置需要加在getter方法上。
如果直接加在属性上,响应会这样返回:
{
"actionStatus": "OK",
"errorInfo": "success",
"errorCode": 0,
"ActionStatus": "OK",
"ErrorInfo": "success",
"ErrorCode": 0
}
正常实现的示例代码如下:
@RequestMapping(value = "/tecentim/v1/callback", method = RequestMethod.POST)
@ResponseBody
public CallbackRsp imcallback(
@RequestParam String SdkAppid,
@RequestParam String CallbackCommand,
@RequestParam String contenttype,
@RequestParam String ClientIP,
@RequestParam String OptPlatform,
@RequestBody CallbackCommand command) {
CallbackRsp result = new CallbackRsp();
result.setActionStatus("OK");
result.setErrorInfo("success");
result.setErrorCode(0);
try {
System.out.println("SdkAppid:"+SdkAppid);
System.out.println("CallbackCommand:"+CallbackCommand);
System.out.println("contenttype:"+contenttype);
System.out.println("ClientIP:"+ClientIP);
System.out.println("OptPlatform:"+OptPlatform);
System.out.println(command);
} catch (DingDongFMException de) {
logger.error("imcallback failed", de);
} catch (Exception e) {
logger.error("imcallback failed", e);
}
return result;
}
import java.util.Arrays;
import com.fasterxml.jackson.annotation.JsonProperty;
public class CallbackCommand {
private String CallbackCommand; //回调命令
private String GroupId; //产生群消息的群组 ID
private String Type; //产生群消息的 群组类型,例如 Private,Public 和 ChatRoom
private String From_Account; //消息发送者 ID
private Integer MsgTime; //消息发送的时间戳,对应后台 Server 时间
private Integer MsgSeq; //消息序列号,唯一标示一条消息
private MsgBody[] MsgBody;//消息体,具体参见 消息格式描述
@JsonProperty(value = "CallbackCommand")
public String getCallbackCommand() {
return CallbackCommand;
}
public void setCallbackCommand(String callbackCommand) {
CallbackCommand = callbackCommand;
}
@JsonProperty(value = "GroupId")
public String getGroupId() {
return GroupId;
}
public void setGroupId(String groupId) {
GroupId = groupId;
}
@JsonProperty(value = "Type")
public String getType() {
return Type;
}
public void setType(String type) {
Type = type;
}
@JsonProperty(value = "From_Account")
public String getFrom_Account() {
return From_Account;
}
public void setFrom_Account(String from_Account) {
From_Account = from_Account;
}
@JsonProperty(value = "MsgTime")
public Integer getMsgTime() {
return MsgTime;
}
public void setMsgTime(Integer msgTime) {
MsgTime = msgTime;
}
@JsonProperty(value = "MsgSeq")
public Integer getMsgSeq() {
return MsgSeq;
}
public void setMsgSeq(Integer msgSeq) {
MsgSeq = msgSeq;
}
@JsonProperty(value = "MsgBody")
public MsgBody[] getMsgBody() {
return MsgBody;
}
public void setMsgBody(MsgBody[] msgBody) {
MsgBody = msgBody;
}
@Override
public String toString() {
return "CallbackCommand [CallbackCommand=" + CallbackCommand + ", GroupId=" + GroupId + ", Type=" + Type
+ ", From_Account=" + From_Account + ", MsgTime=" + MsgTime + ", MsgSeq=" + MsgSeq + ", MsgBody="
+ Arrays.toString(MsgBody) + "]";
}
}
import com.fasterxml.jackson.annotation.JsonProperty;
public class CallbackRsp {
private String ActionStatus;
private String ErrorInfo;
private Integer ErrorCode;
@JsonProperty(value = "ActionStatus")
public String getActionStatus() {
return ActionStatus;
}
public void setActionStatus(String actionStatus) {
ActionStatus = actionStatus;
}
@JsonProperty(value = "ErrorInfo")
public String getErrorInfo() {
return ErrorInfo;
}
public void setErrorInfo(String errorInfo) {
ErrorInfo = errorInfo;
}
@JsonProperty(value = "ErrorCode")
public Integer getErrorCode() {
return ErrorCode;
}
public void setErrorCode(Integer errorCode) {
ErrorCode = errorCode;
}
@Override
public String toString() {
return "CallbackRsp [ActionStatus=" + ActionStatus + ", ErrorInfo=" + ErrorInfo + ", ErrorCode=" + ErrorCode
+ "]";
}
}
bean中属性名和json不一致解决方案(请求和响应)的更多相关文章
- java中的getProperty()方法。获取系统中属性名为key的属性对应的值
总结:getProperty方法:获取系统中属性名为key的属性对应的值,系统中常见的属性名以及属性如下: 现在用getProperty()的方法,获取系统信息代码: package com.aaa; ...
- MyBatis(5)——解决属性名与列名不一致的问题
解决属性名与列名不一致的问题 问题描述: 当实体类的属性与数据库的列名不对应时取不到该列数据 说明:MyBatis会根据查询的列名设值(列名的setter方法),然后以此列名为做查询等操作,在此过程中 ...
- resultMap结果集映射解决属性名和字段不一致问题
解决属性名和字段名不一致的问题 1.出现的问题 数据库中的字段 新建一个项目,拷贝之前的,测试实体类与数据库字段不一致的情况 public class User { private int id; ...
- java jackson 忽略不存在的属性字段 和 按照属性名转json
@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, isGetterVisibi ...
- Visual Studio 下C#编译器在解析属性名时如果增加一个get_[您的另一个已经包含在类中属性名]的属性会报错,微软大哥这是什么鬼?
假设在在我们的vs环境新建一个类 copy以下代码,表面看好像一切都没有问题. using System; using System.Collections.Generic; using System ...
- Dubbo中服务消费者和服务提供者之间的请求和响应过程
服务提供者初始化完成之后,对外暴露Exporter.服务消费者初始化完成之后,得到的是Proxy代理,方法调用的时候就是调用代理. 服务消费者经过初始化之后,得到的是一个动态代理类,InvokerIn ...
- Django & JavaScript 用Ajax实现JSON数据的请求和响应
[描述] 1.Server端定义了两个字段:Article.title 和 Article.content 2.客户端用JavaScript Ajax异步加载请求服务器的JSON数据 效果是点击按钮从 ...
- es6中的对象的可计算的属性名
先简单的啰嗦一下对象的属性: var obj = { a:2 } 要访问obj中a的位置,方法:1. obj.a //2 2..obj ["a"] ...
- Mybatis系列(二):优化MyBatis配置文件中的配置和解决字段名与实体类属性名不相同的冲突
原文链接:http://www.cnblogs.com/xdp-gacl/p/4264301.html http://www.cnblogs.com/xdp-gacl/p/4264425.ht ...
随机推荐
- 「刷题笔记」Tarjan
贴一个讲得非常详细的\(tarjan\)入门教程 信息传递 讲个笑话:我之前用并查集求最小环过的这题,然后看见题目上有个\(tarjan\)标签 留下了深刻的印象:\(tarjan\)就是并查集求最小 ...
- 【GDKOI2014】JZOJ2020年8月13日提高组T2 石油储备计划
[GDKOI2014]JZOJ2020年8月13日提高组T2 石油储备计划 题目 Description Input Output 对于每组数据,输出一个整数,表示达到"平衡"状态 ...
- 【佛山市选2013】JZOJ2020年8月7日提高组T1 回文子序列
[佛山市选2013]JZOJ2020年8月7日提高组T1 回文子序列 题目 描述 回文序列是指左右对称的序列.例如1 2 3 2 1是回文序列,但是1 2 3 2 2就不是.我们会给定一个N×M的矩阵 ...
- Docker学习—Stack
前言: 前一篇了解Docker使用Swarm集群部署方式,并创建服务到Swarm集群中:如果在集群部署过程中存在大量服务部署.编排那么该如何处理呢? 那么就需要了解Docker Stack了. 1.D ...
- 第15.32节 PyQt(Python+Qt)入门学习:containers容器类部件QToolBox工具箱介绍及使用案例
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.概述 容器部件就是可以在部件内放置其他部件的部件,在Qt Designer中可以使用的容器部件有 ...
- PyQt学习随笔:ListView控件的视图和数据模型分离案例
Qt 中view类控件的目的是实现数据和模型分离,控件展示数据,数据保存在数据存储中,数据存储中的数据改变了,则控件中展示的数据跟随改变.当设计时只指定了一个控件和一个数据存储关联时,这种分离虽然也能 ...
- 刷题记录:[GWCTF 2019]枯燥的抽奖
目录 刷题记录:[GWCTF 2019]枯燥的抽奖 知识点 php伪随机性 刷题记录:[GWCTF 2019]枯燥的抽奖 题目复现链接:https://buuoj.cn/challenges 参考链接 ...
- jmeter使用中的问题
1.响应乱码 step1:指定请求节点下,新建后置控制器"BeanShell PostProcessor" step2:其脚本框中输入以下代码,保存 //获取响应代码Unicode ...
- leetcode——(四)2020.06.08
新的一周,leetcode计划:78,79,98,102,236,124,128 (23)
- Geoserver对发布的数据源进行金字塔切片
一.建立切片数据源 1.1建立工作区 1.2添加数据 我这里是老师给的高清卫星地图数据,格式为tif 工作区选择之前建立的工作区,浏览那里选择对应的文件 1.3建立切片源的图层 这里建立的图层中先不用 ...