简体   繁体   English

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

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

I have a spring-boot rest api.我有一个spring-boot rest api。

In my application.properties I have:在我的application.properties我有:

server.port=8100
server.contextPath=/api/users

There's a controller:有一个控制器:

@RestController
@RequestMapping("")
public class UserService {
    @RequestMapping(value = "", method = POST, produces = "application/json; charset=UTF-8")
    public ResponseEntity<UserJson> create(@RequestBody UserJson userJson) {
    ...
    }
}

When I call:当我打电话时:

POST http://localhost:8100/api/users/ notice the trailing slash (with user's json) - create method executes fine. POST http://localhost:8100/api/users/注意尾部斜杠(带有用户的 json) - create方法执行正常。

But when I call:但是当我打电话时:

POST http://localhost:8100/api/users without trailing slash (with user's json) - I receive 405 error: 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/" }

I need my URL without trailing slash, just .../api/users , and why it's being treated as GET method?我需要没有斜杠的 URL,只是.../api/users ,为什么它被视为GET方法?

If I change server.contextPath to server.contextPath=/api and @RequestMapping in my RestController to @RequestMapping("/users") everything works fine.如果我改变server.contextPathserver.contextPath=/api@RequestMapping在我RestController到@RequestMapping("/users")一切工作正常。 create() method is being called with and without of trailing slash at the end on the URL.在 URL 末尾使用和不使用尾随斜杠调用create()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM