package com.madhouse.config;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import static springfox.documentation.builders.PathSelectors.regex;
import static com.google.common.base.Predicates.or;
import com.fasterxml.classmate.TypeResolver;
import com.google.common.base.Predicate;
import com.madhouse.controller.MyController;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.AlternateTypeRule;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.schema.WildcardType;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.ApiKeyVehicle;
import springfox.documentation.swagger.web.SecurityConfiguration;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableWebMvc // NOTE: Only needed in a non-springboot application
@EnableSwagger2
@ComponentScan(basePackageClasses = { MyController.class })
@Component
public class MySpringConfig {
@Bean
public Docket petApi() {
return new Docket(DocumentationType.SWAGGER_2).select() // 生成Docket的选择器对API进行筛选
.apis(RequestHandlerSelectors.any()) //针对controller进行筛选
.paths(PathSelectors.any()) //针对路径进行筛选
.build()
.pathMapping("/") // API的根目录
.directModelSubstitute(Date.class, String.class)// 输出模型的替换(把如期以字符串的形式输出)
.genericModelSubstitutes(ResponseEntity.class) //针对泛型的处理(指定其泛型是哪种类型)
.alternateTypeRules(new AlternateTypeRule(
typeResolver.resolve(DeferredResult.class,
typeResolver.resolve(ResponseEntity.class, WildcardType.class)),
typeResolver.resolve(WildcardType.class)))
.useDefaultResponseMessages(false) //对于不同的http请求会产生不同的返回值
.globalResponseMessage(RequestMethod.GET,
newArrayList(new ResponseMessageBuilder().code(500).message("500 message")
.responseModel(new ModelRef("Error")).build()))//定义对于错误信息的的消息和错误的模型
.securitySchemes(newArrayList(apiKey()))//认证请求中的header
.securityContexts(newArrayList(securityContext()))//上下文路径的认证
.enableUrlTemplating(true);//url的模板路径
}
private <T> List<T> newArrayList(T t) {
List<T> list = new ArrayList<T>();
list.add(t);
return list;
}
@Autowired
private TypeResolver typeResolver;
private ApiKey apiKey() {
return new ApiKey("mykey", "api_key", "header");
}
private SecurityContext securityContext() {
return SecurityContext.builder().securityReferences(defaultAuth()).forPaths(PathSelectors.regex("/anyPath.*"))
.build();
}
List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
return newArrayList(new SecurityReference("mykey", authorizationScopes));
}
@Bean
SecurityConfiguration security() {
return new SecurityConfiguration("test-app-client-id", "test-app-client-secret", "test-app-realm", "test-app",
"apiKey", ApiKeyVehicle.HEADER, "api_key",
"," /* scope separator */);
}
@Bean
UiConfiguration uiConfig() {
return new UiConfiguration("validatorUrl", // url
"none", // docExpansion => none | list
"alpha", // apiSorter => alpha
"schema", // defaultModelRendering => schema
false, // enableJsonEditor => true | false
true); // showRequestHeaders => true | false
}
//Here is an example where we select any api that matches one of these paths
// @SuppressWarnings("unchecked")
// private Predicate<String> paths() {
// return or(
// regex("/business.*"),
// regex("/some.*"),
// regex("/contacts.*"),
// regex("/pet.*"),
// regex("/springsRestController.*"),
// regex("/test.*"));
// }
}
没有合适的资源?快使用搜索试试~ 我知道了~
Springfox(Swagger2)结合Springmvc
共82个文件
js:12个
xml:10个
class:8个
4星 · 超过85%的资源 需积分: 10 184 下载量 103 浏览量
2016-04-06
14:47:48
上传
评论 4
收藏 996KB ZIP 举报
温馨提示
Springfox是由Swagger演变而来,一个用于将Controller以文档形式展现出来的框架,里面我还对一些参数进行了描述,便于理解
资源推荐
资源详情
资源评论
收起资源包目录
CURD.zip (82个子文件)
CURD
pom.xml 6KB
target
test-classes
com
madhouse
test
Swagger2MarkupTest.class 4KB
m2e-wtp
web-resources
META-INF
maven
com.madhosue
CURD
pom.xml 6KB
pom.properties 211B
MANIFEST.MF 107B
classes
ApplicationContext.xml 4KB
jdbc.properties 165B
com
madhouse
dao
IStudentDao.class 446B
impl
StudentDao.class 2KB
config
MySpringConfig.class 7KB
model
Student.class 2KB
mapper
StudentMapper.xml 954B
controller
MyController.class 3KB
service
IStudentService.class 458B
impl
StudentService.class 1KB
Spring-servlet.xml 1KB
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.wst.common.component 653B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.validation.prefs 50B
org.eclipse.wst.ws.service.policy.prefs 87B
.jsdtscope 639B
org.eclipse.jdt.core.prefs 736B
src
test
java
com
madhouse
test
Swagger2MarkupTest.java 2KB
main
webapp
css
typography.css 1KB
print.css 41KB
screen.css 43KB
reset.css 1KB
style.css 5KB
images
wordnik_api.png 980B
pet_store_api.png 824B
favicon-32x32.png 2KB
explorer_icons.png 6KB
favicon-16x16.png 645B
logo_small.png 770B
throbber.gif 9KB
favicon.ico 5KB
WEB-INF
web.xml 2KB
student
toUpdate.jsp 1KB
listAll.jsp 1KB
toAdd.jsp 1020B
o2c.html 449B
swagger-ui.js 2.24MB
fonts
droid-sans-v6-latin-700.ttf 40KB
droid-sans-v6-latin-700.woff2 11KB
droid-sans-v6-latin-700.svg 72KB
droid-sans-v6-latin-700.woff 25KB
droid-sans-v6-latin-regular.ttf 38KB
droid-sans-v6-latin-regular.woff 24KB
droid-sans-v6-latin-regular.woff2 11KB
droid-sans-v6-latin-700.eot 22KB
droid-sans-v6-latin-regular.eot 21KB
droid-sans-v6-latin-regular.svg 70KB
index.html 4KB
index.jsp 362B
lib
marked.js 27KB
handlebars-2.0.0.js 47KB
underscore-min.map 27KB
jquery.ba-bbq.min.js 4KB
jquery.slideto.min.js 369B
underscore-min.js 15KB
jquery-1.8.0.min.js 90KB
highlight.7.3.pack.js 7KB
jquery.wiggle.min.js 762B
swagger-oauth.js 9KB
backbone-min.js 20KB
swagger-ui.min.js 358KB
resources
ApplicationContext.xml 4KB
jdbc.properties 165B
Spring-servlet.xml 1KB
java
com
madhouse
dao
IStudentDao.java 328B
impl
StudentDao.java 965B
config
MySpringConfig.java 5KB
model
Student.java 754B
mapper
StudentMapper.xml 954B
controller
MyController.java 3KB
service
IStudentService.java 334B
impl
StudentService.java 929B
.project 1KB
.classpath 1KB
共 82 条
- 1
何铁
- 粉丝: 3
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页