Почему значение ответа неверно для POST в Swagger? - PullRequest
1 голос
/ 03 апреля 2020

Я пишу документ API через Swagger. Для GET method значение образца модели Response возвращается корректно, но не корректно для POST. Это всегда фиксированное значение.

Конфигурация Swagger. java

 @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2).host("localhost:8080").select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/api/**"))
                .build()
                .directModelSubstitute(LocalDate.class, String.class)
                .genericModelSubstitutes(ResponseEntity.class)
                .alternateTypeRules(
                    newRule(typeResolver.resolve(DeferredResult.class,
                        typeResolver.resolve(ResponseEntity.class, WildcardType.class)),
                        typeResolver.resolve(WildcardType.class)
                    ))
                .useDefaultResponseMessages(false)
                .ignoredParameterTypes(
                    Pageable.class,
                    PagedResourcesAssembler.class,
                    AuthenticationPrincipal.class,
                        OAuth2Authentication.class
                )
                .securitySchemes(singletonList(securityScheme()))
                .securityContexts(singletonList(securityContext()))
                .apiInfo(getApiInfo());
    }

Контроллер API

    @ApiOperation(value = "FIle Upload" , consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity<EntityModel<FileDTO.Response>> uploadFile(@ApiParam(value = "Select te file to Upload")MultipartFile file) throws FileUploadException {
        return ResponseEntity.ok(fileResourceAssembler.toModel(fileService.storeFile(file)));
    }

    @ApiOperation(value = "File Download")
    @GetMapping("/{id}")
    public ResponseEntity<Resource> downloadFile(@PathVariable final Long id) throws FileNotFoundException {
        AttachFile attachFile = fileService.loadFile(id);

        return  ResponseEntity.ok().contentType(MediaType.parseMediaType(attachFile.getContentType()))
                .header(HttpHeaders.CONTENT_DISPOSITION,"attachment; filename=\"" + attachFile.getFilename() + "\"")
                .header(HttpHeaders.ACCEPT_CHARSET, StandardCharsets.UTF_8.toString())
                .body(new ByteArrayResource(attachFile.getData()));
    }

DTO

public class FileDTO {

    @Setter
    @Getter
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString
    @Builder
    public static class Response {

        @NotNull
        private Long id;

        private String serverPath;

        @NotEmpty
        private String filename;

        @NotEmpty
        private String extension;

        @NotEmpty
        private String contentType;

        private Long size;

        private Date insertDate;
    }

}

Swagger enter image description here

enter image description here

...