【问题标题】:Spring Boot throws an error when first testing if the embedded tomcat workSpring Boot首次测试嵌入式tomcat是否工作时会抛出错误
【发布时间】:2021-02-16 13:29:01
【问题描述】:

我刚刚设置了一个 Spring Boot 应用程序,想测试一下,如果 tomcat 服务器会返回一个空白页,但是它在我的 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]\0x06l0xc3G0xfa0xcc0x8d0x0d0x00X0xca;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 类,以及以下 Controller 类:

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!";
    }

}

有谁知道这个错误来自哪里以及如何修复它?

  • 我的请求映射似乎也不起作用。

【问题讨论】:

  • 旁注:如果使用@RestController,可以省略@ResponseBody

标签: java spring spring-boot tomcat


【解决方案1】:

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

【讨论】:

  • 好的,我不知道为什么会这样,但我在其他浏览器中尝试过,它运行良好。所以总结一下:如果没有实现spring security,应该使用HTTP协议,不要用错浏览器!
  • 此外,代码中仍然存在@RequestMapping。这更多是为了语义,因为我正在从服务器请求一些东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-27
  • 2018-10-09
  • 1970-01-01
  • 2018-05-28
  • 2017-09-27
  • 1970-01-01
相关资源
最近更新 更多