spring调试:解析http请求头restcontroller时出错

wztqucjr  于 2021-09-29  发布在  Java
关注(0)|答案(2)|浏览(680)

目前我面临的问题是,我无法在我的rest控制器中调用任何不是logincontroller的http方法。。
testrestcontroller。。。。

package com.sportsapplication.rest;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(path = "/api")
public class TestController {

    //TODO :

    @GetMapping(path = "/test")
    public ResponseEntity test()
    {
        return ResponseEntity.ok("Test");
    }
}

restlogincontroller

package com.sportsapplication.rest;

import com.sportsapplication.dao.training.WorkoutPlan;
import com.sportsapplication.service.CrudServiceCollection;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/api/login")
public class RestLoginController {

    CrudServiceCollection crudServiceCollection;

    public RestLoginController (CrudServiceCollection crudServiceCollection) {
        this.crudServiceCollection = crudServiceCollection;
    }

    //eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwYXNzd29yZCI6IiQyYSQxMCQ3T2ZaLkpNQUFNbFlCTWhIc0dZR2NPWGxFbVd3LlFzWG5VMWE4NXUwQmd2OWpHd2JXcWlpZSIsImV4cCI6MTYxOTg0MzQyMSwidXNlcm5hbWUiOiJ0b21pdGVzdEBPVVRMT09LIn0.qJ60x9Edbo1OtaWwsk2hpGKg5RQmr3YCNHy17AaJcWA
    @RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> login (HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {

        System.out.println();

        return ResponseEntity.ok( httpServletResponse.getHeader("Authorization"));
    }

    @GetMapping(path = "test", produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity<WorkoutPlan> showWorkoutById (

    ) throws Exception {

        WorkoutPlan workoutPlan = crudServiceCollection.workoutPlanService.findById(1L);
        if (workoutPlan == null)
            throw new Exception();

        return ResponseEntity.ok(workoutPlan);

    }
}

我得到以下错误。代码没有引发任何异常,如果在application.properties中启用log.level debug,则会出现此日志。
是否有人遇到此错误并知道如何修复它?
我想得到任何帮助,谢谢你们!
调试日志:

2021-07-15 19:01:15.798 DEBUG 16795 --- [-192.168.178.59] sun.rmi.transport.tcp                    : RMI TCP Connection(47)-192.168.178.59: (port 61101) op = 80
2021-07-15 19:01:15.803 DEBUG 16795 --- [-192.168.178.59] sun.rmi.transport.tcp                    : RMI TCP Connection(48)-192.168.178.59: (port 61101) op = 80
2021-07-15 19:01:16.726 DEBUG 16795 --- [-192.168.178.59] sun.rmi.transport.tcp                    : RMI TCP Connection(5)-192.168.178.59: (port 61101) connection closed
2021-07-15 19:01:16.726 DEBUG 16795 --- [-192.168.178.59] sun.rmi.transport.tcp                    : RMI TCP Connection(5)-192.168.178.59: close connection
2021-07-15 19:01:18.771 DEBUG 16795 --- [o-8035-Acceptor] o.apache.tomcat.util.threads.LimitLatch  : Counting up[http-nio-8035-Acceptor] latch=1
2021-07-15 19:01:18.771 DEBUG 16795 --- [o-8035-Acceptor] o.apache.tomcat.util.threads.LimitLatch  : Counting up[http-nio-8035-Acceptor] latch=2
2021-07-15 19:01:18.794 DEBUG 16795 --- [nio-8035-exec-2] o.a.coyote.http11.Http11InputBuffer      : Before fill(): [true], parsingRequestLine: [true], parsingRequestLinePhase: [0], parsingRequestLineStart: [0], byteBuffer.position() [0]
2021-07-15 19:01:18.794 DEBUG 16795 --- [nio-8035-exec-1] o.a.coyote.http11.Http11InputBuffer      : Before fill(): [true], parsingRequestLine: [true], parsingRequestLinePhase: [0], parsingRequestLineStart: [0], byteBuffer.position() [0]
2021-07-15 19:01:18.795 DEBUG 16795 --- [nio-8035-exec-2] o.a.tomcat.util.net.SocketWrapperBase    : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@7d1c8123:org.apache.tomcat.util.net.NioChannel@4743a30f:java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1:8035 remote=/0:0:0:0:0:0:0:1:61166]], Read from buffer: [0]
2021-07-15 19:01:18.795 DEBUG 16795 --- [nio-8035-exec-1] o.a.tomcat.util.net.SocketWrapperBase    : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@476b1abe:org.apache.tomcat.util.net.NioChannel@70d3d04e:java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1:8035 remote=/0:0:0:0:0:0:0:1:61165]], Read from buffer: [0]
2021-07-15 19:01:18.795 DEBUG 16795 --- [nio-8035-exec-2] org.apache.tomcat.util.net.NioEndpoint   : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@7d1c8123:org.apache.tomcat.util.net.NioChannel@4743a30f:java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1:8035 remote=/0:0:0:0:0:0:0:1:61166]], Read direct from socket: [403]
2021-07-15 19:01:18.795 DEBUG 16795 --- [nio-8035-exec-2] o.a.coyote.http11.Http11InputBuffer      : Received [GET /api/test HTTP/1.1
Content-Type: application/json
Accept: application/json
Host: http:localhost:8035
Connection: keep-alive
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwYXNzd29yZCI6IiQyYSQxMCRVaEkxbHgudjVaZk1HeFVZamhBR1QuRm42cmpmLy5oc1guYjU0RXAueUZqeEFxNXo2aVAxbSIsImV4cCI6MTYyNTg2MTIwMCwidXNlcm5hbWUiOiJ0b21pdGVzdEBPVVRMT09LIn0.28wGVWnPpotL68-mOz_z6cYCBnwfSTJt4mu6cTNcick

]
2021-07-15 19:01:18.795 DEBUG 16795 --- [nio-8035-exec-1] o.apache.coyote.http11.Http11Processor   : Error parsing HTTP request header

