赞
踩
附上swagger注解解析
普通SSM项目搭建
导入jar包
添加到 dependencies标签内
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger2</artifactId>
- <version>2.8.0</version>
- </dependency>
- <!--springfox-ui的jar包(里面包含了swagger的界面静态文件)-->
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger-ui</artifactId>
- <version>2.7.0</version>
- </dependency>
- <!--springfox依赖的jar包;如果你的项目中已经集成了无需重复-->
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>2.9.0</version>
- </dependency>
创建swaggerConfig.java文件
ps:我是直接扔到controller文件夹内的,也可以单独一个文件夹存放
- package com.bdqn.controller;
-
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.test.context.web.WebAppConfiguration;
- import org.springframework.web.servlet.config.annotation.EnableWebMvc;
- import springfox.documentation.builders.ApiInfoBuilder;
- import springfox.documentation.builders.RequestHandlerSelectors;
- import springfox.documentation.service.ApiInfo;
- import springfox.documentation.spi.DocumentationType;
- import springfox.documentation.spring.web.plugins.Docket;
- import springfox.documentation.swagger2.annotations.EnableSwagger2;
-
- /*重要!如果你的项目引入junit测试(jar),此处需要使用@WebAppConfiguration,
- 如果没有使用junit使用@Configuration(友情转载~博客:jinqianwang)*/
- @EnableSwagger2
- @WebAppConfiguration
- @EnableWebMvc
- /*扫描controller层,监听~*/
- @ComponentScan("com.bdqn.controller")
- 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();
- }
- }
追加内容到spring-mvc.xml
- </mvc:annotation-driven>
- <!--将静态资源交由默认的servlet处理-->
- <mvc:default-servlet-handler />
- <!--向容器自动注入配置-->
- <context:annotation-config />
- <!--重要!将你的SwaggerConfig配置类注入-->
- <bean class="com.bdqn.controller.SwaggerConfig"/>
- <!--重要!配置swagger资源不被拦截-->
- <!-- <mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
- <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />-->
controller层
- package com.bdqn.controller;
-
- import com.alibaba.fastjson.JSONArray;
- import com.bdqn.service.GoodsService;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiOperation;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RestController;
-
- import javax.annotation.Resource;
-
- /*tags是进去swgger-ui网页显示的controller名字,description是这个controller的描述*/
- @Api(tags = "这是商品的API",description = "这是商品类的controller")
- @RestController
- public class GoodsController {
- @Resource
- private GoodsService goodsService;
-
- public GoodsService getGoodsService() {
- return goodsService;
- }
-
- public void setGoodsService(GoodsService goodsService) {
- this.goodsService = goodsService;
- }
-
- /*顺序不能反,必须先requestmapping*/
- @RequestMapping(value = "/goods",method = RequestMethod.GET)
- /*value是该请求的说明,httpMethod显示提交方式,notes是详细说明*/
- @ApiOperation(value = "直接获取所有商品信息",httpMethod="get",notes="获取商品信息")
- public String getDate(){
- return JSONArray.toJSONString(goodsService.findAll());
- }
- }
demo结构图
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。