我有一个小型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,并将其停靠,如下所示:
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
问题是,当我这样做我得到一个错误说:
org.springframework.web.请求方法'GET'不支持
然而,当我在本地运行spring应用程序(不停靠它)并从JMeter发送请求时,请求成功发送。
我做错了什么?
这背后的原因肯定在日志中。
这是我的设置,我改变了控制器,这样它就会返回json,而不是长原语:
控制器
@RequestMapping("api/factorial")
@RestController
public class TestApi {
@GetMapping
public Map<String, 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();
Map<String, Long> res = new HashMap<String, Long>();
res.put("time_in_nanos", (endTime - startTime));
return res;
}
}
应用
@SpringBootApplication(scanBasePackages = "com.example.demo")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
聚甲醛
ml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<springboot.version>2.0.8.RELEASE</springboot.version>
</properties>
<dependencies>
<!-- Springboot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>${springboot.version}</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>${springboot.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${springboot.version}</version>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${springboot.version}</version>
<executions>
<execution>
<goals>
<goal>build-info</goal>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Docker命令行
docker run --rm --name apiTest -p 8080:8080 321225ee07bb
我基本上用了你的Dockerfile
从容器中注销:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.8.RELEASE)
2020-02-04 22:55:03.774 INFO 1 --- [ main] com.example.demo.api.Application : Starting Application on 33f1f71d079b with PID 1 (/usr/src/factorial/demo-1.0.jar started by root in /usr/src/factorial)
2020-02-04 22:55:03.796 INFO 1 --- [ main] com.example.demo.api.Application : No active profile set, falling back to default profiles: default
2020-02-04 22:55:04.076 INFO 1 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2f410acf: startup date [Tue Feb 04 22:55:04 GMT 2020]; root of context hierarchy
2020-02-04 22:55:08.891 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-02-04 22:55:09.002 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-02-04 22:55:09.004 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.37
2020-02-04 22:55:09.055 INFO 1 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/lib/jvm/java-1.8-openjdk/jre/lib/amd64/server:/usr/lib/jvm/java-1.8-openjdk/jre/lib/amd64:/usr/lib/jvm/java-1.8-openjdk/jre/../lib/amd64:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib]
2020-02-04 22:55:09.305 INFO 1 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-02-04 22:55:09.307 INFO 1 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 5236 ms
2020-02-04 22:55:10.617 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2020-02-04 22:55:10.626 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'webMvcMetricsFilter' to: [/*]
2020-02-04 22:55:10.636 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2020-02-04 22:55:10.641 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2020-02-04 22:55:10.645 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2020-02-04 22:55:10.647 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpTraceFilter' to: [/*]
2020-02-04 22:55:10.649 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2020-02-04 22:55:11.290 INFO 1 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-02-04 22:55:11.658 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2f410acf: startup date [Tue Feb 04 22:55:04 GMT 2020]; root of context hierarchy
2020-02-04 22:55:11.864 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/factorial],methods=[GET]}" onto public java.util.Map<java.lang.String, java.lang.Long> com.example.demo.api.TestApi.factorial()
2020-02-04 22:55:11.879 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2020-02-04 22:55:11.880 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2020-02-04 22:55:11.960 INFO 1 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-02-04 22:55:11.962 INFO 1 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-02-04 22:55:12.745 INFO 1 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator'
2020-02-04 22:55:12.787 INFO 1 --- [ main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator/health],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
2020-02-04 22:55:12.792 INFO 1 --- [ main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator/info],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
2020-02-04 22:55:12.796 INFO 1 --- [ main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto protected java.util.Map<java.lang.String, java.util.Map<java.lang.String, org.springframework.boot.actuate.endpoint.web.Link>> org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping.links(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2020-02-04 22:55:12.950 INFO 1 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2020-02-04 22:55:13.124 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-02-04 22:55:13.135 INFO 1 --- [ main] com.example.demo.api.Application : Started Application in 10.73 seconds (JVM running for 12.233)
我要寻找的是GET请求的映射,如下所示:
2020-02-04 22:55:11.864 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/factorial],methods=[GET]}" onto public java.util.Map<java.lang.String, java.lang.Long> com.example.demo.api.TestApi.factorial()
您的应用程序部署在docker中,所以它是虚拟机。这是另一个有自己ip地址的主机。但是您正试图向localhost发送GET请求。您应该获得您的docker ip。键入此选项以在Mac OS中获得ip
主办
访问该页面https://spring.io/guides/gs/spring-boot-docker/在该知识库中有一个关于mac os部署的部分
我正在处理一个java springboot项目和rest api,我需要在其中一个uri中传递参数,当我使用它时,我会遇到“request method GET not supported”错误 当我使用时它工作正常 但我需要url有“?”在传递参数之前,所以当我替换 用这个 我得到get方法不受支持的错误。
我正在编写一个控制器来处理来自AJAX的帖子。我一直收到一个错误,那篇文章不受支持。我以前在尝试创建后控制器方法时从未遇到过以下错误,我希望有人能发现我在哪里搞砸了。 这是我为控制控制器中的帖子而编写的方法: 使用JQuery 1.10,这是请求它的Ajax调用: 我知道POST地址是正确的,因为将它转换成GET请求就可以了。此外,我还编写了其他POST请求,它们在同一个控制器类中也能正常工作。任
login.jsp 用户列表.jsp AppController.java 有2页:login.jsp-起始页,其中包括与登录和密码填充的形式-userlist.jsp结果列表“显示数据库中所有用户持久化”..首先显示登录页面,当我单击提交按钮时,我得到了这个错误:org . spring framework . web . servlet . pagenotfound-不支持请求方法“POST”
我试图在这里输入代码`@RequestMapping(value=“/test”,method=RequestMethod.POST),但错误代码为 网状物xml是 springmvc.xmlindex.jsp I input submit botton brower为错误HTTP Status 405-请求方法'GET'不受支持类型状态报告消息请求方法'GET'不受支持描述指定的HTTP方法不允
下面是我的方法 当调用GET request时,它的工作很好,但是当我们调用POST request和request payload checkout_token=xxxx时,我得到以下错误 编辑 甚至我尝试删除所有参数,但仍然没有运气到目前为止。
我有Java控制器: 并遇到以下问题:警告14068--[nio-8080-exec-3].w.s.m.s.defaultHandlerExceptionResolver:Resolved[org.springframework.web.httpRequestMethodNotSupportedException:Request method'DELETE'not support]它发生在我发送删