SSM三大框架集成Swagger2

SSM三大框架集成Swagger2

  1. 在maven的pom文件中引入依赖
<!--Swagger2-->
    <!--springfox的核心jar包-->
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>2.7.0</version>
    </dependency>
    <!--springfox-ui的jar包-->
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>2.7.0</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.0</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>RELEASE</version>
      <scope>compile</scope>
    </dependency>
  1. 创建一个package,并在里面创建SwaggerConfig.java文件
@WebAppConfiguration
@EnableSwagger2
@EnableWebMvc
@ComponentScan(basePackages = "com.XXX")//扫描control所在的package请修改为你control所在package
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("XXX项目接口文档")
                .description("XXX项目接口测试")
                .version("1.0.0")
                .termsOfServiceUrl("")
                .license("")
                .licenseUrl("")
                .build();
    }
}
  1. 在springMVC的配置文件中配置swagger
<!--swagger-->
    <!--将静态资源交由默认的servlet处理-->
    <mvc:default-servlet-handler />
    <!--向容器自动注入配置-->
    <context:annotation-config />
    <!--自动扫描,使springMVC认为包下用了@controller注解的类是控制器-->
    <context:component-scan base-package="com.XXX"/>
    <!--将你的SwaggerConfig配置类注入-->
    <bean class="XXX.SwaggerConfig"/>
    <!--配置swagger资源不被拦截-->
    <mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
    <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />
  1. 修改web.xml文件中配置所有的请求都经DispatcherServlet处理(可能在之前SSM框架集成的时候已经写过了就无需重复)
<servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
  1. controller的配置
@Controller
@Api(value = "/ClothesType", tags = "ClothesType接口")//ClothesType是我自己取的名字
public class ClothesTypeController {
    @Autowired
    private ClothesTypeService clothesTypeService;
    
    @GetMapping("/getAllClothesTypes")
    @ResponseBody
    @ApiOperation(value = "根据id获取用户信息", notes = "获取信息", httpMethod = "GET") //这个好像可不写 哈哈
    public List<ClothesType> getAllClothesType(){
        List<ClothesType> allClothesType = clothesTypeService.getAllClothesType();
        return allClothesType;
    }
  1. 访问地址(8009是我自己的端口号)

[项目访问地址]/swagger-ui.html

例如:
http://localhost:8009/swagger-ui.html
发布了31 篇原创文章 · 获赞 13 · 访问量 1430

猜你喜欢

转载自blog.csdn.net/weixin_44210965/article/details/90319938
今日推荐