Lombok returns null 作为响应值
Lombok returns null as a value of response
我的 Api 测试有问题。
当我尝试从api获取数据时,lombok returns null作为接受值,但api.[=17=中有实数值]
我的 DTO 响应:
@Data
@Builder
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class PositionStatResponceDto {
private Integer keywordTasksCount;
private Integer doneKeywordTasksCount;
private Integer tasksCount;
private Integer doneTasks;
}
我提取正文并发送post请求的步骤
public class 位置步数 {
PositionsController positionsController = new PositionsController();
@Step("Post body with url: http://prod.position.bmp.rocks/api/aparser/get-statistic")
public PositionStatResponceDto postBody(PositionStatDto positionStatDto) {
return positionsController
.getStatistic(positionStatDto)
.statusCode(200)
.extract().body().as(PositionStatResponceDto.class);
}
}
Api json 响应正常。这意味着请求工作正常:
{
"period": {
"20201224": {
"startTime": "2020-12-24 00:00:19",
"endTime": "2020-12-24 06:39:30",
"totalRequestsCount": 0,
"totalQueriesCount": 161887,
"totalQueriesDoneCount": 161887,
"totalFailCount": 161,
"successfulQueries": 161726,
"proxiesUsedCount": 6.49,
"retriesUsedCount": 0,
"avgSpeed": 13.74,
"tasksCount": 1537,
"doneTasks": 1537,
"keywordTasksCount": 725,
"doneKeywordTasksCount": 725,
"runTime": "06:39:11",
"avgTimePerKeyword": 0.15,
"keywordsLost": 0.1
}
},
"avg": {
"totalRequestsCount": 0,
"totalQueriesCount": 161887,
"totalQueriesDoneCount": 161887,
"totalFailCount": 161
}
}
我以与 api 类似的方式提出了 post 请求:
{
"success": 1,
"data": {
"45.90.34.87:59219": [
"http"
],
"217.172.179.54:39492": [
"http"
],
"144.76.108.82:35279": [
"http"
],
"5.9.72.48:43210": [
"http"
],
"144.76.108.82:47165": [
"http"
],
"45.90.34.87:57145": [
"http"
],
"144.76.108.82:53108": [
"http"
],
...
} }
它与 dto 一起正常工作:
@Data
@Builder
@EqualsAndHashCode(exclude = "success")
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class AparsersResponceDto {
private Integer success;
private Map<String, List<String>> data;
}
请帮帮我。我不明白第一个例子有什么问题。每个 Dto 值返回 'null'.
您的 DTO 与您正在解析的响应的结构不匹配。您有一个嵌套结构,在 DTO 上您期望只接收原始值。在上层,您有一个包含两个字段的结构。
{
"period": {...},
"avg": {...}
}
根据示例,我假设 period
是一个键值对日期作为键,您的 PositionStatResponceDto
作为值。
{
"period" : {
"20201224": { <-- nested key with a value matching your DTO
PositionStatResponceDto
}
...
}
所以这意味着只有键值对中的单个项目与您定义的 DTO 匹配,但忽略了所有其他嵌套结构元素。为此,引入新的包装器 DTO 来处理嵌套结构是有意义的。
例如
public class StatDTO {
private Map<String,PositionStatResponceDto> period;
//add avg if needed
}
我的 Api 测试有问题。
当我尝试从api获取数据时,lombok returns null作为接受值,但api.[=17=中有实数值]
我的 DTO 响应:
@Data
@Builder
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class PositionStatResponceDto {
private Integer keywordTasksCount;
private Integer doneKeywordTasksCount;
private Integer tasksCount;
private Integer doneTasks;
}
我提取正文并发送post请求的步骤 public class 位置步数 {
PositionsController positionsController = new PositionsController();
@Step("Post body with url: http://prod.position.bmp.rocks/api/aparser/get-statistic")
public PositionStatResponceDto postBody(PositionStatDto positionStatDto) {
return positionsController
.getStatistic(positionStatDto)
.statusCode(200)
.extract().body().as(PositionStatResponceDto.class);
}
}
Api json 响应正常。这意味着请求工作正常:
{
"period": {
"20201224": {
"startTime": "2020-12-24 00:00:19",
"endTime": "2020-12-24 06:39:30",
"totalRequestsCount": 0,
"totalQueriesCount": 161887,
"totalQueriesDoneCount": 161887,
"totalFailCount": 161,
"successfulQueries": 161726,
"proxiesUsedCount": 6.49,
"retriesUsedCount": 0,
"avgSpeed": 13.74,
"tasksCount": 1537,
"doneTasks": 1537,
"keywordTasksCount": 725,
"doneKeywordTasksCount": 725,
"runTime": "06:39:11",
"avgTimePerKeyword": 0.15,
"keywordsLost": 0.1
}
},
"avg": {
"totalRequestsCount": 0,
"totalQueriesCount": 161887,
"totalQueriesDoneCount": 161887,
"totalFailCount": 161
}
}
我以与 api 类似的方式提出了 post 请求:
{
"success": 1,
"data": {
"45.90.34.87:59219": [
"http"
],
"217.172.179.54:39492": [
"http"
],
"144.76.108.82:35279": [
"http"
],
"5.9.72.48:43210": [
"http"
],
"144.76.108.82:47165": [
"http"
],
"45.90.34.87:57145": [
"http"
],
"144.76.108.82:53108": [
"http"
],
...
} }
它与 dto 一起正常工作:
@Data
@Builder
@EqualsAndHashCode(exclude = "success")
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class AparsersResponceDto {
private Integer success;
private Map<String, List<String>> data;
}
请帮帮我。我不明白第一个例子有什么问题。每个 Dto 值返回 'null'.
您的 DTO 与您正在解析的响应的结构不匹配。您有一个嵌套结构,在 DTO 上您期望只接收原始值。在上层,您有一个包含两个字段的结构。
{
"period": {...},
"avg": {...}
}
根据示例,我假设 period
是一个键值对日期作为键,您的 PositionStatResponceDto
作为值。
{
"period" : {
"20201224": { <-- nested key with a value matching your DTO
PositionStatResponceDto
}
...
}
所以这意味着只有键值对中的单个项目与您定义的 DTO 匹配,但忽略了所有其他嵌套结构元素。为此,引入新的包装器 DTO 来处理嵌套结构是有意义的。 例如
public class StatDTO {
private Map<String,PositionStatResponceDto> period;
//add avg if needed
}