【发布时间】:2021-02-17 03:14:39
【问题描述】:
我正在将我的 Spring Boot 应用程序从 1.5.x 迁移到 2.x 我在没有尾随的情况下对上下文路径进行 POST 调用时收到 405 错误 /
{
"timestamp": "2020-11-04T12:07:19.065+0000",
"status": 405,
"error": "Method Not Allowed",
"message": "Request method 'GET' not supported",
"path": "/hello/"
}
下面是我的代码
application.properties:
spring:
application:
name: hello-world-service
server:
servlet:
context-path: /hello
port: 8082
HelloWorldController.java
@RestController
@RequestMapping(value = "/")
public class HelloWorldController {
@PostMapping
public ResponseEntity<String> helloWorld(@RequestBody HelloDto helloDto){
return ResponseEntity.ok(helloDto.getName());
}
}
HelloDto.java
@Data
public class HelloDto {
private String name;
}
春季开机版本:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.11.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
当我在 http://localhost:8082/test 上进行 POST 调用时,我得到的 405 状态是下面的截图
但是当我在 http://localhost:8082/test/ 上进行 POST 调用时,它工作正常。下面是截图
有什么方法可以处理我们调用localhost:8082/test 会得到与调用localhost:8082/test/ 相同的结果
【问题讨论】:
-
可能是
http://localhost:8082/hello/而不是http://localhost:8082/test/。 -
是的,我的问题已解决,请在下方查看我的回答
标签: java spring-boot spring-mvc contextpath spring-boot-2