What is the correct posture for @ EnableWebMvc in springboot?

1. Describe
I used springboot to integrate swagger, and upload files at the same time. The code is as follows ( code function is fine, it has been tested ):

//
@Api(value = "", description = "API")
@RestController
public class UploadController {

    @Autowired
    private FileUploadService fileUploadService;

    @ApiOperation(value = "", notes = "")
    @RequestMapping(value = "/api/upload", method = RequestMethod.POST, consumes = "multipart/form-data")
    public RestResult upload(@RequestParam MultipartFile file) {
        String fileFullPath = fileUploadService.upload(file);
        return RestResultGenerator.getSuccessResult(fileFullPath);
    }

}

//swagger2
@ConditionalOnProperty(prefix = "swagger2", value = {"enable"}, havingValue = "1")
@EnableSwagger2
@Configuration
public class Swagger2Config {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.freedom.clothing.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Restful API")
                .contact(new Contact("xx", "", ""))
                .version("1.0")
                .description("")
                .build();
    }
}

//DisPatcherServlet
@Configuration
@EnableWebMvc//(swagger2)
public class WebMVCConfig extends WebMvcConfigurerAdapter {

    @Bean
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
        resolver.setDefaultEncoding("utf-8");
        resolver.setMaxInMemorySize(40960);
        resolver.setMaxUploadSize(10485760);

        return resolver;
    }

}

2. The problem I encountered
1 but when I turned on the @ EnableWebMvc annotation, the file could be uploaded, but swagger2 could not access .

@EnableWebMvcswagger2uploadorg.springframework.web.HttpMediaTypeNotSupportedException

3. The question I want to ask
it took me a long time to figure out why, and I always thought there was something wrong with the upload.

my thinking: when @ EnableWebMvc is added to
, it is obvious that the static resources of swagger are blocked, and if you do not add @ EnableWebMvc, you will not intercept.

1 is not all set DispatcherServlet, and the mapping mapping of the two cases is different?
what exactly does 2@EnableWebMvc do? Why sometimes it doesn"t seem to matter whether you add it or not (it should have nothing to do with springboot"s @ EnableAutoConfiguration, I didn"t open this comment).

Apr.03,2021

it is recommended to print out the mapping in the startup log
these are swagger-related resource mappings
clipboard.png

swagger-ui.htmljar
clipboard.png

swagger-ui.html
clipboard.png


when you turn on @ EnableWebMvc, add the following code so that DispatcherServlet does not intercept static resources of swagger.

    /**
     * Swagger2
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

I don't know exactly what @ EnableWebMvc has done.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b38b0d-2c11b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b38b0d-2c11b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?