Map spring rest controller to a path without the servlet context

Viewed 1084

I have a spring boot application with the following context path:

server.servlet.context-path:/api

I need to write a rest controller that's mapped to http://localhost:8080/logout instead of http://localhost:8080/api/logout

Is there a way to achieve this? changing the "server.servelt.context-path" value is not an option.

this is what I tried and didn't work:

@GetMapping(value="../signout"){
public void logout(){
}
2 Answers

Nero, you say you can't change the "server.servlet.context-path" value. I bet you say this because you don't want to break the API, but I think you can manage to change this without breaking the API. Set the context-path to blank, which is permitted. Then in your application change the "api" mapping, which I assume is currently "/", to "api".

  1. Change server.servlet.context-path:/api to server.servlet.context-path:/ or maybe server.servlet.context-path: (no slash). (Supposedly this is the default so you might just remove this entry altogether.)

  2. Somewhere in your application change @RequestMapping("/") to @RequestMapping("/api").

Now you can also have @GetMapping(value="/signout") and you will have resources at http://localhost:8080/logout and http://localhost:8080/api.

I don't know what mapping annotations you happen to be using, but hopefully this is clear enough.

It may not be possible within that application to go outside its context root. Maybe you can create a separate Rest service app for that particular url and take it from there.

Related