首页 \ 问答 \ Spring Swagger2集成ServletContext自动装配问题(Spring Swagger2 integration ServletContext autowiring issue)

Spring Swagger2集成ServletContext自动装配问题(Spring Swagger2 integration ServletContext autowiring issue)

我正在使用嵌入式Jetty 9运行Spring 4 mvc。我试图插入Swagger2工具,但我遇到了下一个异常

Error creating bean with name 'documentationPluginsBootstrapper'

这个例外的根本原因是

nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.servlet.ServletContext] found for dependency [javax.servlet.ServletContext]: expected at least 1 bean which qualifies as autowire candidate for this dependency.

这是我的SwaggerConfigClass

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    private static final Logger logger = Logger.getLogger(SwaggerConfig.class);

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("TITLE")
                .description("DESCRIPTION")
                .version("VERSION")
                .termsOfServiceUrl("http://terms-of-services.url")
                .license("LICENSE")
                .licenseUrl("http://url-to-license.com")
                .build();
    }
}

之后我创建了工厂类

public class ServletContextFactory implements FactoryBean<ServletContext>,
        ServletContextAware {
    private ServletContext servletContext;

   @Override
    public ServletContext getObject() throws Exception {
        return servletContext;
    }

    @Override
    public Class<?> getObjectType() {
        return ServletContext.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

}

然后在重新声明的bean中手动使用它

@Bean
public DocumentationPluginsBootstrapper documentationPluginsBootstrapper(DocumentationPluginsManager documentationPluginsManager,  
                                                                         List<RequestHandlerProvider> handlerProviders,
                                                                         DocumentationCache scanned,
                                                                         ApiDocumentationScanner resourceListing,
                                                                         TypeResolver typeResolver,
                                                                         Defaults defaults) throws Exception {

    return new DocumentationPluginsBootstrapper(documentationPluginsManager,
            handlerProviders,
            scanned,
            resourceListing,
            typeResolver,
            defaults,
            new ServletContextFactory().getObject());
}

但仍然有一个异常(另一个例外)NullPointerException作为下一个

Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException

那么有人可以建议一些这种自动装配问题的解决方案吗?


I'm running Spring 4 mvc with embedded Jetty 9. I tried to plug in Swagger2 tool, but I faced next exception

Error creating bean with name 'documentationPluginsBootstrapper'

the root cause of this exception is

nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.servlet.ServletContext] found for dependency [javax.servlet.ServletContext]: expected at least 1 bean which qualifies as autowire candidate for this dependency.

This is my SwaggerConfigClass

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    private static final Logger logger = Logger.getLogger(SwaggerConfig.class);

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("TITLE")
                .description("DESCRIPTION")
                .version("VERSION")
                .termsOfServiceUrl("http://terms-of-services.url")
                .license("LICENSE")
                .licenseUrl("http://url-to-license.com")
                .build();
    }
}

After that I created factory class

