Spring 首次测试嵌入式 tomcat 是否工作时,启动会抛出错误

Spring Boot throws an error when first testing if the embedded tomcat work

我刚刚设置了一个 Spring 引导应用程序并想测试,如果 tomcat 服务器将 return 一个空白页面,而是抛出以下错误消息在我的 IDE:

  .   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.2)

2021-02-16 14:21:45.297  INFO 6956 --- [           main] com.test.gifly.GiflyApplication          : Starting GiflyApplication using Java 15.0.2 on LAPTOP-LJRSO4L6 with PID 6956 (D:\Coding\gifly\build\classes\java\main started by Dominik Lovetinsky in D:\Coding\gifly)
2021-02-16 14:21:45.302  INFO 6956 --- [           main] com.test.gifly.GiflyApplication          : No active profile set, falling back to default profiles: default
2021-02-16 14:21:46.102  INFO 6956 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-02-16 14:21:46.119  INFO 6956 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-02-16 14:21:46.120  INFO 6956 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-02-16 14:21:46.211  INFO 6956 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-02-16 14:21:46.212  INFO 6956 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 859 ms
2021-02-16 14:21:46.367  INFO 6956 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-02-16 14:21:46.545  INFO 6956 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-02-16 14:21:46.554  INFO 6956 --- [           main] com.test.gifly.GiflyApplication          : Started GiflyApplication in 1.607 seconds (JVM running for 2.285)
2021-02-16 14:22:00.025  INFO 6956 --- [nio-8080-exec-1] o.apache.coyote.http11.Http11Processor   : Error parsing HTTP request header
 Note: further occurrences of HTTP request parsing errors will be logged at DEBUG level.

java.lang.IllegalArgumentException: Invalid character found in method name [0x160x030x010x020x000x010x000x010xfc0x030x030x870x97`F0xe7Q[0x960x02<0xe40xde][=12=]x06l0xc3G0xfa0xcc0x8d0x0d0x00X0xca;V0x160x9a0xef0xca0xc1]. HTTP method names must be tokens
    at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:417) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:261) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:888) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1597) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) ~[na:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) ~[na:na]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
    at java.base/java.lang.Thread.run(Thread.java:832) ~[na:na]

2021-02-16 14:22:00.025  INFO 6956 --- [nio-8080-exec-2] o.apache.coyote.http11.Http11Processor   : Error parsing HTTP request header
 Note: further occurrences of HTTP request parsing errors will be logged at DEBUG level.

我已经指定的唯一代码是主要 java class 和以下控制器 class:

package com.test.gifly.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class GifController {

    @RequestMapping(value = "/")
    @ResponseBody
    public String listGifs(){
        return "List fo all the GIFs!";
    }

}

有谁知道这个错误从何而来以及如何解决?

您可以尝试将@RequestMapping 更改为@GetMapping 吗?还要检查您是否要访问以 https 而不是 http

开头的网站