Skip to content

Instantly share code, notes, and snippets.

@sojw
Last active August 5, 2019 02:09
Show Gist options
  • Save sojw/d8c55001b28d838a096c3256d4b1a7ac to your computer and use it in GitHub Desktop.
Save sojw/d8c55001b28d838a096c3256d4b1a7ac to your computer and use it in GitHub Desktop.
Spring SwaggerConfiguration JavaConfig example
@Api(value = "Some API")
@RestController
@RequestMapping(value = "/Service")
public class SampleApiContorller {
@Autowired
private ResponseEntityUtil responseEntityUtil;
@Autowired
private ServiceApiProxyFactory serviceApiProxyFactory;
@ApiOperation(value = "Category List API", notes = "get some list ", response = Category.class, responseContainer = "List")
@RequestMapping(value = {"/CategoryList.json"}, produces = Type.JSON_UTF8, method = RequestMethod.GET)
public ResponseEntity<?> legacyCategoryList(
@ApiParam(value = "service id", required = true) @RequestParam("serviceId") String serviceId,
@ApiParam(hidden = true) @ModelAttribute("loginInfo") LoginInfo loginInfo) throws Exception {
List<Category> categories = serviceApiProxyFactory.getCategoryList(loginInfo.getId());
return responseEntityUtil.success(categories);
}
}
@Configuration
@EnableWebMvc
@EnableSwagger2
@ComponentScan(basePackages = "com.naver", useDefaultFilters = false, includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = Controller.class),
@ComponentScan.Filter(type = FilterType.ANNOTATION, value = ControllerAdvice.class)})
@Import({CoreApplicationContextConfiguration.class, SwaggerConfig.class})
public class SpringMvcConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("/static/");
// swagger 설정
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean
public Docket documentation() {
return new Docket(DocumentationType.SWAGGER_2).select().apis(
RequestHandlerSelectors.basePackage("api.class.packagename")).paths(PathSelectors.any()).build().pathMapping(
"/").apiInfo(apiInfo());
}
@Bean
UiConfiguration uiConfig() {
return new UiConfiguration("validatorUrl");
}
private ApiInfo apiInfo() {
return new ApiInfo("Swagger API", "Swagger API Desc", "1.0", "My Apps API terms of service",
"[email protected]", "", "");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment