此时@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不一致解决方案(请求和响应)的更多相关文章

  1. java中的getProperty()方法。获取系统中属性名为key的属性对应的值

    总结:getProperty方法:获取系统中属性名为key的属性对应的值,系统中常见的属性名以及属性如下: 现在用getProperty()的方法,获取系统信息代码: package com.aaa; ...

  2. MyBatis(5)——解决属性名与列名不一致的问题

    解决属性名与列名不一致的问题 问题描述: 当实体类的属性与数据库的列名不对应时取不到该列数据 说明:MyBatis会根据查询的列名设值(列名的setter方法),然后以此列名为做查询等操作,在此过程中 ...

  3. resultMap结果集映射解决属性名和字段不一致问题

    解决属性名和字段名不一致的问题 1.出现的问题 数据库中的字段 ​ 新建一个项目,拷贝之前的,测试实体类与数据库字段不一致的情况 public class User { private int id; ...

  4. java jackson 忽略不存在的属性字段 和 按照属性名转json

    @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, isGetterVisibi ...

  5. Visual Studio 下C#编译器在解析属性名时如果增加一个get_[您的另一个已经包含在类中属性名]的属性会报错,微软大哥这是什么鬼?

    假设在在我们的vs环境新建一个类 copy以下代码,表面看好像一切都没有问题. using System; using System.Collections.Generic; using System ...

  6. Dubbo中服务消费者和服务提供者之间的请求和响应过程

    服务提供者初始化完成之后,对外暴露Exporter.服务消费者初始化完成之后,得到的是Proxy代理,方法调用的时候就是调用代理. 服务消费者经过初始化之后,得到的是一个动态代理类,InvokerIn ...

  7. Django & JavaScript 用Ajax实现JSON数据的请求和响应

    [描述] 1.Server端定义了两个字段:Article.title 和 Article.content 2.客户端用JavaScript Ajax异步加载请求服务器的JSON数据 效果是点击按钮从 ...

  8. es6中的对象的可计算的属性名

    先简单的啰嗦一下对象的属性: var obj = { a:2 } 要访问obj中a的位置,方法:1. obj.a     //2            2..obj ["a"]   ...

  9. Mybatis系列(二):优化MyBatis配置文件中的配置和解决字段名与实体类属性名不相同的冲突

    原文链接:http://www.cnblogs.com/xdp-gacl/p/4264301.html     http://www.cnblogs.com/xdp-gacl/p/4264425.ht ...

随机推荐

  1. sql注入绕过安全狗实战

    =绕过 like regexp(匹配)> < (测试安全狗 ><不可以绕过) 注释+换行(%0a作为垃圾数据填充) 联合查询绕过 探测出当union select 出现时会拦截 ...

  2. Feign 超时设置

    问题描述 微服务之间使用 Feign 调用,偶发超时问题,配置如下: feign: client: config: default: connectTimeout: 10000 readTimeout ...

  3. SQL Alias模板

    再记不住的话就扇自己巴掌 SELECT * FROM class1 c INNER JOIN class2 p ON c.id = p.id INNER JOIN class3 s ON c.id = ...

  4. 分享篇:聊一聊 15.5K 的 FileSaver,是如何工作的?

    聊一聊 15.5K 的 FileSaver,是如何工作的? FileSaver.js 是在客户端保存文件的解决方案,非常适合在客户端上生成文件的 Web 应用程序.它简单易用且兼容大多数浏览器,被作为 ...

  5. 第7.8节 Python中隐秘的类封装方法

    前面章节已经介绍了Python中的多态和继承,本节将介绍面向对象程序设计OOP三大特征的另一个特征--封装. 一.    概念 封装是将对象的状态信息(也就是数据.属性)隐藏在对象内部,将对象的属性和 ...

  6. 第15.30节 PyQt编程实战:通过eventFilter监视QScrollArea的widget()的Paint消息画出scrollAreaWidgetContents的范围矩形

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.引言 在<PyQt(Python+Qt)学习随笔:QScrollArea滚动区域详解> ...

  7. 在浏览器中输入URL后,执行的全部过程。会用到哪些协议?(一次完整的HTTP请求过程)

    在浏览器中输入URL后,执行的全部过程.会用到哪些协议?(一次完整的HTTP请求过程) 整个流程如下: 域名解析 为了将消息从你的PC上传到服务器上,需要用到IP协议.ARP协议和OSPF协议. 发起 ...

  8. 跨国合作:Serverless Components 在腾讯云的落地和实践

    导语 | Serverless Components 是 Serverless Framework 推出的最新解决⽅案,具有基础设施编排能⼒,开发者通过使⽤ Serverless Components ...

  9. WPF中DatePiker值绑定以及精简查询

    WPF中DatePiker值绑定以及精简查询 1.WPF中DatePiker值绑定 Xaml中值绑定使用Text <DatePicker Text="{Binding strMinDa ...

  10. C#实例化对象的三种方式及性能对比

    前言 做项目过程中有个需求要实例化两万个对象并添加到List中,这个过程大概需要1min才能加载完(传参较多),于是开启了代码优化之旅,再此记录. 首先想到的是可能实例化比较耗时,于是开始对每种实例化 ...