Java 在tomcat上找不到Spring引导应用程序-404
我已经读过很多关于这方面的问题,但没有一个能帮上忙。 我正在构建一个非常简单的rest应用程序,但控制器没有响应请求,因此出现以下错误:Java 在tomcat上找不到Spring引导应用程序-404,java,spring,maven,tomcat,http-status-code-404,Java,Spring,Maven,Tomcat,Http Status Code 404,我已经读过很多关于这方面的问题,但没有一个能帮上忙。 我正在构建一个非常简单的rest应用程序,但控制器没有响应请求,因此出现以下错误: HTTP Status 404 – Not Found Type Status Report Message / Description The origin server did not find a current representation for the target resource or is not willing to disclose
HTTP Status 404 – Not Found
Type Status Report
Message /
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Apache Tomcat/8.5.23
我在控制器中放入了System.out
,但也没有显示,因此这些方法甚至没有被调用
我的项目结构是:
应用程序类位于app
package下
配置:
@Configuration
@EnableTransactionManagement
public class DatabaseConfig {
@Autowired
private Environment env;
@Bean(name = "dataSource")
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("db.driver"));
dataSource.setUrl(env.getProperty("db.url"));
dataSource.setUsername(env.getProperty("db.username"));
dataSource.setPassword(env.getProperty("db.password"));
return dataSource;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManagerFactory =
new LocalContainerEntityManagerFactoryBean();
entityManagerFactory.setDataSource(dataSource());
// Classpath scanning of @Component, @Service, etc annotated class
entityManagerFactory.setPackagesToScan(
env.getProperty("entitymanager.packagesToScan"));
// Vendor adapter
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
entityManagerFactory.setJpaVendorAdapter(vendorAdapter);
// Hibernate properties
Properties additionalProperties = new Properties();
additionalProperties.put(
"hibernate.dialect",
env.getProperty("hibernate.dialect"));
additionalProperties.put(
"hibernate.show_sql",
env.getProperty("hibernate.show_sql"));
additionalProperties.put(
"hibernate.hbm2ddl.auto",
env.getProperty("hibernate.hbm2ddl.auto"));
entityManagerFactory.setJpaProperties(additionalProperties);
return entityManagerFactory;
}
@Bean
public JpaTransactionManager transactionManager() {
JpaTransactionManager transactionManager =
new JpaTransactionManager();
transactionManager.setEntityManagerFactory(
entityManagerFactory().getObject());
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}
控制器:
@Controller
public class CharacterController {
@RequestMapping(value="/", method= RequestMethod.GET)
public String start() {
System.out.println("Start");
return "Hello.";
}
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>xxxxxxxxxxx</groupId>
<artifactId>xxxxxxxxxxxxxxx</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>xxxxxxxxxxxxxxxxx</name>
<description>xxxxxxxxxxxxxxxxxxxxxx</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent fapp.repositorytory -->
</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.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</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-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1206-jdbc42</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我还尝试使用@SpringBootApplication
,将(basePackages={“app”,“app.config”etc})放入,只是应用程序
,控制器类。。。即使将所有类移到同一个包中,但我现在确信这不是问题所在
控制台日志以以下内容结束:
2018-01-27 00:08:08.291 INFO 14596 --- [ main] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000228: Running hbm2ddl schema update
2018-01-27 00:08:09.634 INFO 14596 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2018-01-27 00:08:10.584 INFO 14596 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-01-27 00:08:10.632 INFO 14596 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-01-27 00:08:10.637 INFO 14596 --- [ main] app.Application : Started Application in 38.878 seconds (JVM running for 39.335)
所以它看起来应该是有效的
环境等级:
package org.springframework.core.env;
public interface Environment extends PropertyResolver {
String[] getActiveProfiles();
String[] getDefaultProfiles();
boolean acceptsProfiles(String... var1);
}
当您使用SpringBoot时,您应该记住它为您管理大量的工作人员
您应该使用Spring引导依赖项,如:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
如果所有这些都不起作用,您应该创建一个新的Spring boot项目,可能使用eclipse的STS插件,或者从下载一个带有依赖项的项目。然后开始回答您的问题,我强烈建议你先看一些Spring文档:@BenBen是的,我以前读过所有这些,但我真的不知道我的应用程序缺少什么…你能发布classEnvironment.java
?的内容吗?DoNhuVy在最后补充道。不过,这不是我的课,这是从Spring来的,我需要付出很多努力来重现你的错误。您不发布属性文件,也不发布带有导入的Java源代码,也不发布数据库模式和格式源代码。你能把你的源代码发布到GitHub上,并给我一个链接吗?我会为你修复。我有一段时间依赖于starterweb
,它的工作效率甚至比现在还要低。我得到java.lang.NoClassDefFoundError:org/springframework/core/kotlindector
异常。在阅读了其他一些问题后,我用tomcat替换了它,它至少启动并创建了数据库方案。我确实通过添加Jpa依赖项生成了这个项目。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Mapped "{[/],methods=[GET]}" onto public java.lang.String com.example.demo.CharacterController.start()