简体   繁体   中英

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.

In one particular GET API, I am getting 406 HTTP Response.

Here is how my Controller method looks:

    @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)

@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. However, the response is always 406.

I need the response in JSON format, hence, explicit mentioning of the response type isn't really necessary IMO.

Nevertheless, I tried adding the below content to the @GetMapping annotation, but found no luck:

produces = MediaType.APPLICATION_JSON_VALUE

A majority of relevant posts are suggesting to add the jackson-mapper-asl and jackson-core-asl libraries. I tried it, but that didn't make any difference

Please note, it's just this particular API that's causing issues. There are other APIs defined in the same Controller that are working fine.

Kindly suggest....

Update: I am trying to hit the API using Postman and I did try adding the Accept Header parameter explicitly. No luck with it

Was able to get this working eventually by updating the Response object class's definition. Lombok was been used and I had applied the @Data annotation to the Response class as well as the static inner class. 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.

So, one or more of these Lombok annotations was the real culprit in this scenario: @ToString, @EqualsAndHashCode, @RequiredArgsConstructor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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