[스프링부트] swagger 의존성 없이 사용하기

2022. 10. 17. 17:50스프링

반응형

사용자가 yml파일을 따로 작성하여 스웨거가 작성된 yml를 불러올수 있도록 할수 있다.

설정법은 다음과 같다.

 

controller

@Controller
@RequestMapping("/swagger")
public class SwaggerController
{
	private static String rootPath = "classpath:resources";
	private static String indexPath = "/index.html";

	@Autowired
	private ResourceLoader resourceLoader;

	@GetMapping("/{yml}")
	public void getApiDocs
	(
		HttpServletResponse res,
		@PathVariable("yml") String yml
	)
	throws IOException {
		ModelAndView modelAndView = new ModelAndView();

		File file = resourceLoader.getResource(rootPath + "/" + yml + ".yml").getFile();
		res.setContentLength((int) file.length());
		FileCopyUtils.copy(new FileInputStream(file), res.getOutputStream());
	}


	@GetMapping("/custom/index.html")
	public void getIndex
	(HttpServletResponse res)throws IOException {
		File file = resourceLoader.getResource(rootPath + indexPath).getFile();
		res.setContentLength((int) file.length());
		FileCopyUtils.copy(new FileInputStream(file), res.getOutputStream());
	}


}

 

resources

리소스 경로에 아래와 같이 디렉토리와 파일 구성
resources 
   api
       sample.yml
   index.html

 

index.html 와 sample.yml은 첨부파일 참조

 

index.html
0.00MB
sample.yml
0.02MB

 

반응형

'스프링' 카테고리의 다른 글

[스프링부트] swagger 설정하기  (0) 2022.10.17
[스프링] ibatis에 mariaDB연동  (0) 2019.10.18