有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaSpringBootRESTAPI。截断尾斜杠

我有一个spring-bootRESTAPI

在我的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方法


共 (1) 个答案

  1. # 1 楼答案

    如果我将RestController中的server.contextPath更改为server.contextPath=/api,将@RequestMapping更改为@RequestMapping("/users"),一切都会正常工作^正在调用{}方法,在URL的末尾使用或不使用尾随斜杠