springboot+swagger2

http://start.spring.io/生成springboot工程



引入maven依赖

<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.7.0</version>
</dependency>
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>2.7.0</version>
</dependency>
加载swagger2配置

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Autowired
    Environment env;
    @Bean
    public Docket createRestApi(ApiInfo apiInfo) {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage(env.getProperty("swagger.scan")))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(env.getProperty("swagger.title"))
                .description(env.getProperty("swagger.description"))
                .version(env.getProperty("swagger.version"))
                .build();
    }
}
application.yml

spring:
  application:
    name: swagger-test
server:
  port: 8080
  context-path: /api
swagger:
  scan: com.swagger.test
  title: 用户service API
  description: 用户service API
  version: 1.0.0
controller

@RestController
@RequestMapping("/user")
@Api(value="用户controller",description="用户操作",tags={"用户操作接口"})
public class UserController {
    @GetMapping("/get")
    @ApiOperation(value = "获取用户接口",notes = "获取用户接口notes",response = String.class)
    public String get(@ApiParam(name = "userName",value = "用户名",required = true) @RequestParam("userName") String userName){

        return "返回值:"+userName;
    }
    @PostMapping("/post")
    @ApiOperation(value = "获取用户接口Post",notes = "获取用户接口notes Post",response = UserForm.class)
    public UserForm post(@RequestBody @ApiParam(name="用户对象",value="传入json格式",required=true)  UserForm userForm){
        return userForm;
    }
}

form

@ApiModel(description = "用户from")
public class UserForm {
    @ApiModelProperty(value = "用户名",name = "userName",required = true)
    private String userName;

    @Override
    public String toString() {
        return "UserForm{" +
                "userName='" + userName + '\'' +
                '}';
    }

    /**
     * Getter method for property <tt>userName</tt>
     *
     * @return property value of userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * Setter method for property <tt>userName</tt>.
     *
     * @param userName value to be assigned to property userName
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }
}

启动 访问:http://localhost:8080/api/swagger-ui.html


例子下载地址:http://download.csdn.net/download/zyj_2012/10191998




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值