简体   繁体   English

在带有 MVC REST API 的 SpringBoot 项目中返回 406 响应

[英]In a SpringBoot project with MVC REST API is returning 406 response

I am working on a SpringBoot project, that uses the regular MVC mechanism to expose the REST APIs.我正在开发一个 SpringBoot 项目,该项目使用常规 MVC 机制来公开 REST API。

In one particular GET API, I am getting 406 HTTP Response.在一个特定的 GET API 中,我收到了 406 HTTP 响应。

Here is how my Controller method looks:这是我的 Controller 方法的外观:

    @GetMapping(path = "/analysis/detail/{analysisId}")
    public ResponseEntity<AnalysisDetailResponse> getAnalysisDetails(
            @PathVariable @NotNull Long analysisId) throws BusinessException {
        AnalysisDetailResponse analysisDetailResponse = analysisService.getAnalysisDetailsByAnalysisId(analysisId);
//        return ResponseEntity.ok(analysisService.getAnalysisDetailsByAnalysisId(analysisId));
        return ResponseEntity.ok(analysisDetailResponse);
    }

The AnalysisDetailResponse is created using Lombok (which is working flawlessly in case of other APIs) AnalysisDetailResponse 是使用 Lombok 创建的(在其他 API 的情况下可以完美地工作)

@Builder
@ToString
public class AnalysisDetailResponse {

    @NotNull
    private Long id;

    @NotNull
    private AnalysisStatus state;

    private String failedSummary;

    @NotNull
    @NotEmpty
    @Valid
    private List<PostDetail> posts;

    @Builder
    @ToString
    private static class PostDetail {

        @NotNull
        private Long id;

        @NotNull
        private float score;

        @NotNull
        private String body;

        private List<String> childBodyList;
    }
}

I've verified the contents of the entire Response object and it seems to be perfect.我已经验证了整个 Response 对象的内容,它似乎是完美的。 However, the response is always 406.但是,响应始终是 406。

I need the response in JSON format, hence, explicit mentioning of the response type isn't really necessary IMO.我需要 JSON 格式的响应,因此,IMO 没有必要明确提及响应类型。

Nevertheless, I tried adding the below content to the @GetMapping annotation, but found no luck:尽管如此,我尝试将以下内容添加到 @GetMapping 注释中,但没有发现任何运气:

produces = MediaType.APPLICATION_JSON_VALUE

A majority of relevant posts are suggesting to add the jackson-mapper-asl and jackson-core-asl libraries.大多数相关帖子都建议添加jackson-mapper-asljackson-core-asl库。 I tried it, but that didn't make any difference我试过了,但这没有任何区别

Please note, it's just this particular API that's causing issues.请注意,正是这个特定的 API 导致了问题。 There are other APIs defined in the same Controller that are working fine.在同一个控制器中定义的其他 API 工作正常。

Kindly suggest....请建议....

Update: I am trying to hit the API using Postman and I did try adding the Accept Header parameter explicitly.更新:我正在尝试使用 Postman 访问 API,并且我确实尝试明确添加Accept Header 参数。 No luck with it没有运气

Was able to get this working eventually by updating the Response object class's definition.通过更新 Response 对象类的定义,最终能够使其工作。 Lombok was been used and I had applied the @Data annotation to the Response class as well as the static inner class.使用了 Lombok,我将@Data注释应用于 Response 类以及静态内部类。 The motive was to club multiple Lombok annotations into one Replacing the @Data annotation with a more verbose set including @NoArgsConstructor, @AllArgsConstructor, @Getter, @Setter resolved the issue.动机是将多个 Lombok 注释合并为一个 用包括@NoArgsConstructor, @AllArgsConstructor, @Getter, @Setter在内的更详细的集合替换@Data注释解决了该问题。

So, one or more of these Lombok annotations was the real culprit in this scenario: @ToString, @EqualsAndHashCode, @RequiredArgsConstructor因此,在这种情况下,这些 Lombok 注释中的一个或多个是真正的罪魁祸首: @ToString, @EqualsAndHashCode, @RequiredArgsConstructor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM