Video in progress

Junior 🌱 Spring

What do the server.port and server.servlet.context-path settings do?

server.port is the port the embedded server listens on (default 8080). context-path adds a common prefix in front of all URLs, for example /shop.

📖 Answer

server:
  port: 9090
  servlet:
    context-path: /shop

Now an endpoint @GetMapping("/api/orders") is served at http://localhost:9090/shop/api/orders.

  • server.port=0: a free random port is picked; handy in tests when several applications run at once;
  • you can also set the port with an environment variable: SERVER_PORT=8081 java -jar app.jar;
  • the "Port 8080 was already in use" error means another process is running on that port. On macOS/Linux you find it with lsof -i :8080.

context-path is usually needed when several applications sit under one domain behind a reverse proxy (Nginx, a gateway). If you add it, put the prefix in the frontend, the health check and the proxy configuration too, otherwise you get 404 everywhere.