Spring Boot Deployed in Tomcat gives 404 but works Stand-alone

前端 未结 3 489
孤城傲影
孤城傲影 2021-01-04 09:29

I have been testing Spring Boot with embedded Tomcat for about a month now to build a REST API. Everything was working fine. We now want to deploy the API in a separate deve

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-04 09:48

    When running an application the path to call consists of a couple of parts.

    The first is the base URL on which the application is deployed, in your case that is /sophia.

    The second is the servlet mapping of the DispatcherServlet in your case that is /sohpia/*.

    The third is the mapping of the controller inside the DispatcherServlet, in your example that is /users.

    All those things combined create the URL /sophia/sophia/users.

    The difference between the deployment as a WAR is that you included a separate URL to deploy on, when running as a jar it, by default, is deployed to / (the root).

    You could fix it by putting /sophia as the server.context-path in the application.properties and map the DispatcherServlet to /* or /. That will in both situations give you the URL you want (and expected).

提交回复
热议问题