java.io.EOFException: null
    at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.fillReadBuffer(NioEndpoint.java:1231) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
    at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.read(NioEndpoint.java:1141) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
    at org.apache.coyote.http11.Http11InputBuffer.fill(Http11InputBuffer.java:795) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
    at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:359) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:261) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.39.jar:9.0.39]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-9.0.39.jar:9.0.39]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590) [tomcat-embed-core-9.0.39.jar:9.0.39]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.39.jar:9.0.39]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_241]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_241]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.39.jar:9.0.39]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_241]

2021-07-15 19:01:18.796 DEBUG 16795 --- [nio-8035-exec-1] o.apache.coyote.http11.Http11Processor   : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@476b1abe:org.apache.tomcat.util.net.NioChannel@70d3d04e:java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1:8035 remote=/0:0:0:0:0:0:0:1:61165]], Status in: [OPEN_READ], State out: [CLOSED]
2021-07-15 19:01:18.796 DEBUG 16795 --- [nio-8035-exec-1] o.apache.tomcat.util.threads.LimitLatch  : Counting down[http-nio-8035-exec-1] latch=2
2021-07-15 19:01:18.796 DEBUG 16795 --- [nio-8035-exec-1] org.apache.tomcat.util.net.NioEndpoint   : Calling [org.apache.tomcat.util.net.NioEndpoint@7fb714c4].closeSocket([org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@476b1abe:org.apache.tomcat.util.net.NioChannel@70d3d04e:java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1:8035 remote=/0:0:0:0:0:0:0:1:61165]])
2021-07-15 19:01:18.810 DEBUG 16795 --- [nio-8035-exec-2] org.apache.tomcat.util.http.Parameters   : Set query string encoding to UTF-8

堆栈跟踪:

java.io.EOFException: null
    at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.fillReadBuffer(NioEndpoint.java:1231) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
    at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.read(NioEndpoint.java:1141) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
    at org.apache.coyote.http11.Http11InputBuffer.fill(Http11InputBuffer.java:795) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
    at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:359) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:261) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.39.jar:9.0.39]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-9.0.39.jar:9.0.39]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590) [tomcat-embed-core-9.0.39.jar:9.0.39]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.39.jar:9.0.39]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_241]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_241]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.39.jar:9.0.39]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_241]

curl 请求

curl --location --request GET 'http://localhost:8035/api/test' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwYXNzd29yZCI6IiQyYSQxMCRVaEkxbHgudjVaZk1HeFVZamhBR1QuRm42cmpmLy5oc1guYjU0RXAueUZqeEFxNXo2aVAxbSIsImV4cCI6MTYyNTg2MTIwMCwidXNlcm5hbWUiOiJ0b21pdGVzdEBPVVRMT09LIn0.28wGVWnPpotL68-mOz_z6cYCBnwfSTJt4mu6cTNcick' \
--header 'Host: http:localhost:8035' \
--header 'Connection: keep-alive'

curl 请求2

curl --location --request GET 'http://localhost:8035/api/test' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwYXNzd29yZCI6IiQyYSQxMCRVaEkxbHgudjVaZk1HeFVZamhBR1QuRm42cmpmLy5oc1guYjU0RXAueUZqeEFxNXo2aVAxbSIsImV4cCI6MTYyNTg2MTIwMCwidXNlcm5hbWUiOiJ0b21pdGVzdEBPVVRMT09LIn0.28wGVWnPpotL68-mOz_z6cYCBnwfSTJt4mu6cTNcick' \
--header 'Host: http:localhost:8035' \

Postman 回应

<!doctype html>
<html lang="en">

<head>
    <title>HTTP Status 400 – Bad Request</title>
    <style type="text/css">
        body {
            font-family: Tahoma, Arial, sans-serif;
        }

        h1,
        h2,
        h3,
        b {
            color: white;
            background-color: #525D76;
        }

        h1 {
            font-size: 22px;
        }

        h2 {
            font-size: 16px;
        }

        h3 {
            font-size: 14px;
        }

        p {
            font-size: 12px;
        }

        a {
            color: black;
        }

        .line {
            height: 1px;
            background-color: #525D76;
            border: none;
        }
    </style>
</head>

<body>
    <h1>HTTP Status 400 – Bad Request</h1>
    <hr class="line" />
    <p><b>Type</b> Status Report</p>
    <p><b>Description</b> The server cannot or will not process the request due to something that is perceived to be a
        client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
    </p>
    <hr class="line" />
    <h3>Apache Tomcat/9.0.39</h3>
</body>

</html>
u4dcyp6a

u4dcyp6a1#

在你的 TestController 要么你需要添加 @ResponseBody 对其进行注解或更改 @Controller@RestController ,就像你穿的一样 LoginController .
这个 @ResponseBody 注解告诉spring返回值应该绑定到web响应主体。
错误来自调试日志,因此不太可能是您的问题。请尝试删除 --header 'Accept: application/json' 来自您的请求,因为您正在从 TestController .

dgiusagp

dgiusagp2#

改变 --header 'Host: http:localhost:8035'--header 'Host: localhost:8035'

相关问题