Add contextpath param to @GetMapping

davisoski :

I'm trying to create my routes without depending on the server.contextPath in application.properties

This is an example:

@PreAuthorize("hasRole('ROLE_ADMIN') 
@GetMapping("/dashboard/admin/list/param1/{param1}")
public String method(@PathVariable String param1, Model model, HttpServletRequest request) {

  //Some stuff

  String contextPath = request.getContextPath();
  return contextPath + "/dashboard/admin/list";
}

but as expected the view is not found because of the contextPath added.

If I make a redirect like this:

String contextPath = request.getContextPath();
String redirect = contextPath + "/dashboard/admin/list";
return "redirect:/dashboard/admin/directorio/list";

everything works great, but sometime I don't need to redirect.

The idea behind, is to follow the process to deploy as a war file in tomcat as I asked in this link: How to get context path in controller without set in application.properties

So the question is: Is possible to add some param in @GetMapping to add the contextPath

UPDATE 1

I'm not sure what you are asking.

Lets say I create two war projects from the same project called webapp1 and webapp2 and I deploy in my tomcat server.

I can acces both projects like this:

http://localhost:8080/webapp1/dashboard/admin/list/param1/100

http://localhost:8080/webapp2/dashboard/admin/list/param1/200

but when I make the return to my thymeleaf page located in src/main/resources/templates/dashboard/admin/list.html the page is not found (that's the error), because in the method the @GetMapping cant find the contextPath which could be webapp1 or webapp2.

I don't want to use server.contextPath because in that case I think you can have just one project with the name of the server.contextPath.

Thanks

cool cool :

Spring maintains it context path so you do not have to worry about that. Your code looks fine.

what you can try.

Try to remove server.contextPath line completely from application.properties file. stop server clean and build, restart the server and launch application again.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=467134&siteId=1