[英]Spring-boot rest api. Truncate trailing slash
I have a spring-boot
rest api.我有一个
spring-boot
rest api。
In my application.properties
I have:在我的
application.properties
我有:
server.port=8100
server.contextPath=/api/users
There's a controller:有一个控制器:
@RestController
@RequestMapping("")
public class UserService {
@RequestMapping(value = "", method = POST, produces = "application/json; charset=UTF-8")
public ResponseEntity<UserJson> create(@RequestBody UserJson userJson) {
...
}
}
When I call:当我打电话时:
POST http://localhost:8100/api/users/
notice the trailing slash (with user's json) - create
method executes fine. POST http://localhost:8100/api/users/
注意尾部斜杠(带有用户的 json) - create
方法执行正常。
But when I call:但是当我打电话时:
POST http://localhost:8100/api/users
without trailing slash (with user's json) - I receive 405
error: POST http://localhost:8100/api/users
不带斜杠(使用用户的 json) - 我收到405
错误:
{ "timestamp": 1520839904193, "status": 405, "error": "Method Not Allowed", "exception": "org.springframework.web.HttpRequestMethodNotSupportedException", "message": "Request method 'GET' not supported", "path": "/api/users/" }
I need my URL without trailing slash, just .../api/users
, and why it's being treated as GET
method?我需要没有斜杠的 URL,只是
.../api/users
,为什么它被视为GET
方法?
If I change server.contextPath
to server.contextPath=/api
and @RequestMapping
in my RestController to @RequestMapping("/users")
everything works fine.如果我改变
server.contextPath
到server.contextPath=/api
和@RequestMapping
在我RestController到@RequestMapping("/users")
一切工作正常。 create()
method is being called with and without of trailing slash at the end on the URL.在 URL 末尾使用和不使用尾随斜杠调用
create()
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.