撸了今年阿里、头条和美团的面试,我有一个重要发现.......>>>
本文永久更新地址:https://my.oschina.net/bysu/blog/3092623
推荐一款免费的自购省钱,分享赚钱的平台——赚赚熊,感兴趣的,特别是家里有婆娘专职带娃的,请看文末。
1.在pom.xml文件中加入
<properties>
<swagger.version>2.6.1</swagger.version>
</properties>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
2.创建一个cmd包,在cmd中创建Application类,如下:
package cmd;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("video.aigo.server")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
3.创建video.aigo.config包,在config中创建SwaggerConfig类,如下:
package video.aigo.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.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;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).pathMapping("/").select().paths(PathSelectors.regex("/.*")).build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder().title("我的接口文档")
.contact(new Contact("bysu","","by.su@qq.com"))
.description("这是我的swaggerui生成的接口文档").version("1.0.0")
.build();
}
}
4.运行Application后,在浏览器地址栏输入http://localhost:8080/swagger-ui.html 如下图为集成成功
如何使用swaggerui?1.创建包video.aigo.server,在server中创建MyGetMethod类,代码如下:
package video.aigo.server;
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.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
@RestController
@Api(value = "/",description = "这是我全部的get方法")
public class MyGetMethod {
@RequestMapping(value = "/getCookies",method = RequestMethod.GET)
@ApiOperation(value = "通过这个get方法可以获取到cookies的值",httpMethod = "GET")
public String getCookies(HttpServletResponse response){
Cookie cookie = new Cookie("login","true");
response.addCookie(cookie);
return "恭喜获取cookies成功";
}
}
2.把上面集成的步骤2中@ComponentScan("video.aigo.server")改成@ComponentScan("video.aigo");
3.运行Application后,在浏览器地址栏输入http://localhost:8080/swagger-ui.html
4.在包video.aigo.server中新增MyPostMethod类,代码如下:
package video.aigo.server;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
@RestController
@Api(value = "/",description = "这是我全部的post方法")
@RequestMapping(value = "v1")
public class MyPostMethod {
private static Cookie cookie;
@RequestMapping(value = "/login",method = RequestMethod.POST)
@ApiOperation(value = "登录接口,成功后获取cookies信息",httpMethod = "POST")
public String login(HttpServletResponse response,
@RequestParam(value = "userName",required = true) String userName,
@RequestParam(value = "passwd",required = true) String passwd){
if (userName.equals("bysu") && passwd.equals("666666")){
cookie = new Cookie("login","true");
response.addCookie(cookie);
return "恭喜你登录成功";
}
return "用户名或密码错误";
}
}
5.重启Application后,刷新页面,结果如下图,发现红框中与上图不一样。在userName和passwd中输入用户名和密码,点击try it out,会对账户名密码进行校验。
最后:整个project的目录如下
----------------------下方为免费的广告---------------------
1.识别下图中的二维码,跳到步骤2的界面;
2.填入手机,获取验证码,填入验证码注册,界面跳转至步骤3:
3.点开右上角的“...”,界面跳转至步骤4:
4.选择在系统浏览器打开后,界面跳转至步骤5:
5.根据你手机的系统,选择下载的版本。
6.安装成功后,登录app,选择授权淘宝。授权后,在淘宝购买东西大多数都有返利,后续还会支持京东,唯品会...各大平台。
赚赚熊有问题可以咨询