Igor Simic
4 years ago

How to add prefix /api to all of your URLs in Spring JAVA


In order to wrap your all Java Spring Controllers routes with prefix "api" here is what you have to do. So, for example if you have "/user/add" route which is defined is UserController.java
...
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @GetMapping("add")
....
and to add prefix "api" you have to modify application.yml file and to set servlet contextPath param:
server:
  servlet:
    contextPath: /api
or if you do not use application.yml but application.properties, add this to your file:
server.servlet.contextPath = /api

and now all of your routes will be prefixed with /api.