public class ServletContextFactory implements FactoryBean<ServletContext>,
        ServletContextAware {
    private ServletContext servletContext;

   @Override
    public ServletContext getObject() throws Exception {
        return servletContext;
    }

    @Override
    public Class<?> getObjectType() {
        return ServletContext.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

}

and then manually used it in redeclared bean

@Bean
public DocumentationPluginsBootstrapper documentationPluginsBootstrapper(DocumentationPluginsManager documentationPluginsManager,  
                                                                         List<RequestHandlerProvider> handlerProviders,
                                                                         DocumentationCache scanned,
                                                                         ApiDocumentationScanner resourceListing,
                                                                         TypeResolver typeResolver,
                                                                         Defaults defaults) throws Exception {

    return new DocumentationPluginsBootstrapper(documentationPluginsManager,
            handlerProviders,
            scanned,
            resourceListing,
            typeResolver,
            defaults,
            new ServletContextFactory().getObject());
}

but still had an exception (another exception) NullPointerException as next

Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException

So can anyone suggest some solution of this autowiring problem?


原文:https://stackoverflow.com/questions/44567619
更新时间:2022-07-05 22:07

最满意答案

Paramiko默认不输出任何内容。 您可能调用了日志记录模块,设置了当paramiko设置自己的日志记录时继承的日志级别。

如果您想要访问paramiko logger来覆盖设置:

logger = paramiko.util.logging.getLogger()

还有一个便利功能,可以将所有内容记录到文件中:

paramiko.util.log_to_file('filename.log')

Paramiko doesn't output anything by default. You probably have a call to the logging module, setting a loglevel that's inherited when paramiko sets up it's own logging.

If you want to get at the paramiko logger to override the settings:

logger = paramiko.util.logging.getLogger()

There's also a convenience function to log everything to a file:

paramiko.util.log_to_file('filename.log')

相关问答

更多
  • 用法没有问题,这边也是这么用的,连接后可以直接发送命令,不需要再输入密码。 下面是一个使用ssh查询df信息的例子,Windows XP,Python 3.3,Oracle Linux环境运行正常。 import paramiko client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect("10.0.0.111", 22, "root", "oracle" ...
  • paramiko.SFTPClient 例: import paramiko paramiko.util.log_to_file('/tmp/paramiko.log') # Open a transport host = "example.com" port = 22 transport = paramiko.Transport((host, port)) # Auth password = "foo" username = "bar" transport.connect(username = u ...
  • 在您的环境中,您无法从paramiko连接运行sudo 。 由于您以root身份连接,因此没有理由让您运行sudo 。 试试这个: my_command = 'timeout 10 tcpdump -i eth0 ip -w somefile' In your environment, you cannot run sudo from a paramiko connection. Since you are connecting as root, there is no reason for you to ...
  • 在exec_command执行之后,只有paramiko关闭了通道,并且ssh返回一个auth提示符。 似乎它不可能与paramiko ,尝试fabric或其他工具。 ** fabric也没有解决 。 With just paramiko after the exec_command executes the channel is closed and the ssh returns an auth prompt. Seems its not possible with just paramiko, try ...
  • Paramiko使用Threading库将任务分发到不同的线程,然后设置事件,然后它们相互通信并返回输出。 这里的问题是在test.py中你只是导入test_child而不是专门调用它的任何函数。 因此,调用check_sshd()的执行点是test_child.py而不是test.py。 因此未设置线程事件。 所以对于这个你要么从同一个文件调用函数,要么你需要专门从test.py调用函数而不是只导入文件。 test.py import test_child test_child.check_ssh_co ...
  • 我无法使用您的命令重现问题,但我可以使用cat some_big_file.txt等命令重现它。 所以看起来你的假设是正确的。 在您阅读channel所有内容之前,可以准备退出状态。 目前尚不清楚你是否真的需要使用select 。 如果不是,我会重写循环: while True: buf = channel.recv(1024) if not buf: break print buf 这样的循环将继续读取通道,同时其中包含一些数据。 如果你真的想使用select你可 ...
  • 假设远程用户有一个POSIX shell,这应该工作: def shell_escape(arg): return "'%s'" % (arg.replace(r"'", r"'\''"), ) 为什么这样做? POSIX shell单引号定义为: 用单引号('')括起字符应保留单引号中每个字符的字面值。 单引号内不能出现单引号。 这里的想法是将字符串括在单引号中。 仅此一项就足够了 - 除了单引号之外的每个字符都将按字面解释。 对于单引号,您将退出单引号字符串(第一个' ),添加单引号( \' ...
  • 好吧,发现它:这只是一个pprint奇怪的行为。 我把收到的行放在一个列表中,然后pprint这个列表pprint 。 如果我做 : for line in received: print(line) 然后就可以了。 印刷: ['-rw-r----- 1 myuser mygroup 23228063744 06 mai 11:41 ' 'my_file.txt-rw-r----- 1 ' ... 我不知道为什么。 那么,我会停止使用pprint。 Ok, found ...
  • 这是一个工作示例,用于在本地计算机上提取复制测试文件。 该文件远小于1 GIG,但给出了总体规划。 import paramiko import os import shutil import time import getpass # get params user = getpass.getuser() pwd = getpass.getpass("Enter password: ") bufsize = 2**20 host = 'localhost' test_file_lines = 10000 ...
  • Paramiko默认不输出任何内容。 您可能调用了日志记录模块,设置了当paramiko设置自己的日志记录时继承的日志级别。 如果您想要访问paramiko logger来覆盖设置: logger = paramiko.util.logging.getLogger() 还有一个便利功能,可以将所有内容记录到文件中: paramiko.util.log_to_file('filename.log') Paramiko doesn't output anything by default. You prob ...

相关文章

更多

最新问答

更多
  • 如何检索Ember.js模型的所有属性(How to retrieve all properties of an Ember.js model)
  • maven中snapshot快照库和release发布库的区别和作用
  • arraylist中的搜索元素(Search element in arraylist)
  • 从mysli_fetch_array中获取选定的值并输出(Get selected value from mysli_fetch_array and output)
  • Windows Phone上的可用共享扩展(Available Share Extensions on Windows Phone)
  • 如何在命令提示符下将日期设置为文件名(How to set file name as date in command prompt)
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • 从iframe访问父页面的id元素(accessing id element of parent page from iframe)
  • linux的常用命令干什么用的
  • Feign Client + Eureka POST请求正文(Feign Client + Eureka POST request body)
  • 怎么删除禁用RHEL/CentOS 7上不需要的服务
  • 为什么Gradle运行测试两次?(Why does Gradle run tests twice?)
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在android中的活动之间切换?(Switching between activities in android?)
  • Perforce:如何从Depot到Workspace丢失文件?(Perforce: how to get missing file from Depot to Workspace?)
  • Webform页面避免运行服务器(Webform page avoiding runat server)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 内存布局破解(memory layout hack)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • 我们可以有一个调度程序,你可以异步添加东西,但会同步按顺序执行吗?(Can we have a dispatcher that you can add things todo asynchronously but will be executed in that order synchronously?)
  • “FROM a,b”和“FROM a FULL OUTER JOIN b”之间有什么区别?(What is the difference between “FROM a, b” and “FROM a FULL OUTER JOIN b”?)
  • Java中的不可变类(Immutable class in Java)
  • bat批处理文件结果导出到txt
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • 德州新起点计算机培训学校主要课程有什么?
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • “latin1_german1_ci”整理来自哪里?(Where is “latin1_german1_ci” collation coming from?)