After deploying war file in Tomcat api not working

我们两清 提交于 2019-12-25 03:45:09

问题


I'm trying to deploy app in my local tomcat but have some problems. I use: Tomcat 9, Spring boot, ReactJS and Webpack. When I run embedded Tomcat (in Eclipse) all be ok - API working good, but when I build war file and paste it to my local Tomcat - API not working, all request failed.

How I build war file:

  • run mvn "clean install";
  • paste war file to my local Tomcat directory "webapps";
  • wait for deploying;
  • go to "http://localhost:8080/web_importer/#/importCandidates". But in this moment when i run in Eclipse my app i use another path: "http://localhost:8080/#/importCandidates" and API use this path to (example) "http://localhost:8080/api/originator", not "http://localhost:8080/web_importer/api/originator".

How I can change this path?

application.properties:

server.port = 8080
management.server.port: 8995
management.server.address: 127.0.0.1
spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=100MB
spring.http.encoding.force=true

OriginatorController.java:

@RequestMapping(value = "/api/originator", method = RequestMethod.GET)
public List<OriginatorModel> retrieveOriginators() {
    logger.info("Performing /api/originator GET request");
    return originatorService.retrieveOriginators();
}

My pom.xml:

    <groupId>com.kddb_web_importer</groupId>
    <artifactId>web_importer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>web_importer</name>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>   
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>           
        </dependency>           
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>         
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>            
        </plugins>
    </build>

回答1:


When you start the application from Eclipse, it uses a standalone server, which is directly accessed via localhost and the URL is http://localhost:8080/api/originator.

When you run your local tomcat instance, http://localhost:8080 is the base tomcat URL. What follows next is the name of your application, in this case web_importer. So the URL becomes http://localhost:8080/web_importer/api/originator and this is why you get 404 Not Found.

It seems that your frontend is calling the API directly at http://localhost:8080/api/originator. You need to change your base API URL in your frontend configuration when you want to use the tomcat-deployed version of your API.



来源:https://stackoverflow.com/questions/54308359/after-deploying-war-file-in-tomcat-api-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!