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')

相关问答

更多

windows下paramiko sshclient远程登录到linux机器的交互问题

用法没有问题,这边也是这么用的,连接后可以直接发送命令,不需要再输入密码。 下面是一个使用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", "or...

paramiko的sshclient与sftp(paramiko's sshclient with sftp)

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(usernam...

无法使用Paramiko远程执行tcpdump(Unable to execute tcpdump remotely with Paramiko)

在您的环境中,您无法从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 y...

Python的paramiko模块使用多个命令(Python paramiko module using multiple commands)

在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...

paramiko SSHClient连接卡住了(paramiko SSHClient connect stuck)

Paramiko使用Threading库将任务分发到不同的线程,然后设置事件,然后它们相互通信并返回输出。 这里的问题是在test.py中你只是导入test_child而不是专门调用它的任何函数。 因此,调用check_sshd()的执行点是test_child.py而不是test.py。 因此未设置线程事件。 所以对于这个你要么从同一个文件调用函数,要么你需要专门从test.py调用函数而不是只导入文件。 test.py import test_child test_child.check_s...

Paramiko在读取所有输出之前完成处理(Paramiko finish process before reading all output)

我无法使用您的命令重现问题,但我可以使用cat some_big_file.txt等命令重现它。 所以看起来你的假设是正确的。 在您阅读channel所有内容之前,可以准备退出状态。 目前尚不清楚你是否真的需要使用select 。 如果不是,我会重写循环: while True: buf = channel.recv(1024) if not buf: break print buf 这样的循环将继续读取通道,同时其中包含一些数据。 如果你真的想使用sel...

转义paramiko.SSHClient()。exec_command的参数(Escape arguments for paramiko.SSHClient().exec_command)

假设远程用户有一个POSIX shell,这应该工作: def shell_escape(arg): return "'%s'" % (arg.replace(r"'", r"'\''"), ) 为什么这样做? POSIX shell单引号定义为: 用单引号('')括起字符应保留单引号中每个字符的字面值。 单引号内不能出现单引号。 这里的想法是将字符串括在单引号中。 仅此一项就足够了 - 除了单引号之外的每个字符都将按字面解释。 对于单引号,您将退出单引号字符串(第一个' ),添加单引号...

增加paramiko.SSHClient.exec_command()宽度(increase paramiko.SSHClient.exec_command() width)

好吧,发现它:这只是一个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, ...

Paramiko Buffer问题(Paramiko Buffer issue)

这是一个工作示例,用于在本地计算机上提取复制测试文件。 该文件远小于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 = ...

抑制Paramiko SSHClient类的输出(Suppressing Output of Paramiko SSHClient Class)

Paramiko默认不输出任何内容。 您可能调用了日志记录模块,设置了当paramiko设置自己的日志记录时继承的日志级别。 如果您想要访问paramiko logger来覆盖设置: logger = paramiko.util.logging.getLogger() 还有一个便利功能,可以将所有内容记录到文件中: paramiko.util.log_to_file('filename.log') Paramiko doesn't output anything by default. You...

相关文章

更多

springboot整合Swagger2

Swagger2是一个开源项目,用于为RESTful Web服务自动生成REST API文档。 它提供 ...

使用spring-integration-kafka操作kafka

依赖 &lt;dependency&gt; &lt;groupId&gt;org.springf ...

Spring MVC 3 深入总结

一、前言: 大家好,Spring3 MVC是非常优秀的MVC框架,由其是在3.0版本发布后,现在有越来 ...

Swagger UI 快速入门-springmvc 整合Swagger UI 教程

现在很多开发团队都是前端开发与服务端分离,前端直接调用服务端写好接口。 如果你是服务端开发人员,也许你 ...

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本