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
最满意答案
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')
相关问答
更多-
windows下paramiko sshclient远程登录到linux机器的交互问题[2022-02-19]
用法没有问题,这边也是这么用的,连接后可以直接发送命令,不需要再输入密码。 下面是一个使用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你可 ...
-
转义paramiko.SSHClient()。exec_command的参数(Escape arguments for paramiko.SSHClient().exec_command)[2023-10-10]
假设远程用户有一个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 ...
-
Paramiko Buffer问题(Paramiko Buffer issue)[2021-10-26]
这是一个工作示例,用于在本地计算机上提取复制测试文件。 该文件远小于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 ...