我正在开发演示REST服务,使用Spring Boot用户必须登录才能执行某些操作子集.Swagger UI使用springfox该简单配置添加(使用库)后:
@Bean
public Docket docApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(any())
.paths(PathSelectors.ant("/api/**"))
.build()
.pathMapping("/")
.apiInfo(apiInfo())
.directModelSubstitute(LocalDate.class, String.class)
.useDefaultResponseMessages(true)
.enableUrlTemplating(true);
}
Run Code Online (Sandbox Code Playgroud)
我最终得到了所有apis与Swagger UI页面上列出的所有操作.不幸的是,我没有在其中列出登录/注销端点.
问题是部分操作无法通过Swagger UI内置形式执行(我发现它非常好用,并希望使其工作),因为用户没有登录.有没有解决这个问题的方法?我可以手动定义一些端点Swagger吗?
如果有表单提交凭证(即登录/注销端点),我可以在使用该安全端点之前执行授权.然后,Swagger用户可以token/sessionid从响应中提取并将其粘贴到通过定义的自定义查询参数@ApiImplicitParams.
您可以在下面找到我的安全配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin()
.loginProcessingUrl("/api/login")
.usernameParameter("username")
.passwordParameter("password")
.successHandler(new CustomAuthenticationSuccessHandler())
.failureHandler(new CustomAuthenticationFailureHandler())
.permitAll()
.and()
.logout()
.logoutUrl("/api/logout")
.logoutSuccessHandler(new CustomLogoutSuccessHandler())
.deleteCookies("JSESSIONID")
.permitAll()
.and()
.csrf()
.disable()
.exceptionHandling()
.authenticationEntryPoint(new CustomAuthenticationEntryPoint())
.and()
.authorizeRequests()
.and() …Run Code Online (Sandbox Code Playgroud) 我们在API网关后面提供了Spring Boot服务。使用较早版本的Springfox-2.1.2,我们在加载swagger-ui.html页面时没有任何问题。这与Spring Boot 1.4.3.RELEASE一起使用。从那时起,我们已升级到Boot 1.5.7,并将Springfox升级到了2.8.0。
现在,如果我们加载页面,则会显示一个带有以下长消息的警报框。
无法推断基本网址。当使用动态servlet注册或API位于API网关后面时,这很常见。基本url是为所有swagger资源提供服务的根。例如,如果api在http://example.org/api/v2/api-docs上可用,则基本URL是http://example.org/api/。请手动输入位置
我在网上搜索时得到了一些提示,但是这些情况似乎不适用于我们。例如,如果我只是还原版本,它将通过相同的API网关再次开始工作。
跟踪流量,似乎调用了.html页所提供的三个XHR资源引起了问题。这些从我们的API网关返回401。它们返回401的原因是未传递cookie。
这三个调用是:
如果我将这些URL作为纯浏览器请求加载-它们可以工作-因为发送了cookie。
我怀疑是否适用CORS,因为HTML是从与招摇的JSON和实际服务调用相同的地址提供的。
知道为什么会这样吗?有人遇到类似的问题吗?解决方法的建议?在此先感谢。
我使用Springfox遇到了一些小麻烦.我无法为@RestController类设置名称.
我正在使用Spring启动和Swagger2.
以下代码将在springfox ui中生成一个名为"rest-status-controller"的控制器.我期待一个"应用程序状态".还有其他配置我不知道吗?
@Api("Application Status")
@RestController
@RequestMapping("/rest/status")
public class RestStatusController {
@ApiOperation(value="Get components current status")
@RequestMapping(method = RequestMethod.GET, produces=MediaType.APPLICATION_JSON)
public String global() {
//...
}
@ApiOperation(value="Get mysql current status")
@RequestMapping(value="/mysql" method = RequestMethod.GET, produces=MediaType.APPLICATION_JSON)
public String mysql() {
//...
}
}
Run Code Online (Sandbox Code Playgroud) 我已经使用 springfox 2.0 配置了我的 spring 项目。我能够用它生成开放的 api 规范。
"paths": {
"/test/testinfo": {
"post": {
"tags": [
"test-controller"
],
"summary": "getTestInfo",
"operationId": "getTestInfoInfoUsingGET",
"consumes": [
"application/json"
],
"produces": [
"application/json"
]
Run Code Online (Sandbox Code Playgroud)
正如你所看到的 operationId 的值是格式
[java_method_name_here]Using[HTTP_verb_here]
Run Code Online (Sandbox Code Playgroud)
前任。getPetsUsingGET
此 operationId 在使用 swagger-codegen 生成客户端时使用。有谁知道如何自定义它?我知道这可以通过每个 api 使用来完成,@ApiOperation但是有没有更通用的方法来为所有 api 定义这种格式?
我正在设计一个api,其中一个POST方法采用Map<String, String>任何键值对.
@RequestMapping(value = "/start", method = RequestMethod.POST)
public void startProcess(
@ApiParam(examples = @Example(value = {
@ExampleProperty(
mediaType="application/json",
value = "{\"userId\":\"1234\",\"userName\":\"JoshJ\"}"
)
}))
@RequestBody(required = false) Map<String, String> fields) {
// .. does stuff
}
Run Code Online (Sandbox Code Playgroud)
我想提供一个示例输入,fields但我似乎无法让它在swagger输出中呈现.这不是正确的使用方法@Example吗?
我正在尝试运行我的 Spring boot 应用程序,该应用程序基于带有 swagger UI 的 3.0 版本,并且遇到了很多异常,我已经探索了许多来源,例如 youtube 和文档,但我无法找到解决方案。
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
如果有人可以分享解决方案,那就太好了。
将Swagger Codegen生成的服务器存根插入现有Spring MVC应用程序的最佳方法是什么?
我开始试图使用petstore存根样本.
我的Spring配置是用Java编写的,如下所示:
public class SpringConfigurationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { ApplicationContext.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { WebMvcContext.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
// ... onStartup etc.
}
Run Code Online (Sandbox Code Playgroud)
WebMvcConfigurationSupport:
@Configuration
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ)
@PropertySource({ "classpath:config.properties", "file:${CONFIGDIR}/config.properties" })
@ComponentScan(useDefaultFilters = false, basePackages = { "com.yyy", "com.xxx" }, includeFilters = { @Filter(type = FilterType.ANNOTATION, …Run Code Online (Sandbox Code Playgroud) 我有使用另一个Spring启动授权服务器的spring boot rest api(资源),我已经将Swagger配置添加到资源应用程序,以便为其余的API获得一个漂亮而快速的文档/测试平台.我的Swagger配置如下所示:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Autowired
private TypeResolver typeResolver;
@Value("${app.client.id}")
private String clientId;
@Value("${app.client.secret}")
private String clientSecret;
@Value("${info.build.name}")
private String infoBuildName;
public static final String securitySchemaOAuth2 = "oauth2";
public static final String authorizationScopeGlobal = "global";
public static final String authorizationScopeGlobalDesc = "accessEverything";
@Bean
public Docket api() {
List<ResponseMessage> list = new java.util.ArrayList<ResponseMessage>();
list.add(new ResponseMessageBuilder()
.code(500)
.message("500 message")
.responseModel(new ModelRef("JSONResult«string»"))
.build());
list.add(new ResponseMessageBuilder()
.code(401)
.message("Unauthorized")
.responseModel(new ModelRef("JSONResult«string»"))
.build());
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.securitySchemes(Collections.singletonList(securitySchema())) …Run Code Online (Sandbox Code Playgroud) 我在 Spring Boot 应用程序中使用 springfox-boot-starter 依赖项来获取 swagger UI,但是当我尝试运行该应用程序时,出现以下错误
Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NoSuchMethodError: org.springframework.plugin.core.PluginRegistry.getPluginFor(Ljava/lang/Object;)Ljava/util/Optional;
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:184)
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:52)
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356)
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:157)
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:121)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:885)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:161)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)
Run Code Online (Sandbox Code Playgroud)
我的配置文件:
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.swagger.demo.controller"))
.paths(regex("/student.*"))
.build()
.enableUrlTemplating(true)
.produces(DEFAULT_PRODUCES_AND_CONSUMES)
.consumes(DEFAULT_PRODUCES_AND_CONSUMES)
.apiInfo(apiEndPointsInfo());
}
private ApiInfo apiEndPointsInfo() {
return new ApiInfoBuilder()
.title("demo")
.description("demo")
.version("1.0")
.build();
}
Run Code Online (Sandbox Code Playgroud)
以下是我正在使用的依赖项。 pom.xml 文件:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from …Run Code Online (Sandbox Code Playgroud) 我使用SpringFox库来获取我的spring boot应用程序的休息文档.当我单击模型时,所有元素都将作为可选项返回.有没有办法将所需元素显示为必需元素?是否需要添加其他配置?