JavaSpringBootRESTAPI。截断尾斜杠
我有一个spring-boot
RESTAPI
在我的application.properties
中,我有:
server.port=8100
server.contextPath=/api/users
有一个控制器:
@RestController
@RequestMapping("")
public class UserService {
@RequestMapping(value = "", method = POST, produces = "application/json; charset=UTF-8")
public ResponseEntity<UserJson> create(@RequestBody UserJson userJson) {
...
}
}
当我打电话时:
POST http://localhost:8100/api/users/
注意后面的斜杠(使用用户的json)-create
方法执行得很好
但当我打电话时:
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/" }
我需要我的URL没有尾随斜杠,只有.../api/users
,为什么它被视为GET
方法
# 1 楼答案
如果我将RestController中的}方法,在URL的末尾使用或不使用尾随斜杠
server.contextPath
更改为server.contextPath=/api
,将@RequestMapping
更改为@RequestMapping("/users")
,一切都会正常工作^正在调用{