此时@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. redis面试问题(二)

    1.redis和其他缓存相比有哪些优点呢 见上一篇 2. 你刚刚提到了持久化,能重点介绍一下么 见上一篇 3.Redis中对于IO的控制做过什么优化? pipeline? 4 有没有尝试进行多机red ...

  2. 虚拟机VMware15 Ubuntu18.04 搭建FTP服务器

    1.安装vsftpd sudo apt install vsftpd 2.查看是否安装成功,出现版本等信息即成功 sudo vsftpd -v 3.添加ftp用户 sudo useradd -m su ...

  3. LeetCode 033 Search in Rotated Sorted Array

    题目要求:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you b ...

  4. 05_Content Provider

    Content Provider是内容提供器,与内容(数据)的存取(存储.获取)有关,是Android应用程序的四大组成部分之一,是Android中的跨应用访问数据机制. 数据库在Android当中是 ...

  5. Happy Tree Friends——团队展示

    这个作业属于哪个课程 软件工程 (福州大学至诚学院 - 计算机工程系) 团队名称 Happy Tree Friends 这个作业要求在哪里 团队作业第一次 这个作业的目标 团队合作 作业正文 正文 其 ...

  6. 当vue.js与其他js文件同时引用导致页面不显示的问题

    作为一个萌新,昨天学习的过程中遇到了vuejs与其他js在共同页面时引用时冲突的问题 具体如下 虽然注意到了前后顺序,但是页面还是出不来东西 我知道现实开发中可能不是这么引用,但是学习中是这么引入的, ...

  7. day3(使用axios实现登录成功)

    1.创建一个login.vue页面 1.1写页面components/Login.vue 在 src/components 下创建 Login.vue 页面 <template>   &l ...

  8. Docker学习—Stack

    前言: 前一篇了解Docker使用Swarm集群部署方式,并创建服务到Swarm集群中:如果在集群部署过程中存在大量服务部署.编排那么该如何处理呢? 那么就需要了解Docker Stack了. 1.D ...

  9. moviepy执行TextClip.search方法时报错TypeError: a bytes-like object is required, not str

    ☞ ░ 前往老猿Python博文目录 ░ 执行TextClip.search方法时,报错: >>> from moviepy.editor import * >>> ...

  10. Python中排序方法sort、函数sorted的key参数的作用分析

    从Python2.4开始,list.sort方法 和 sorted方法 都增加了一个 'key' 参数用来在进行比较之前指定每个列表元素上要调用的函数,将函数的返回值作为比较的依据. 那么怎么使用这个 ...