我有一个小型 Spring 应用程序,如下所示:
package com.example.demo.api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("api/factorial")
@RestController
public class TestApi {
@GetMapping
public long factorial(){
long startTime = System.nanoTime();
int i;
double fact=1;
int number=50;//It is the number to calculate factorial
for(i=1;i<=number;i++){
fact=fact*i;
System.out.println("Factorial of "+i+" is: "+fact);
}
long endTime = System.nanoTime();
return (endTime - startTime);
}
}
我已经生成了该应用程序的 jar 并将其进行了 docker 化,如下所示:
FROM openjdk:8-jdk-alpine
ADD ./demo-0.0.1-SNAPSHOT.jar /usr/src/factorial/
WORKDIR /usr/src/factorial
EXPOSE 8080
CMD java -XX:+PrintFlagsFinal $JAVA_OPTIONS -jar demo-0.0.1-SNAPSHOT.jar
然后我运行以下命令来运行 docker 容器
docker run --rm --name factorialContainer -p 8080:8080 -e JAVA_OPTIONS="$(cat jvmFlags.txt)" suleka96/factorial:latest
然后我尝试使用 JMeter 向 REST 端点发出 HTTP 请求,如下所示
问题是,当我这样做时,我收到一条错误消息:
org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
但是,当我在本地运行 spring 应用程序时(没有对其进行 Docker 化) 并从JMeter发送请求,请求成功发送。
我做错了什么?
最佳答案
您的应用程序部署在docker中,因此它是虚拟机。那是另一台拥有自己 IP 地址的主机。但您正在尝试向本地主机发送 GET 请求。你应该得到你的 docker ip。 在 Mac 操作系统中键入此命令可获取 IP
主机> docker-machine ip 默认
访问该页面https://spring.io/guides/gs/spring-boot-docker/该知识库中有一个关于 macOS 部署的部分
关于java - Dockerized SpringBoot REST api 不支持请求方法 'GET',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60048198/