springmvc+swagger2在线文档生成(maven项目)

1.导入jar包
需要导入springfox-swagger2和springfox-swagger-ui


    io.springfox
    springfox-swagger2
    2.7.0




    io.springfox
    springfox-swagger-ui
    2.7.0

相关jar包

org.codehaus.jackson
jackson-mapper-asl
1.9.13

    
        com.fasterxml.jackson.core    
        jackson-core    
        2.8.0    
   
    
        
        com.fasterxml.jackson.core    
        jackson-databind    
        2.8.0    
   

   
    com.alibaba
    fastjson
    1.2.40
 
2.配置基类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;


import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


/**
 * @author xiegx
 * @version 创建时间:2016-8-16 下午2:01:10
 * SwaggerUI配置
 */
@Configuration
@EnableSwagger2
@EnableWebMvc
@ComponentScan(basePackages ={"com.era.controller"})  
public class SwaggerConfig  extends WebMvcConfigurerAdapter {  


@Bean  
    public Docket createRestApi() {  
        return new Docket(DocumentationType.SWAGGER_2)  
                .apiInfo(apiInfo())  
                .select()  
                .apis(RequestHandlerSelectors.basePackage("com.era.controller"))  
                .paths(PathSelectors.any())  
                .build();  
    }
private ApiInfo apiInfo() {  
        return new ApiInfoBuilder()  
                .title("永高项目")  
                .description("永高项目后台接口文档")  
                .termsOfServiceUrl("http://honghu.com")  
                .contact(new Contact("", "", "HongHu"))  
                .version("1.1.0")  
                .build();  
    }

}
然后再springmvc的配置文件中配置

 
3.从官网或者github上下载对应的swagger-ui压缩包
将从官网下载的包解压缩然后放到webapp下文件夹命名为swagger
4.在web.xml中配置静态文件的路径

    default
    /swagger/*
 

5.在controller中加入相应的注解用来进行测试


@Controller
@Api(value="用户管理")
public class UserController {

@Autowired
private UserBiz userBiz;

/*
* @ApiOperation(value = "接口说明", httpMethod ="接口请求方式", response ="接口返回参数类型", notes ="接口发布说明"

* @ApiParam(required = "是否必须参数", name ="参数名称", value ="参数具体描述"
*/

//获取所有的用户类型
@ResponseBody
@RequestMapping(value="/getAllUserType.do",method=RequestMethod.POST)
@ApiOperation(value="接口说明(测试)",httpMethod="POST",notes="在没有会话、没有签名的情况下,进入方法体",produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
public Response getAllUserType(){
Response response=userBiz.getAllUserType();
return response;
}
}
6.修改swagger文件夹下的dist/index.html文件修改url为http://localhost:8080/ERAPIPE/v2/api-docs

你可能感兴趣的:(spring,springMvc)