当我使用mavan构建它时。war文件已经生成,我在tomcat中部署了它,但它不起作用??war文件不起作用。如果我尝试单击tomcat中的链接。网络应用程序显示404。那么这有什么问题呢。我附加了我的主类文件、应用程序属性和pom.xml文件。如果我建立一个war文件。然后我部署在tomcat中。我的数据库没有创建任何表,我想我的jdbc连接和tomcat webapp可能有问题。所以请验证代码并给我一些答案
主类
package com.eot.eot;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@SpringBootApplication
public class EotApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
System.getProperties().put("server.port", 8081);
SpringApplication.run(EotApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(EotApplication.class);
}
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CorsFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Methods",
"ACL, CANCELUPLOAD, CHECKIN, CHECKOUT, COPY, DELETE, GET, HEAD, LOCK, MKCALENDAR, MKCOL, MOVE, OPTIONS, POST, PROPFIND, PROPPATCH, PUT, REPORT, SEARCH, UNCHECKOUT, UNLOCK, UPDATE, VERSION-CONTROL");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Key, Authorization");
if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
response.setStatus(HttpServletResponse.SC_OK);
} else {
chain.doFilter(req, res);
}
}
public void init(FilterConfig filterConfig) {
// not needed
}
public void destroy() {
//not needed
}
}
}
应用程序属性
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/onlinedb
spring.datasource.username= root
spring.datasource.password= 1234
spring.jpa.show-sql= true
spring.jpa.hibernate.ddl-auto= update
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5Dialect
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.eot</groupId>
<artifactId>eot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>eot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<start-class>com.eot.eot.EotApplication</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
终端mavan构建
PS D:\Studies\EOT\EOT-BE\eot> & "d:\Studies\EOT\EOT-BE\eot\mvnw.cmd" clean -f "d:\Studies\EOT\EOT-BE\eot\pom.xml"
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------------< com.eot:eot >-----------------------------
[INFO] Building eot 0.0.1-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ eot ---
[INFO] Deleting d:\Studies\EOT\EOT-BE\eot\target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14.889 s
[INFO] Finished at: 2021-05-03T09:04:53+05:30
[INFO] ------------------------------------------------------------------------
PS D:\Studies\EOT\EOT-BE\eot> & "d:\Studies\EOT\EOT-BE\eot\mvnw.cmd" install -f "d:\Studies\EOT\EOT-BE\eot\pom.xml"
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------------< com.eot:eot >-----------------------------
[INFO] Building eot 0.0.1-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ eot ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ eot ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 14 source files to d:\Studies\EOT\EOT-BE\eot\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ eot ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory d:\Studies\EOT\EOT-BE\eot\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ eot ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to d:\Studies\EOT\EOT-BE\eot\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ eot ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.eot.eot.EotApplicationTests
09:07:15.570 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
09:07:15.690 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
09:07:15.916 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.eot.eot.EotApplicationTests] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper]
09:07:16.094 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test
class [com.eot.eot.EotApplicationTests], using SpringBootContextLoader
09:07:16.149 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.eot.eot.EotApplicationTests]: class path resource [com/eot/eot/EotApplicationTests-context.xml] does not exist
09:07:16.151 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.eot.eot.EotApplicationTests]: class path resource [com/eot/eot/EotApplicationTestsContext.groovy] does not exist
09:07:16.151 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.eot.eot.EotApplicationTests]: no resource found for suffixes {-context.xml, Context.groovy}.
09:07:16.154 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.eot.eot.EotApplicationTests]: EotApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
09:07:16.564 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.eot.eot.EotApplicationTests]
09:07:17.114 [main] DEBUG org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider - Identified candidate component class: file [d:\Studies\EOT\EOT-BE\eot\target\classes\com\eot\eot\EotApplication.class]
09:07:17.119 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.eot.eot.EotApplication for test
class com.eot.eot.EotApplicationTests
09:07:18.250 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - @TestExecutionListeners is not present for class [com.eot.eot.EotApplicationTests]: using defaults.
09:07:18.252 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.event.ApplicationEventsTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
09:07:18.685 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@7d42c224, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@56aaaecd, org.springframework.test.context.event.ApplicationEventsTestExecutionListener@522a32b1, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@35390ee3, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@5e01a982, org.springframework.test.context.support.DirtiesContextTestExecutionListener@5ddea849, org.springframework.test.context.transaction.TransactionalTestExecutionListener@5ee2b6f9, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@23d1e5d0, org.springframework.test.context.event.EventPublishingTestExecutionListener@704f1591, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@58fb7731, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@13e547a9, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@3fb6cf60, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@37ddb69a, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@349c1daf, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener@dfddc9a]
09:07:18.726 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@18c5069b testClass = EotApplicationTests, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@3a0d172f testClass = EotApplicationTests, locations = '{}', classes = '{class com.eot.eot.EotApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@13d9cbf5, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@c667f46, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@3b5fad2d, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@30b6ffe0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@609bcfb6, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@c267ef4], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes
= map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true]], class annotated with @DirtiesContext [false] with mode [null].
09:07:19.110 [main] DEBUG org.springframework.test.context.support.TestPropertySourceUtils - Adding inlined properties to environment: {spring.jmx.enabled=false, org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.5)
2021-05-03 09:07:21.560 INFO 2648 --- [ main] com.eot.eot.EotApplicationTests : Starting EotApplicationTests using Java 11.0.10 on DESKTOP-QPK38JR with PID 2648 (started by Thiru in d:\Studies\EOT\EOT-BE\eot)
2021-05-03 09:07:21.572 INFO 2648 --- [ main] com.eot.eot.EotApplicationTests : No active profile set, falling back to default profiles: default
2021-05-03 09:07:26.508 INFO 2648 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-05-03 09:07:26.968 INFO 2648 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 425 ms. Found 3 JPA repository interfaces.
2021-05-03 09:07:30.618 INFO 2648 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-05-03 09:07:31.093 INFO 2648 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.30.Final
2021-05-03 09:07:32.522 INFO 2648 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-05-03 09:07:34.614 INFO 2648 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2021-05-03 09:07:37.186 INFO 2648 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2021-05-03 09:07:37.408 INFO 2648 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2021-05-03 09:07:42.498 INFO 2648 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-05-03 09:07:42.548 INFO 2648 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-05-03 09:07:45.363 WARN 2648 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-05-03 09:07:46.741 INFO 2648 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-05-03 09:07:48.091 WARN 2648 --- [ main] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
2021-05-03 09:07:48.795 INFO 2648 --- [ main] com.eot.eot.EotApplicationTests : Started EotApplicationTests in 29.627 seconds (JVM running for 39.577)
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 35.433 s - in com.eot.eot.EotApplicationTests
2021-05-03 09:07:50.415 INFO 2648 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
2021-05-03 09:07:50.424 INFO 2648 --- [extShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2021-05-03 09:07:50.453 INFO 2648 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2021-05-03 09:07:50.599 INFO 2648 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-war-plugin:3.3.1:war (default-war) @ eot ---
[INFO] Packaging webapp
[INFO] Assembling webapp [eot] in [d:\Studies\EOT\EOT-BE\eot\target\eot-0.0.1-SNAPSHOT]
[INFO] Processing war project
[INFO] Building war: d:\Studies\EOT\EOT-BE\eot\target\eot-0.0.1-SNAPSHOT.war
[INFO]
[INFO] --- spring-boot-maven-plugin:2.4.5:repackage (repackage) @ eot ---
[INFO] Replacing main artifact with repackaged archive
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ eot ---
[INFO] Installing d:\Studies\EOT\EOT-BE\eot\target\eot-0.0.1-SNAPSHOT.war to C:\Users\Thiru\.m2\repository\com\eot\eot\0.0.1-SNAPSHOT\eot-0.0.1-SNAPSHOT.war
[INFO] Installing d:\Studies\EOT\EOT-BE\eot\pom.xml to C:\Users\Thiru\.m2\repository\com\eot\eot\0.0.1-SNAPSHOT\eot-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 03:17 min
[INFO] Finished at: 2021-05-03T09:08:42+05:30
[INFO] ------------------------------------------------------------------------
```[tomcat webapp][1]
[1]: https://i.stack.imgur.com/utjwv.png
暂无答案!
目前还没有任何答案,快来回答吧!