将 Swagger 添加到 Spring MVC 项目(创建 bean 时出错)

Adding Swagger to Spring MVC project (Error creating bean)

这通常是一件非常简单的事情,但我已经坐了很长时间,尝试了多种配置和 git 存储库,但无法让它工作。

我收到的错误如下所示:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.mangofactory.swagger.configuration.SpringSwaggerConfig#0': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mangofactory.swagger.models.ModelProvider com.mangofactory.swagger.configuration.SpringSwaggerConfig.modelProvider; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:304) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) at

这个错误有某种联系,但不知道为什么它不起作用。已尝试 MVN:clean 并更新,但它不起作用。

我的设置如下所示:

src/main/webapp/WEB-INF/config/rest-services-config.xml:

<mvc:default-servlet-handler/>

<context:property-placeholder location="classpath:config/swagger.properties"/>

<!-- creates a controller at /api-docs from this uri, which serves swagger's raw documentation in JSON format. -->
<bean class="com.mangofactory.swagger.configuration.SpringSwaggerConfig" />

src/main/webapp/WEB-INF/Web xml:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Test Swagger</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
            /WEB-INF/config/rest-services-config.xml
        </param-value>
  </context-param>
 <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>restservices</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value></param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>restservices</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>




</web-app>

pom.xml

    <dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>0.9.4</version>

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>${jackson.mapper.version}</version>
    </dependency>

<repositories>
    <repository>
      <id>jcenter-release</id>
      <name>jcenter</name>
      <url>http://oss.jfrog.org/artifactory/oss-release-local/</url>
    </repository>
</repositories>

并在项目中添加了 required 库。 为什么我不能使用 swagger? 我也想在之后加上swagger-ui,这样行吗?

感谢正手

对我来说,你的 POM 中似乎缺少对 Jackson 的依赖:

is java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper

我成功地解决了这个问题,但是出现了另一个更奇怪的错误。 然而,这是解决方案: 我将其添加到我的 pom.xml 文件中:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.5.0</version>
</dependency>