簡體   English   中英

Spring-boot 休息 api。 截斷尾部斜杠

[英]Spring-boot rest api. Truncate trailing slash

我有一個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方法?

如果我改變server.contextPathserver.contextPath=/api@RequestMapping在我RestController到@RequestMapping("/users")一切工作正常。 在 URL 末尾使用和不使用尾隨斜杠調用create()方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM