...... 최근 회사의 프로젝트는 겉보기에는 접촉, 인터페이스 테스트 도구의 우편 배달부가 시작하기 전에하는 API 인터페이스 테스트 도구 자신감을 사용, 그래서 나는 빨리 여기 잡기
1. 필요한 추가 Swagger2을 따라 달라집니다
<! - 依赖Swagger2 부팅添加봄 -> < 의존성 > < 의 groupId > io.springfox </ 의 groupId > < artifactId를 > springfox - 자신감 - UI </ artifactId를 > </ 의존성 > < 의존성 > < 의 groupId > io.springfox </ 의 groupId > < artifactId를 > springfox-swagger2 </ artifactId를 > </ 의존성 >
2. 프로젝트 SwaggerConfig 카테고리를 추가 :
패키지 com.xianwen.sp.springjtool.swagger;
수입 org.springframework.context.annotation.Bean;
수입 org.springframework.context.annotation.Configuration;
수입 io.swagger.annotations.ApiOperation;
수입 springfox.documentation.builders.ApiInfoBuilder;
수입 springfox.documentation.builders.PathSelectors;
수입 springfox.documentation.builders.RequestHandlerSelectors;
수입 springfox.documentation.service.ApiInfo;
수입 springfox.documentation.service.Contact;
수입 springfox.documentation.spi.DocumentationType;
수입 springfox.documentation.spring.web.plugins.Docket;
수입 springfox.documentation.swagger2.annotations.EnableSwagger2;
/ **
* @author : zhoux
*
* 구성 Swagger2를 들어이 프로젝트.
*
* /
@Configuration
@ EnableSwagger2
SwaggerConfig가 {공용 클래스
@Bean
공공 문서 번호 creatRestApi는 () {
반환 새 새 문서 번호 (DocumentationType.SWAGGER_2)
.groupName ( "봄 jtool")
.apiInfo (getApiInfo ())
ALL 기타 사항 서보 -OFF ()
// 레이블 @Api basePackage의 모든 클래스의 모든 메소드는 패키지의 API로 제공됩니다
.apis (RequestHandlerSelectors.basePackage ( "com.xianwen.sp.springjtool.controller"))
//에만 태그 @ ApiOperation 방법 자신감에 노출되는
.apis (RequestHandlerSelectors.withMethodAnnotation (ApiOperation.class))
.paths (PathSelectors.any ()) // PathSelectors.regex ( "/ API /.*")
.build ();
}
개인 ApiInfo getApiInfo () {
새로운 ApiInfoBuilder를 반환 ()
.title ( "API 문서")
.description ( "Swagger2의 API 문서 ")
//.contact(new 연락 ("zhoux ","HTTP : //localhost/swagger-ui.html ","[email protected] "))
.version를 ("1.0.0 ")
.build을 () ;
}
}
제어기 @Api 3. (태그 = "****") 특수 태그는 인터페이스 메소드 @ApiOperation ( 「*****」) 특수 태그를 사용하여 시험한다 :
패키지 com.xianwen.sp.springjtool.controller; 수입은 java.util.List; 수입 org.springframework.beans.factory.annotation.Autowired; 수입 org.springframework.http.MediaType; 수입 org.springframework.web.bind.annotation.GetMapping; 수입 org.springframework.web.bind.annotation.PathVariable; 수입 org.springframework.web.bind.annotation.PostMapping; 수입 org.springframework.web.bind.annotation.RequestMapping; 수입 org.springframework.web.bind.annotation.RestController; 수입 com.xianwen.sp.springjtool.entity.User; 수입 com.xianwen.sp.springjtool.service.impl.UserServiceImpl; 수입 io.swagger.annotations.Api; 수입 io.swagger.annotations.ApiOperation; / ** * @ClassName : UserController의 *의 @description : 사용자를위한 컨트롤러. * * @author : zhoux * * @date : 2019년 9월 10일 10시 10분 59초 * * / @Api (설명 = "用户管理-用户数据") @RestController @RequestMapping ( "/ 사용자") 공용 클래스 UserController { @Autowired 개인 UserServiceImpl mUserServiceImpl; @ApiOperation ( "获取所有的用户信息") @PostMapping (값 = "/ findUser가"= MediaType.APPLICATION_JSON_UTF8_VALUE는 = MediaType.APPLICATION_JSON_UTF8_VALUE 소비 생산하고) 공개 목록 < 사용자 > findAllUser을 () { mUserServiceImpl을 반환합니다. } @GetMapping (값 = "/ {ID}"생산 = MediaType.APPLICATION_JSON_UTF8_VALUE) 공개 목록 < 사용자 > findAllById (@PathVariable ( "ID") 롱 ID) { mUserServiceImpl.findById (ID)를 반환; } @ApiOperation ( "根据이름获取用户信息") @GetMapping가 (값 = "/ {이름}"= 생산 MediaType.APPLICATION_JSON_UTF8_VALUE) 공개 목록 < 사용자 > findAllByName (@PathVariable ( "이름") 문자열 이름) { mUserServiceImpl를 반환 .findByName (이름); } }