在Pojo下上传Spring Multipart文件

时间:2018-08-24 12:20:32

标签: spring-mvc swagger-2.0 spring-restcontroller

您好,我需要Multi-Part File Upload under the POJO class的帮助。我正在使用Swagger图像上传列表文件。当我运行代码时,它将请求模式显示为JSON而不是文件按钮

POJO类:-

public class User {

    static final long serialVersionUID = 1L;

    @ApiModelProperty(notes = "The database generated user, token and session mapping ID.")
    private Long id;

    @ApiModelProperty(notes = "user name")
    private String username;

    @ApiModelProperty(notes = "User password")
    private String password;

    @ApiModelProperty(notes = "Indicates whether the user is enabled or disabled. A disabled user cannot be authenticated.")
    private boolean enabled;

    @ApiModelProperty(notes = "Multiple file for user.", dataType = "java.io.File", required = true)
    private List<MultipartFile> multipartFiles;


}

Rest-Controller:-

@ApiOperation(value = "Authenticated User Login", response = User.class)
@RequestMapping(value = "/upload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ApiResponses(value = {
        @ApiResponse(code = 200, message = "Success", response = User.class),
        @ApiResponse(code = 401, message = "Unauthorized"),
        @ApiResponse(code = 403, message = "Forbidden"),
        @ApiResponse(code = 404, message = "Not Found"),
        @ApiResponse(code = 500, message = "Failure")})
public ResponseEntity<User> fileUpload(@RequestBody User user) { return new ResponseEntity(user, HttpStatus.OK); 
}

SwaggerConfig:-

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket productApi() {

        return new Docket(DocumentationType.SWAGGER_2)
                .select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any()).build().apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("Authentication API").description("")
                .termsOfServiceUrl("https://www.example.com/api")
                .contact(new Contact("Developers", "https://projects.spring.io/spring-boot/", ""))
                .license("Open Source")
                .licenseUrl("\"https://www.apache.org/licenses/LICENSE-2.0")
                .version("1.0.0")
                .build();

    }

}

昂首阔步的表演:-

{
  "enabled": false,
  "id": 0,
  "multipartFiles": {
    "absolute": true,
    "absolutePath": "string",
    "canonicalPath": "string",
    "directory": true,
    "executable": true,
    "file": true,
    "freeSpace": 0,
    "hidden": true,
    "lastModified": 0,
    "name": "string",
    "parent": "string",
    "path": "string",
    "readable": true,
    "totalSpace": 0,
    "usableSpace": 0,
    "writable": true
  },
  "password": "string",
  "username": "string"
}

我的问题是我如何通过Swagger在List of File's下上传POJO class

0 个答案:

没有答案