使用自定义注释在方法级别中添加前缀或后缀在方法级别中删除控制器-Spring Rest Controller
我试图在控制器的方法级别上在get,post,put,删除映射的基础上添加一个前缀或后缀。
控制器类
@RestController
@RequestMapping("/something")
public class SomeController {
@PutMapping("/some/path/")
public ResponseEntity<ApiResponse<String>> someMethod() {
....
}
...
}
,因此,基本上,上述请求URL应该是: http:// localhost:8080/sosings/some/some/path/path/
现在,我只想添加一些前缀或后缀请求URL,就像: http:// localhost:8080/themes/read/some/path/path/path/strong>或 http:// localhost:8080/sosings/some/some/some/some/path /read/额外的“/读”,需要将其作为前缀或后缀添加到请求URL中。我可以通过将其添加到putmapping值中直接做到这一点,但是我想使用 @read (例如
,更新的控制器类)
@RestController
@RequestMapping("/something")
public class SomeController {
@Read
@PutMapping("/some/path/")
public ResponseEntity<ApiResponse<String>> someMethod() {
....
}
...
}
的注释来对其进行装饰,并且更新的请求URL将是相同的方式喜欢: http:// localhost:8080/something/read/some/path/
我找不到更好的方法。到目前为止,我只使用自定义注释添加了类级前缀。
任何人都可以帮助您满足上述要求吗? 谢谢 !!
我也很想知道是否可以实现?
I am trying to add a prefix or suffix in the controller at the method level on top of GET, POST, PUT, DELETE mappings.
Controller class
@RestController
@RequestMapping("/something")
public class SomeController {
@PutMapping("/some/path/")
public ResponseEntity<ApiResponse<String>> someMethod() {
....
}
...
}
So, basically, the above request URL should be something like : http://localhost:8080/something/some/path/
Now, I just want to add some prefix or suffix whatever is feasible to the request URL which will be something like : http://localhost:8080/something/read/some/path/ or http://localhost:8080/something/some/path/read/ the extra "/read" which needs to be added to the request URL as a prefix or suffix. I can do this directly by adding this to the PutMapping value, but I want to decorate it somewhat using annotation like @Read
So, the updated Controller class will be like
@RestController
@RequestMapping("/something")
public class SomeController {
@Read
@PutMapping("/some/path/")
public ResponseEntity<ApiResponse<String>> someMethod() {
....
}
...
}
and the same way updated request URL will be like : http://localhost:8080/something/read/some/path/
I am unable to find a better way to do this. Till now I have only achieved adding a class-level prefix using custom annotation.
Can anyone please help with the above requirement.?
Thank you !!
I am also curious to know whether is achievable or not even?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用这样的路径扩展使您的代码不太可理解。 (也许您应该阅读更多有关RESTFUL API的信息),但是Spring几乎可以完成您想要的一切。
扩展点在这里,您可以像想要的那样更改路径。
示例:
请求:http:// localhost:8080/books/asd
响应:404
输出:
请求:http:// localhost:8080/books/books/books/asd/read/read
wendment:asd
upputies appection:
dependencies
application.properties.properties.properties
Using such way of path extension make your code less understandable. (maybe you should read more about RESTful API) But spring can do almost everything you want.
Extension point is here, you can change path like you want.
Example:
request: http://localhost:8080/books/asd
response: 404
output:
request: http://localhost:8080/books/asd/read
response: asd
output:
dependencies
application.properties