반응형
webMvcConfigurer 랑 resources handler 추가
//implements WebMvcConfigurer
//extends WebMvcConfigurationSupport
package com.spring2.nyong.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableWebMvc
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {
@Bean
public Docket swaggerApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(swaggerInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.spring2.nyong.controller"))
.paths(PathSelectors.any())
.build()
.useDefaultResponseMessages(false); // 기본으로 세팅되는 200,401,403,404 메시지 표시 x
}
@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/");
// -- Static resources
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
registry.addResourceHandler("/css/**").addResourceLocations("classpath:/static/css");
registry.addResourceHandler("/js/**").addResourceLocations("classpath:/static/js/");
registry.addResourceHandler("/images/**").addResourceLocations("classpath:/static/images/");
}
private ApiInfo swaggerInfo() {
return new ApiInfoBuilder().title("Spring Nyong API Documentation")
.description("뇽이 서버 API에 대한 연동 문서입니다.")
.license("nyong").licenseUrl("https://crazynyong.tistory.com/")
.version("1")
.build();
}
}
혹시 시큐리티 컨피규레이션 쓰면 "/v2/api-docs", "/configuration/**", "/swagger*/**", "/webjars/**" 요것들 권한 permitAll로 바꾼다.
.antMatchers("/resources/**","/css/**", "/images/**","/configuration/**", "/js/**", "/h2-console/**","/v2/api-docs", "/configuration/**", "/swagger*/**", "/webjars/**")
.permitAll()
만약 이거 해도 안되면 인텔리제이,sts,비쥬얼스튜디오 세개다 써서 켜봣는데
인텔리제이랑 sts는 되는데 비쥬얼스튜디오로 키면 swagger.html 안된다.
v2/api-docs 는 세개다됨 우선 저거 되는지보고 되면 툴을 다른거로 바꿔서 서버실행해보세요!
반응형
댓글