我有一个spring-boot
的休息API。
在我的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
方法?
如果我将server.contextPath
更改为server.contextPath=/api
并在我的 RestController 中@RequestMapping
以@RequestMapping("/users")
一切正常。 调用create()
方法时,URL 末尾有和没有尾部斜杠。