SpringBoot集成Swagger

本文介绍了如何在SpringBoot项目中集成Swagger2来实现API文档的自动化生成,详细步骤包括添加依赖、编写控制器、配置Swagger,以及解决在测试过程中遇到的空指针异常和404错误。通过调整配置,最终成功运行并访问Swagger UI。
摘要由CSDN通过智能技术生成

1.新建一个SpringBoot-web项目

2.导入相关依赖

swagger2

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

ui

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

3.编写一个hello 工程

package com.ning.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping(value = "/hello")
    public String hello(){
        return "hello";
    }
}

4.配置Swagger --> config

package com.ning.config;

import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2    //开启swagger2
public class SwaggerConfig {
}

5.测试运行

测试运行时,遇到空指针异常

解决方法:在springboot中的配置文件加入:

spring.mvc.pathmatch.matching-strategy=ant_path_matcher

springboot运行成功后,访问swagger-ui.html页面404错误,没有找到页面。将maven版本降低为2.9.2解决问题。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值