【发布时间】:2018-08-20 04:13:12
【问题描述】:
我有一个spring-boot rest 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 方法?
【问题讨论】:
-
看起来您的 HTTP 服务器正在重写不带斜杠的请求并发送重定向,用 GET 代替 POST。检查您的服务器配置。
-
@JimGarrison 你指的是什么配置?我以
mvn spring-boot:run启动我的应用程序,因此它是嵌入式Tomcat。我没有任何额外的配置。 -
嗯...基于this question 看起来它不是为进行重定向而设计的......所以这超出了我的专业知识。对不起。
-
我从这里找到了答案。 stackoverflow.com/a/45258671/331946
-
@htshame 删除“更新”部分+将其添加为答案!
标签: java rest spring-boot request-mapping