zoukankan      html  css  js  c++  java
  • 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
                    + "]";
        }
        
        
    }
  • 相关阅读:
    lua pbc
    c保存lua函数
    c语言的lua库编写
    Gridcontrol新增行选中有关问题
    python3 练习题100例 (十五)
    python3 练习题100例 (十四)
    python3 练习题100例 (十三)
    python3 练习题100例 (十二)
    python3 练习题100例 (十一)
    python3 练习题100例 (十)
  • 原文地址:https://www.cnblogs.com/xianz666/p/13297268.html
Copyright © 2011-2022 走看看