本周学习
本周开始写项目,前端后端的第一次交互,说实话看着有点懵,前后端交互的第一步swagger我们一直在尝试,结果是一直报错,各种解决方法都找了一边还是不行,说一下我们配置swagger环境的基本步骤吧,大家看一下问题出现在哪里.
第一步
maven引入相关jar包
<!-- swagger -->
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
<version>2.10.2<version>
</dependency>
第二步
建立swagger的相关配置
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2 //Loads the spring beans required by the framework
public class MySwaggerConfig {
@Bean
public Docket userApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select() // 选择那些路径和api会生成document
.apis(RequestHandlerSelectors.any()) // 对所有api进行监控
.paths(PathSelectors.any()) // 对所有路径进行监�?
.build();
}
@Bean
public RequestMappingInfoHandlerMapping requestMapping(){
return new RequestMappingHandlerMapping();
}
}
第三步
springMVC引入相关swagger的配置
<bean class="springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration" id="swagger2Config"/>
第四步
在applicationContext.xml进行配置
<!-- 引入swagger相关 -->
<bean class="com.test.document.MySwaggerConfig"/>
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" >
第五步
在controller配置方法
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.wordnik.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
@Controller
@RequestMapping(value="/TestExcelFast")
@Api(value = "restful", description = "测试")
public class TestExcelFastController {undefined
@ApiOperation(value = "测试专用")
@RequestMapping(value="/test",method=RequestMethod.GET)
@ResponseBody
public String test(){undefined
String str = "123";
System.out.println(1);
return str;
}
}
第六步
运行tomat 启动项目
http://localhost:8080/项目名称/swagger-ui.html
大致就是这样子来进行配置,但是我们一直出现报错,我们进行的是ssm框架整合,网上搜的几乎都是关于spring-boot的报错解决.