将Spring应用部署到没有web.xml的Tomcat。

huangapple go评论76阅读模式
英文:

Deploy Spring App to Tomcat without web.xml

问题

我已经成功地使用嵌入的TOMCAT测试了我的war文件,并且它正常工作。现在我想将其部署到TOMCAT,但我一直得到404错误。

我在浏览器中导航到的URL是
http://localhost:9999/springrest/demo/users/all

我的TOMCAT使用端口9999

我的JRE_HOME版本是1.8.0_261

我的pom.xml中有**<java.version>1.8</java.version>**

TOMCAT管理器显示应用正在运行
将Spring应用部署到没有web.xml的Tomcat。

我的控制器代码如下

@RestController
@RequestMapping(path="/demo") 
public class UserController {

    @Autowired 
    private UserRepository userRepository;

    @GetMapping(path="/users/all")
    public @ResponseBody Iterable<User> getAllUsers() {
        // This returns a JSON or XML with the users
        return userRepository.findAll();
    }

}

我的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.0.0.RELEASE</version>
        <relativePath/> 
    </parent>
    <groupId>com.eptc</groupId>
    <artifactId>springrest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springrest</name>
    <description>Rest API for mobile app</description>
    <packaging>war</packaging>

    <properties>
        <java.version>1.8</java.version>	
        <start-class>com.eptc.springrest.SpringrestApplication</start-class>
    </properties>

    <dependencies>
        <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>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.6.2</version>
            <scope>test</scope>
            <type>jar</type>
        </dependency> 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>${artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
英文:

I have successfully tested my war file with embedded TOMCAT and it works fine. Now I want to deploy it to TOMCAT but I keep getting a 404 error.

The URL I navigate to on the browser is
http://localhost:9999/springrest/demo/users/all

My TOMCAT uses the port 9999

My JRE_HOME version is 1.8.0_261

My pom.xml has <java.version>1.8</java.version>

TOMCAT manager shows the app as running
将Spring应用部署到没有web.xml的Tomcat。

My Controller code is as follows

  @RestController
@RequestMapping(path=&quot;/demo&quot;) 
public class UserController {
@Autowired 
private UserRepository userRepository;
@GetMapping(path=&quot;/users/all&quot;)
public @ResponseBody Iterable&lt;User&gt; getAllUsers() {
// This returns a JSON or XML with the users
return userRepository.findAll();
}
}

My pom.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; 
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 
https://maven.apache.org/xsd/maven-4.0.0.xsd">
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;parent&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&lt;version&gt;2.0.0.RELEASE&lt;/version&gt;
&lt;relativePath/&gt; 
&lt;/parent&gt;
&lt;groupId&gt;com.eptc&lt;/groupId&gt;
&lt;artifactId&gt;springrest&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;name&gt;springrest&lt;/name&gt;
&lt;description&gt;Rest API for mobile app&lt;/description&gt;
&lt;packaging&gt;war&lt;/packaging&gt;
&lt;properties&gt;
&lt;java.version&gt;1.8&lt;/java.version&gt;	
&lt;start-class&gt;com.eptc.springrest.SpringrestApplication&lt;/start-class&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;mysql&lt;/groupId&gt;
&lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
&lt;scope&gt;runtime&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;exclusions&gt;
&lt;exclusion&gt;
&lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
&lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;/exclusions&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
&lt;artifactId&gt;junit-jupiter-api&lt;/artifactId&gt;
&lt;version&gt;5.6.2&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;type&gt;jar&lt;/type&gt;
&lt;/dependency&gt; 
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
&lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
&lt;finalName&gt;${artifactId}&lt;/finalName&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;

</project>

答案1

得分: 0

你是否通过实现SpringBootServletInitializer接口来初始化Tomcat所需的Servlet上下文?

@SpringBootApplication
public class SpringBootTomcatApplication extends SpringBootServletInitializer {
}

请参考此链接获取详细信息:将Spring Boot WAR部署到Tomcat服务器

英文:

Have you initialized the Servlet context required by Tomcat by implementing the SpringBootServletInitializer interface?

@SpringBootApplication
public class SpringBootTomcatApplication extends SpringBootServletInitializer {
}

Please refer this link for the details :Deploy a Spring Boot WAR into a Tomcat Server

答案2

得分: 0

将我的 Apache Tomcat 版本 更改为 Apache Tomcat 8.5.58,然后它就能正常运行。
在运行 catalina.bat run 时,甚至在 cmd 上显示了 Springboot 横幅。

英文:

Changed my Apache Tomcat version to Apache Tomcat 8.5.58 and it worked like a charm.
The Springboot banner even shows on cmd on running catalina.bat run

huangapple
  • 本文由 发表于 2020年9月29日 15:07:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64114525.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定