Spring Boot接口报Failed to read request请求解析失败求助
问题:Spring Boot接口请求解析失败,无法进入断点
使用Swagger或Postman调用Spring Boot的/lineinpolygon接口时,收到“Failed to read request”响应,代码从未进入断点。相关代码和请求JSON如下:
Controller代码
@PostMapping(value = "/lineinpolygon", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @Operation(summary = "Is line within polygon.", description = "Is line within polygon. Give the id for the for the polygon and a track to check. The geometry format should be Lon, Lat, Alt. The SRID for the geometries use 4326.", responses = { @ApiResponse(responseCode = "200", description = "Success", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = WithinPolygonResponse.class)) }), @ApiResponse(responseCode = "400", description = "An error occurred while getting if line is within polygon.") }) public ResponseEntity<WithinPolygonResponse> getLineWithinPolygon(@RequestBody LineWithinRequestID entity) { try { Optional<Integer>opSrid = Optional.ofNullable(entity.getSrid()) ; boolean match = myService.isLineContainedInPolygon(entity.getId(),entity.getCoordinateList(), opSrid.orElse(4326)); return ResponseEntity.ok(new WithinPolygonResponse(match)); } catch (Exception exc) { throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Get line Within polygon error.", exc); } }
LineWithinRequestID实体类
@Getter @Setter @Data @NoArgsConstructor @JsonInclude(JsonInclude.Include.NON_NULL) @JsonIgnoreProperties(ignoreUnknown = true) public class LineWithinRequestID { @JsonProperty("id") private Long id; @JsonProperty("srid") private Integer srid; @JsonProperty("coordinates") private List<List<Double>> coordinateList; }
WithinPolygonResponse实体类
@Getter @Setter @Data @NoArgsConstructor @AllArgsConstructor @JsonInclude(JsonInclude.Include.NON_NULL) @JsonIgnoreProperties(ignoreUnknown = true) public class WithinPolygonResponse { @JsonProperty("status") private Boolean status; }
请求JSON
{ "id": 2, "srid": 4326, "coordinates": [ [ [-77.292894, 38.392816, 0], [-77.169498, 38.468925, 0], [-77.204214, 38.568971, 0] ] ] }
问题原因及修复方案
核心问题是请求JSON的coordinates结构和实体类定义不匹配:
- 实体类中
coordinateList定义为List<List<Double>>,对应JSON应该是[[x1,y1,z1], [x2,y2,z2], ...]的两层数组结构,用来表示一条线的多个坐标点。 - 但你提供的请求JSON里,
coordinates多嵌套了一层,变成了三层数组[[[x1,y1,z1], [x2,y2,z2], ...]],Jackson无法将三层数组解析为两层的List结构,导致请求解析失败,直接返回400错误,代码自然进不了断点。
修复步骤:
- 修正请求JSON,去掉
coordinates外层多余的数组:
{ "id": 2, "srid": 4326, "coordinates": [ [-77.292894, 38.392816, 0], [-77.169498, 38.468925, 0], [-77.204214, 38.568971, 0] ] }
- (可选)如果业务逻辑确实需要三层数组结构,需修改实体类的
coordinateList定义为List<List<List<Double>>>,同时调整Service层的处理逻辑,但从接口描述来看,两层数组更符合“线的坐标点”的业务场景。
额外排查建议:
- 开启Spring Boot请求日志,查看详细解析错误:在
application.yml中添加配置,能直接看到Jackson解析失败的具体原因,方便快速定位问题:
logging: level: org.springframework.web: DEBUG
- 使用Swagger时,直接用实体类生成的请求示例测试,避免手动构造JSON时出现结构错误。
内容的提问来源于stack exchange,提问作者user398482
相关产品推荐
相关产品推荐

