In addition to the other comments about changing the application property for the context path, you can also use an application property to set the prefix for the dispatcher servlet alone, in Spring Boot 2.3.1.
spring.mvc.servlet.path=/api
The request mappings would not change in your controllers. While context path moves the entire application to a different path, servlet path only limits the URLs that are handled by the dispatcher servlet. The servlet path is the equivalent of the servlet mapping in web.xml. Other resources that do not use
the dispatcher servlet can be accessed from any other URL.
If you have other controllers that are not mapped to the /api
prefix, then this will not work, unless you declare a second dispatcher servlet with a different prefix for those controllers.