考虑在您的配置中定义类型为 'javax.servlet.ServletContext' 的 bean
Consider defining a bean of type 'javax.servlet.ServletContext' in your configuration
我正在尝试将 API 连接到 postgres 数据库。为此,我正在使用 Spring Boot。
测试 api 时,我不断收到错误
Consider defining a bean of type 'javax.servlet.ServletContext' in your configuration.
我知道我需要一个 @Bean
用于 ServletController
,但我不知道这个 bean 应该做什么。当我做了一个通用的,但随后控制台告诉我重新访问它。我不知道在这里做什么,Whosebug 也没有帮助。
这是我的申请文件:
@SpringBootApplication
@SwaggerDefinition
@EnableSwagger2
@EnableConfigurationProperties(XXXXConfiguracaoApplication.class)
public class FonumS3ApiApplication {
public static void main(String[] args) {
SpringApplication.run(XXXXApiApplication.class, args);
}
@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 ApiInfo(
"API XXXX",
"API XXXX para integrações entre sistemas.",
"API V1",
"Terms of service",
new Contact("XXXX", "www.XXXX.com", "XXXX.XXXX@XXXX.com"),
"License of XXX", "API license URL", Collections.emptyList());
}
}
}
(注释在那里,但由于某种原因,Whosebug 不接受它们)而我的 pom 是
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.13.Final</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
因为您已从网络中排除 Tomcat 并添加了 Tomcat 提供的范围,所以我假设您正在尝试创建 war 并部署到外部 Tomcat,所以您必须扩展 SpringBootServletInitializer
并覆盖 configure
方法。
@SpringBootApplication
@SwaggerDefinition
@EnableSwagger2
@EnableConfigurationProperties(XXXXConfiguracaoApplication.class)
public class FonumS3ApiApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(XXXXApiApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.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 ApiInfo(
"API XXXX",
"API XXXX para integrações entre sistemas.",
"API V1",
"Terms of service",
new Contact("XXXX", "www.XXXX.com", "XXXX.XXXX@XXXX.com"),
"License of XXX", "API license URL", Collections.emptyList());
}
}
}
或者,如果您是 运行 嵌入式 Tomcat,则不要更改主 class 中的任何内容,并从网络中删除 exclusion
并删除 Tomcat jar :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
并删除下面的 Tomcat 罐子:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
我正在尝试将 API 连接到 postgres 数据库。为此,我正在使用 Spring Boot。 测试 api 时,我不断收到错误
Consider defining a bean of type 'javax.servlet.ServletContext' in your configuration.
我知道我需要一个 @Bean
用于 ServletController
,但我不知道这个 bean 应该做什么。当我做了一个通用的,但随后控制台告诉我重新访问它。我不知道在这里做什么,Whosebug 也没有帮助。
这是我的申请文件:
@SpringBootApplication
@SwaggerDefinition
@EnableSwagger2
@EnableConfigurationProperties(XXXXConfiguracaoApplication.class)
public class FonumS3ApiApplication {
public static void main(String[] args) {
SpringApplication.run(XXXXApiApplication.class, args);
}
@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 ApiInfo(
"API XXXX",
"API XXXX para integrações entre sistemas.",
"API V1",
"Terms of service",
new Contact("XXXX", "www.XXXX.com", "XXXX.XXXX@XXXX.com"),
"License of XXX", "API license URL", Collections.emptyList());
}
}
}
(注释在那里,但由于某种原因,Whosebug 不接受它们)而我的 pom 是
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.13.Final</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
因为您已从网络中排除 Tomcat 并添加了 Tomcat 提供的范围,所以我假设您正在尝试创建 war 并部署到外部 Tomcat,所以您必须扩展 SpringBootServletInitializer
并覆盖 configure
方法。
@SpringBootApplication
@SwaggerDefinition
@EnableSwagger2
@EnableConfigurationProperties(XXXXConfiguracaoApplication.class)
public class FonumS3ApiApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(XXXXApiApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.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 ApiInfo(
"API XXXX",
"API XXXX para integrações entre sistemas.",
"API V1",
"Terms of service",
new Contact("XXXX", "www.XXXX.com", "XXXX.XXXX@XXXX.com"),
"License of XXX", "API license URL", Collections.emptyList());
}
}
}
或者,如果您是 运行 嵌入式 Tomcat,则不要更改主 class 中的任何内容,并从网络中删除 exclusion
并删除 Tomcat jar :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
并删除下面的 Tomcat 罐子:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>