swagger配置

1、pox.xml

在pox.xml中添加内容:

 <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>2.6.1</version>
    </dependency>
    <!-- swagger-ui 为项目提供api展示及测试的界面 -->
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>2.6.1</version>
    </dependency>
    <!-- 集成 swagger 的时候,缺少这个 jar3.包是不OK的-->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>${jackson.versioin}</version>
        <exclusions>
            <exclusion>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>${jackson.versioin}</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>${jackson.versioin}</version>
    </dependency>

2、web.xml

在web.xml中加入以下内容:

 <servlet>
        <servlet-name>dispacher-servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>

    </servlet>

    <servlet-mapping>
        <servlet-name>dispacher-servlet</servlet-name>
        <url-pattern>/swagger/*</url-pattern>
    </servlet-mapping>

3、spring-mvc.xml

在spring-mvc.xml中加入(我们的程序代码位于com.nova下面):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.2.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    <!-- 注解扫描包 -->
    <context:component-scan base-package="com.nova.*" />

    <!-- 开启mvc注解 -->
    <mvc:annotation-driven ></mvc:annotation-driven>
    <context:annotation-config></context:annotation-config>
    <mvc:default-servlet-handler/>

    <!-- 静态资源过滤 -->

    <mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
    <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>

    <!-- JSP视图解析器-->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
        <!--  prefix和suffix:查找视图页面的前缀和后缀(前缀[逻辑视图名]后缀),
        	   比如传进来的逻辑视图名为hello,则该该jsp视图页面应该存放在“WEB-INF/views/hello.jsp” -->
    </bean>



    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>

</beans>

4、添加swagger配置文件

package com.nova.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
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;

/**
 * Created by SQL on 2019-01-07.
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {


    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .apiInfo(MyApiInfo());
    }

    private ApiInfo MyApiInfo(){



        return new ApiInfoBuilder()
                .title("xxxx对外开放接口文档")     //大标题
                .description("HTTP对外接口")      //小标题
                .version("0.0.1")                //版本
                .termsOfServiceUrl("http://www.xxxx.com")
                .license("LINCENSE")            
                .licenseUrl("http://xxx.xxx.com")
                .build();
    }
}

5、添加接口

package com.nova.controller;

import com.nova.model.Product;
import io.swagger.annotations.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Created by SQL on 2019-01-04.
 */
@RestController
@RequestMapping(value = {"/api"})
@Api(value = "/api",tags = "api")
@EnableSwagger2
public class ProductController {


    @ApiOperation(value = "获取Name1",notes = "这就是一个获取name的测试")
    @RequestMapping(value = "/GetByName/{name}",method = RequestMethod.GET)
    //@ApiImplicitParams(@ApiImplicitParam(name = "name",value = "学生的名称",required = true,dataType = "String"))
    public ResponseEntity<String> getName( @PathVariable("name") String name )
    {
        return ResponseEntity.ok( name + " SQL");
    }

    @ApiOperation(value = "测试下拉框")
    @RequestMapping(value = "/TestSelect/{type}",method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type",value = "类型",dataType = "String",paramType = "query", allowableValues = ",shi,zhang",required = true)
    })
    public ResponseEntity<String> SelectTest(String type){
        return ResponseEntity.ok("Test "+type);
    }

}

6、整个项目

7、配置tomcat,并启动

 

 

8、访问http://localhost/swagger/swagger-ui.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Swagger是一个开源的规范和工具集,用于设计、构建、文档化和使用RESTful Web服务。使用Swagger可以轻松地创建和维护API文档,并为开发人员、测试人员和其他项目相关人员提供一个统一的界面来查看和测试API。 对于后端开发人员来说,Swagger可以帮助他们更好地设计和构建API,并提供自动生成的API文档。 对于前端开发人员来说,Swagger提供了一个可视化界面,方便他们快速了解和使用后端提供的API接口。 对于测试人员来说,Swagger提供了一个集成测试的平台,可以直接在Swagger界面上对API进行测试和验证。 要配置Swagger,首先需要引入Swagger的依赖库。推荐使用2.7.0版本,因为2.6.0版本存在一些bug。可以在项目的pom.xml文件中添加以下依赖: ``` <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency> ``` 然后,在Spring Boot项目中整合Swagger,可以创建一个SwaggerConfig配置类,使用`@EnableSwagger2`注解启用Swagger,并创建一个`Docket` bean来配置Swagger的一些基本信息,如API文档的标题、描述和版本等。可以根据需要添加其他的配置,如选择扫描哪些接口、路径等。 在项目中集成Swagger的具体步骤如下: 1. 引入Swagger的依赖库; 2. 创建一个SwaggerConfig配置类,使用`@EnableSwagger2`注解启用Swagger; 3. 创建一个`Docket` bean来配置Swagger的基本信息; 4. 在controller中使用Swagger的注解来标记API接口; 5. 访问本地链接即可查看和使用Swagger的界面。 在使用Swagger过程中,需要注意以下几个问题: 1. 对于只有一个HttpServletRequest参数的方法,推荐使用`@ApiImplicitParams`注解方式单独封装每一个参数; 2. 默认的访问地址是`ip:port/swagger-ui.html#`,但是在Shiro中,会拦截所有请求,所以需要加上默认访问路径,如`ip:port/context/swagger-ui.html#`,并登录后才能查看; 3. 在GET请求中,参数在Body体里面,不能使用`@RequestBody`注解;在POST请求中,可以使用`@RequestBody`和`@RequestParam`注解; 4. 如果使用`@RequestBody`注解,在controller中必须统一指定请求类型,否则Swagger会生成所有类型的参数; 5. 在生产环境中,不应该对外暴露Swagger界面,可以使用`@Profile`注解指定只在开发、测试和预发布环境中使用。 综上所述,Swagger是一个功能强大的工具,可以帮助开发人员更好地设计、构建和文档化API,提高开发效率和API的可用性。通过合理配置和使用Swagger,可以更好地管理和使用API接口。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Swagger基本配置](https://blog.csdn.net/qq_40099908/article/details/131102237)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值