Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we run multiple spring boot applications in same port? If not, why we have the context path option in application.properties?

server.port=8080
server.contextPath=/myapp

Consider the above configuration in the application.properties of a spring boot application. What is the need of contextPath if we can't really run another spring boot application in the same port 8080. Can anyone provide some real time scenarios where this contextPath useful?

[edit] In my experience, when we deploy multiple web applications (web applications contexts) to single tomcat instance running on 8080 port the context path is used to distinguish the applications. This is what kept me thinking about the possibility of running multiple spring applications in a single port. Otherwise, I don't see the value of having an option to change the contextPath. Also considering the fact that we will have nginx or apache as a web server front and this spring applications are going to be run only inside the firewall.

like image 594
Ramachandran RajaGopal Avatar asked Apr 29 '19 11:04

Ramachandran RajaGopal


2 Answers

  1. No you can have only one application listening on a specific port at a time.

  2. Changing the context path can be helpful in order to globally add a prefix to all your controller endpoints. For instance if your application is serving rest API and you want all the urls to start with /api then one option would be to add this prefix to each request mapping (for example @RequestMapping("/api/user)), or change the context path to /api then you can have your request mapping now as @RequestMapping("/user)

like image 172
Ahmed Abdelhady Avatar answered Oct 06 '22 23:10

Ahmed Abdelhady


Nope same PORT cannot be used by multiple process/applications

like image 34
RAJKUMAR NAGARETHINAM Avatar answered Oct 06 '22 23:10

RAJKUMAR NAGARETHINAM