问题描述
我一直在尝试根据this tutorial用Spring MVC配置Swagger 2。它与Spring Boot兼容,但与Spring MVC兼容。我什至没有收到任何错误消息。我刚收到“ HTTP状态404 –找不到”。
我正在使用Spring MVC 5.2.9.RELEASE和Springfox 2.9.2。这是pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ControleGestao</groupId>
<artifactId>ControleGestao</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin-version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<spring-mvc-version> 5.2.9.RELEASE </spring-mvc-version>
<apache-tomcat-version>8.5.58</apache-tomcat-version>
<servlet-version>2.0</servlet-version>
<javax-servlet-jsp-jstl-version>1.2</javax-servlet-jsp-jstl-version>
<org-slf4j-version>1.7.30</org-slf4j-version>
<org-glassfish-web-version>1.2</org-glassfish-web-version>
<!-- <jcl-over-slf4j-version>1.7.25</jcl-over-slf4j-version> -->
<slf4j-log4j12-version>1.7.30</slf4j-log4j12-version>
<log4j-version>1.2.17</log4j-version>
<maven-compiler-plugin-version>3.8.1</maven-compiler-plugin-version>
<hibernate-entitymanager-version>5.4.22.Final</hibernate-entitymanager-version>
<hibernate-jpa-version>1.0.2.Final</hibernate-jpa-version>
<spring-orm-version>5.2.9.RELEASE</spring-orm-version>
<springfox-swagger2-version>2.9.2</springfox-swagger2-version>
<springfox-swagger2-ui-version>2.6.1</springfox-swagger2-ui-version>
<hibernate-validator-version>6.0.21.Final</hibernate-validator-version>
<log4j-version>1.2.16</log4j-version>
<jcl-over-slf4j-version>1.7.30</jcl-over-slf4j-version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<scope>compile</scope>
<version>${spring-mvc-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring-mvc-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring-mvc-version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>${apache-tomcat-version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${servlet-version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>${javax-servlet-jsp-jstl-version}</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jstl-impl</artifactId>
<version>${org-glassfish-web-version}</version>
</dependency>
<!-- Logging Primeiro -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org-slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${jcl-over-slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j-log4j12-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j-version}</version>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate-entitymanager-version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate-entitymanager-version}</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>${hibernate-jpa-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring-orm-version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-swagger2-version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox-swagger2-ui-version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate-validator-version}</version>
</dependency>
</dependencies>
</project>
现在是调度员:
package com.controlegestao.configs;
import org.springframework.web.servlet.support.AbstractAnnotationConfigdispatcherServletinitializer;
public class InicioConfigsMVC extends AbstractAnnotationConfigdispatcherServletinitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
// Todo Auto-generated method stub
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {AppConfigClass.class,JPAConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
}
Web配置:
package com.controlegestao.configs;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import com.controlegestao.controllers.InicioController;
import com.controlegestao.daos.ProdutosDao;
import com.controlegestao.servicos.ProdutosServicos;
@EnableWebMvc
@ComponentScan(basePackageClasses = { InicioController.class,ProdutosDao.class,ProdutosServicos.class
})
public class AppConfigClass {
@Bean
public InternalResourceViewResolver InternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}
我最后一次尝试的Swagger配置(因为我已经尝试了其他Web方式):
package com.controlegestao.configs;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SpringFoxConfig extends WebMvcConfigurerAdapter {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
@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/");
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)