简体   繁体   English

Map spring rest Z594C103F2C6E04C3D8AB059F031E0C1的路径

[英]Map spring rest controller to a path without the servlet context

I have a spring boot application with the following context path:我有一个 spring 启动应用程序,其上下文路径如下:

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 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.更改“server.servelt.context-path”值不是一种选择。

this is what I tried and didn't work:这是我尝试过但没有奏效的方法:

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

Nero, you say you can't change the "server.servlet.context-path" value. Nero,你说你不能改变“server.servlet.context-path”的值。 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.我敢打赌你这么说是因为你不想破坏 API,但我认为你可以在不破坏 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".然后在您的应用程序中将“api”映射(我假设当前为“/”)更改为“api”。

  1. Change server.servlet.context-path:/api to server.servlet.context-path:/ or maybe server.servlet.context-path: (no slash).server.servlet.context-path:/api更改为server.servlet.context-path:/或者server.servlet.context-path: (没有斜线)。 (Supposedly this is the default so you might just remove this entry altogether.) (假设这是默认设置,因此您可以完全删除此条目。)

  2. Somewhere in your application change @RequestMapping("/") to @RequestMapping("/api") .在您的应用程序中的某处将@RequestMapping("/")更改为@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 .现在您还可以拥有@GetMapping(value="/signout")并且您将在http://localhost:8080/logouthttp://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.在 go 的上下文根之外,在该应用程序中可能是不可能的。 Maybe you can create a separate Rest service app for that particular url and take it from there.也许您可以为特定的 url 创建一个单独的 Rest 服务应用程序并从那里获取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM