因为开发腾讯云业务,要写一个回调函数,规范文档上说会传一个json传给我
{
"EventType": "ComposeMediaComplete",
"ComposeMediaCompleteEvent": {
"TaskId": "1256768367-ComposeMedia-f5ac8127b3b6b85cdc13f237c6005d8",
"Status": "FINISH",
"ErrCode": 0,
"Message": "SUCCESS",
"Input": {
"Tracks": [{
"Type": "Video",
"TrackItems": [{
"Type": "Video",
"SourceMedia": "5285485487985271487",
"AudioOperations": [{
"Type": "Volume",
"VolumeParam": {
"Mute": 1
}
}]
}]
},
{
"Type": "Audio",
"TrackItems": [{
"Type": "Empty",
"EmptyItem": {
"Duration": 5
}
},
{
"Type": "Audio",
"AudioItem": {
"SourceMedia": "5285485487985271488",
"Duration": 15
}
},
{
"Type": "Audio",
"AudioItem": {
"SourceMedia": "5285485487985271489",
"SourceMediaStartTime": 2,
"Duration": 14
}
}
]
}
],
"Output": {
"FileName": "视频合成效果测试",
"Container": "mp4"
}
},
"Output": {
"FileType": "mp4",
"FileId": 5285485487985271490,
"FileUrl": "http://125676836723.vod2.myqcloud.com/xxx/xxx/xxx.mp4"
}
}
}
json结构比较复杂,嵌套类+属性首字母大写
按照格式,我声明了一个DTO类
@Data
public class ComposeCallbackRequest {
/**
* 事件类型:默认为ComposeMediaComplete
*/
private String EventType;
private ComposeMediaCompleteEvent ComposeMediaCompleteEvent;
@Data
class ComposeMediaCompleteEvent{
private String TaskId;
private String Status;
private Integer ErrCode;
private String Message;
private Output Output;
//一些不涉及业务的类 略去不再声明
}
@Data
class Output {
private String FileType;
private String FileId;
private String FileUrl;
}
}
经postman测试,后台收到属性均为null
原因与我声明属性名首字母大写有关:
这是由于JavaBean的规范导致的。一般JavaBean属性都是首字母小写,以驼峰命名格式命名,相应的 getter/setter 方法是 get/set 接上首字母大写的属性名。例如:属性名为clientId,其对应的getter/setter 方法是 getClientId/setClientId。
解决方法是在每个属性名上加@JsonProperty注解,例如@JsonProperty(“EventType”)
加上注解后,报了另一个错误信息:
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of
com.***.dto.request.ComposeCallbackRequest$Output
(although at least one Creator exists): can only instantiate non-static inner class by using default, no-argument constructor; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance ofcom.***.dto.request.ComposeCallbackRequest$Output
(although at least one Creator exists): can only instantiate non-static inner class by using default, no-argument constructor
重点信息:
can only instantiate non-static inner class //只能实例化非静态内部类
在内部类 Output 的声明前加了一个static,问题顺利解决。
放上最终代码:
@Data
public class ComposeCallbackRequest {
/**
* 事件类型:默认为ComposeMediaComplete
*/
@JsonProperty("EventType")
private String EventType;
@JsonProperty("ComposeMediaCompleteEvent")
private ComposeMediaCompleteEvent ComposeMediaCompleteEvent;
@Data
class ComposeMediaCompleteEvent{
@JsonProperty("TaskId")
private String TaskId;
@JsonProperty("Status")
private String Status;
@JsonProperty("ErrCode")
private Integer ErrCode;
@JsonProperty("Message")
private String Message;
@JsonProperty("Output")
private Output Output;
}
@Data
static class Output {
@JsonProperty("FileType")
private String FileType;
@JsonProperty("FileId")
private String FileId;
@JsonProperty("FileUrl")
private String FileUrl;
}
}