<aside> ✨ Storelink 개발팀은 이제 Swagger를 사용할 수 있습니다
</aside>
<aside> 💡 초기 세팅 후 기본 경로 JSON 응답 : http://localhost:8080/v2/api-docs Swagger UI : http://localhost:8080/swagger-ui.html
</aside>
<aside> 💡 storelink auth header를 세팅해야 테스트할 수 있습니다. local, dev 환경에서만 Bean이 등록되어 동작합니다.
</aside>
// 매 요청 시 header 세팅
@Configuration
@EnableSwagger2
@Profile({"local","dev"})
public class SwaggerConfig {
@Bean
public Docket api() {
ParameterBuilder aParameterBuilder = new ParameterBuilder();
aParameterBuilder.name("*auth-header-name*") //헤더 이름
.description("*auth-header*") //설명
.modelRef(new ModelRef("string"))
.parameterType("header")
.required(false)
.build();
List<Parameter> aParameters = new ArrayList<>();
aParameters.add(aParameterBuilder.build());
return (new Docket(DocumentationType.SWAGGER_2)
.globalOperationParameters(**aParameters**)
.apiInfo(getApiInfo())
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build());
}
...
---
// 최초 일괄 세팅 후 모두 적용
.build()
.securitySchemes(Arrays.asList(apiKey())));
private ApiKey **apiKey**() {
return new ApiKey("*KEY*", "header-name", "header");
}
일반적인 GET method
**@GetMapping**
public ModelAndView getBannerListForAdmin() {
ModelAndView mav = new ModelAndView("jsonView");
String resultCode = "0000";
String resultMsg = "";
List<Banner> result = tempService.getTempListAdmin();
mav.addObject("result_code", resultCode);
mav.addObject("result_msg", resultMsg);
mav.addObject("data", result);
return mav;
}
Swagger를 적용 한 Controller
**@Api(value**="메인 이벤트 및 소개영상 관련 컨트롤러")
@RestController
@RequiredArgsConstructor
@RequestMapping("/Temp")
public class TempController extends CommonHeaderController{
}
---
// 전반적인 설명과 필요 조건 명시
**@ApiOperation(**
value="getId",
notes = "temp 테이블 전체조회",
httpMethod="GET",
produces ="application/json",
consumes ="application/json",
protocols="http",
responseHeaders = {
//Headers..
})
---
// 반환 코드 message 설정
**@ApiResponses**({
@ApiResponse(code = 200, message = "성공했을 때 상태값"),
@ApiResponse(code = 404, message = "페이지를 찾을 수 없을 때 상태"),
@ApiResponse(code = 401, message = "Security를 통과하지 못했습니다.")
})
입력 파라미터 정의 - method 상단에 직접 정의
// 해당 method 파라미터만 정의
@ApiImplicitParams({
@ApiImplicitParam(name = "title", value = "제목", required = true, dataType = "string", paramType = "query", defaultValue = ""),
@ApiImplicitParam(name = "temp_type_cd", value = "유형", required = true, dataType = "string", paramType = "query", defaultValue = ""),
@ApiImplicitParam(name = "temp_image", value = "이미지", required = true, dataType = "string", paramType = "query", defaultValue = ""),
@ApiImplicitParam(name = "temp_mobile_image", value = "모바일 사이즈 이미지", required = true, dataType = "string", paramType = "query", defaultValue = ""),
})
입력 파라미터 정의 - Model 일괄 정의