码迷,mamicode.com
首页 > 其他好文 > 详细

Swagger2接口开发

时间:2018-09-19 22:08:05      阅读:460      评论:0      收藏:0      [点我收藏+]

标签:check   set   api   docke   method   required   pos   operation   pack   

Swagger2接口开发

 

    1、先在pom.xml添加

          <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.5.0</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.5.0</version>
		</dependency>

 

    2、配置config

 

@EnableSwagger2
@Configuration
public class SwggerConfig {

    @Bean
    public Docket createMobileAppApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("手机端调用API")
                // .pathMapping("/api")
                .select()
                // 为当前包路径
                .apis(RequestHandlerSelectors
                        .basePackage("com.djb.swagger.control"))
                .paths(PathSelectors.regex("/api/app/.*")).build()
                .apiInfo(appInfo());
    }

    // 构建 api文档的详细信息函数
    private ApiInfo appInfo() {
        return new ApiInfoBuilder()
        // 页面标题
                .title("XX系统-App调用API")
                // 创建人
                .contact(new Contact("lyj", "", "xxxxxxxxx@qq.com"))
                // 版本号
                .version("1.0")
                // 描述
                .description("手机端接口...").build();
    }

    3、controller层

private static final Log log = LogFactory.getLog(AppController.class);

    @ApiOperation(value = "Login")
    @RequestMapping(value = "login", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public RecvAppResponse recvLoginInfo(
            @ApiParam(name = "username", value = "Account", required = true) @RequestParam String username,
            @ApiParam(name = "password", value = "password", required = true) @RequestParam String password) {
        RecvAppResponse recvAppResponse = new RecvAppResponse();
        ErrorObject error = new ErrorObject();
        error = appService.checkLogin(username, password);
        if (!error.isSuccess()) {
            recvAppResponse.setError(error);
            recvAppResponse.setSuccess(false);
            return recvAppResponse;
        }
        recvAppResponse.setUserToken(error.getMsg());
        error.setMsg("success!");
        recvAppResponse.setError(error);
        recvAppResponse.setSuccess(true);
        return recvAppResponse;
    }

    4、登录  http://localhost:9090/api

技术分享图片

    5、测试

      swagger方便直接测试

技术分享图片

 

Swagger2接口开发

标签:check   set   api   docke   method   required   pos   operation   pack   

原文地址:https://www.cnblogs.com/lrj1009IRET/p/9641754.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!