弹簧启动休息 api.截断尾部斜杠



我有一个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 末尾有和没有尾部斜杠。

最新更新