SpringBoot整合swagger生成在线文档步骤,及404的解决方法

一、引入依赖

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

二、添加swagger配置

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.test.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2构建RESTful APIs")
                .description("更多请关注http://www.baidu.com")
                .termsOfServiceUrl("http://www.baidu.com")
                .version("1.0")
                .build();
    }
}

在SpringBoot启动类上添加注释@EnableSwagger2

@SpringBootApplication
@EnableSwagger2
@MapperScan(basePackages = {"com.djej.mapper"})
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

三、编写swagger在线接口文档示例

@Controller
@RequestMapping("/swaggerController")
@Api(description = "swagger在线文档")
public class SwaggerController {

    @GetMapping("/getData")
    @ResponseBody
    @ApiOperation(value = "查询数据",notes = "根据id查询数据")
    public Object getData(@ApiParam("用户id") Integer id){
        return null;
    }

    @PostMapping("/insertData")
    @ResponseBody
    @ApiOperation(value = "保存数据",notes = "保存用户信息")
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功"),
            @ApiResponse(code = 1001, message = "失败"),
            @ApiResponse(code = 1002, message = "缺少参数") })
    public Object insertData(@RequestBody String params){
        return null;  
    }

}

 增删改查示例

在形参前添加@RequestBody 或者什么都不加则默认接收的是paramter。

在形参前添加@RequestBody 接收的是json。

@RestController
@RequestMapping("/userController")
@Api(description = "User增删改查")
public class UserController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Resource
    private IUserService userService;


    @RequestMapping(value = "/user/{page}/{rows}/{param}",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询数据",notes = "若param为空查询所有用户,若param不为空则根据传入内容进行查询")
    public ResponseData selectAll(@PathVariable("page") @ApiParam(value = "第几页",required = true) Integer page,
                                  @PathVariable("rows") @ApiParam(value = "每页多少行", required = true)Integer pageSize,
                                  @PathVariable(value = "param",required = false) @ApiParam(value = "要查询的信息",required = false)String param) {
        System.out.println("page:" + page);
        System.out.println("rows:" + pageSize);
        System.out.println("param:" + param);

        ResponseData responseData = userService.selectAll(page, pageSize,param);
        return responseData;
    }

    @RequestMapping(value = "user",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "插入数据",notes = "将User数据插入数据库中")
    public ResponseResult insertSelective(@RequestBody @ApiParam(value = "要插入的用户信息",required = true) User user) {
        System.out.println(user);
        int i = userService.insertSelective(user);
        return ResponseResult.success(i);
    }


    @RequestMapping(value = "user",method = RequestMethod.PUT)
    @ResponseBody
    @ApiOperation(value = "更新数据",notes = "更新User数据到数据库中")
    public ResponseResult updateByPrimaryKeySelective(@RequestBody @ApiParam(value = "要更新的用户信息",required = true)User record) {
        int i = userService.updateByPrimaryKeySelective(record);
        return ResponseResult.success(i);
    }


    @RequestMapping(value = "user",method = RequestMethod.DELETE)
    @ResponseBody
    @ApiOperation(value = "删除数据",notes = "根据用户ID删除数据")
    public ResponseResult deleteByPrimaryKey(@RequestParam @ApiParam(value = "要删除的用户ID",required = true)Integer id) {
        int i = userService.deleteByPrimaryKey(id);
        return ResponseResult.success(i);
    }


    @RequestMapping(value = "test2",method = {RequestMethod.GET})
    @ResponseBody
    public String test2() {
        return userService.test2();
    }
}

 

四、访问http://localhost:端口/项目名/swagger-ui.html

 

 

一般来说,到这里就完成了,但是我出现了404错误。

经过百度和各种尝试后无果,后发现是maven仓库里的jar包有问题,重新下载后就能访问了

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值