SSM三大框架集成Swagger2

SSM三大框架集成Swagger2

  1. 在maven的pom文件中引入依赖

    
    <dependency>
      <groupId>io.springfoxgroupId>
      <artifactId>springfox-swagger2artifactId>
      <version>2.7.0version>
    dependency>
    
    <dependency>
      <groupId>io.springfoxgroupId>
      <artifactId>springfox-swagger-uiartifactId>
      <version>2.7.0version>
    dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-databindartifactId>
      <version>2.9.0version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-testartifactId>
      <version>RELEASEversion>
      <scope>compilescope>
    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

    
    <mvc:default-servlet-handler />
    
    <context:annotation-config />
    
    <context:component-scan base-package="com.XXX"/>
    
    <bean class="XXX.SwaggerConfig"/>
    
    <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>SpringMVCservlet-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

你可能感兴趣的:(util,#,Spring,架构新技术)