Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring Boot 集成swagger实例 #5

Open
x113773 opened this issue Jun 20, 2017 · 1 comment
Open

Spring Boot 集成swagger实例 #5

x113773 opened this issue Jun 20, 2017 · 1 comment
Labels

Comments

@x113773
Copy link
Owner

x113773 commented Jun 20, 2017

  1. 首先添加maven依赖
                <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>
  1. 编写swagger配置SwaggerConfig.java
@Configuration
@EnableSwagger2
public class SwaggerConfig {

	 @Bean
	    public Docket createRestApi() {
		 return new Docket(DocumentationType.SWAGGER_2)
			     .pathMapping("/")// base,最终调用接口后会和paths拼接在一起
			     .select()
			     .paths(or(regex("/api/.*")))//过滤的接口
                 .build()
                 .apiInfo(testApiInfo());
	    }

	 private ApiInfo testApiInfo() {
	        return new ApiInfoBuilder()
	            .title("大标题大标题大标题")//大标题
	            .description("详细描述详细描述")//详细描述
	            .version("1.0版本版本")//版本
	            .termsOfServiceUrl("NO terms of service")
	            .contact("/作者/作者/作者 ")//作者
	            .license("The Apache License, Version 2.0")
	            .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
	            .build();
	    }

}
  1. 测试用的控制器SwaggerController.java
@RestController
public class SwaggerController {

	
	@ApiOperation(value = "获取用户详细信息", notes = "根据url的id来获取用户详细信息")
	@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
	@RequestMapping(value = "/api/{id}", method = RequestMethod.GET)
	public Error400 getUser(@PathVariable Long id) {
		Error400 er = new Error400();
		return er;
	}

	@ApiOperation(value = "创建用户", notes = "根据User对象创建用户")
	@ApiResponses({ @ApiResponse(code = 400, message = "请求参数没填好"), @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对", response = Error400.class) })
	@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
	@RequestMapping(value = "/api/user", method = RequestMethod.POST)
	public String postUser(@RequestBody User user) {
		return "success";
	}
	
}
  1. 实体类Error400.javaUser.java
@ApiModel(value = "400Error")
public class Error400 {
	/**
	 * 错误反馈编码
	 */
	@ApiModelProperty(value = "错误反馈编码")
	private String respCode;

	/**
	 * 错误反馈描述
	 */
	@ApiModelProperty(value = "错误反馈描述")
	private String respDesc;

	 //省略get set

}
@x113773
Copy link
Owner Author

x113773 commented Jun 20, 2017

启动项目,访问http://localhost:8080/swagger-ui.html

qq 20170620135750

@x113773 x113773 changed the title Spring Boot 集成swagger示例 Spring Boot 集成swagger实例 Jun 20, 2017
@x113773 x113773 added the doc label Jun 29, 2017
@x113773 x113773 added doc and removed doc labels Sep 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant