我有一个使用Spring xml配置构建的应用程序,我想添加Swagger来记录端点。我在网上遵循了指南,但偶然发现了错误。
首先,我遇到了这个异常:
由于:java.io.FileNotFoundException:类路径资源 [org / springframework / boot / autoconfigure / condition / ConditionalOnWebApplication.class] 无法打开,因为它不存在
我在Swagger2DocumentationConfiguration类中展示了Swagger使用@ConditionalOnWebApplication,它需要将spring-boot-autoconfigure 2.1.5-RELEASE添加到pom.xml中。
之后,我得到了这个异常:
无法加载bean类: springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration; 嵌套的异常是org.springframework.core.NestedIOException:ASM ClassReader无法解析类文件-可能是由于新的Java 尚不支持的课程文件版本
这是我的配置xml:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.x.x"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="contentType" value="application/xml;charset=UTF-8"/>
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:resources mapping="/css/**" location="WEB-INF/css/"/>
<mvc:resources mapping="/js/**" location="WEB-INF/js/"/>
<mvc:resources mapping="/fonts/**" location="WEB-INF/fonts/"/>
<mvc:resources mapping="/images/**" location="WEB-INF/images/"/>
<mvc:resources mapping="/lib/**" location="WEB-INF/dojo/"/>
<mvc:resources order="1" location="/resources/" mapping="/resources/**" />
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />
<mvc:default-servlet-handler />
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
<tx:annotation-driven/>
<bean id="propertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="ignoreResourceNotFound" value="false"/>
<property name="locations">
<list>
<value>classpath:application.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" autowire-candidate="true">
<property name="driverClassName" value="${datasource.driverClassName}"/>
<property name="url" value="${datasource.url}"/>
<property name="username" value="${datasource.username}"/>
<property name="password" value="${datasource.password}"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.default_schema">xxx</prop>
<!--prop key="hibernate.hbm2ddl.auto">update</prop-->
</props>
</property>
<property name="packagesToScan" value="com.ikh.kam.*"/>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory">
</bean>
<bean id="swagger2Config"
class="springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration">
</bean>
</beans>
某些配置已经存在。
我创建了一个Config类:
@Configuration
@ImportResource("classpath:/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml")
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}