使用springboot開發rest接口,
第三方定義的接口的請求和響應格式要求如下,沒有按照正常的駝峯格式命名,
此時@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
+ "]";
}
}