在Accept标头中没有“application/json”属性的Apache Camel REST API上的HTTP请求导致错误

mhd8tkvw  于 2022-11-07  发布在  Apache
关注(0)|答案(1)|浏览(125)

我正在使用Apache Camel REST API运行Sping Boot 应用程序。它按我预期的方式工作,除了当我发送接受头中没有“application/json”属性的HTTP GET请求时,应用程序引发错误,并返回代码为200和空正文的HTTP响应。我希望应用程序无论如何都能发送正确的响应。
我无法找到在Sping Boot 中管理此行为的方法。
基本路由类:

import io.patriot_framework.virtual_smart_home.AppConfig;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.rest.RestBindingMode;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;

/**
 * Routing configuration for localhost:8080.
 */
@Component
public class BaseRoute extends RouteBuilder {

    private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

    @Override
    public void configure() throws Exception {
        restConfiguration()
                .component("servlet")
                .host("localhost").port(8080)
                .bindingMode(RestBindingMode.auto);
    }
}

房屋路线:

import io.patriot_framework.virtual_smart_home.house.House;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

/**
 * House endpoint which allows HTTP GET request only and returns information
 * about the whole {@code House} object.
 */
@Component
public class HouseRoute extends BaseRoute {

    @Autowired House house;

    @Override
    public void configure() throws Exception {
        rest(getRoute())
                .get()
                    .produces(MediaType.APPLICATION_JSON_VALUE)
                    .route()
                    .process(exchange -> exchange.getMessage().setBody(house))
                    .endRest();
    }

    protected String getRoute() {
        return "house/";
    }
}

错误:

2022-07-01 09:45:04.057 ERROR 8403 --- [nio-8080-exec-2] o.a.c.c.s.CamelHttpTransportServlet      : Error processing request

java.io.IOException: Stream closed
    at org.apache.catalina.connector.InputBuffer.throwIfClosed(InputBuffer.java:527) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:338) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:132) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:110) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at org.apache.camel.util.IOHelper.copy(IOHelper.java:180) ~[camel-util-3.9.0.jar!/:3.9.0]
    at org.apache.camel.util.IOHelper.copy(IOHelper.java:148) ~[camel-util-3.9.0.jar!/:3.9.0]
    at org.apache.camel.http.common.DefaultHttpBinding.copyStream(DefaultHttpBinding.java:493) ~[camel-http-common-3.9.0.jar!/:3.9.0]
    at org.apache.camel.http.common.DefaultHttpBinding.doWriteDirectResponse(DefaultHttpBinding.java:559) ~[camel-http-common-3.9.0.jar!/:3.9.0]
    at org.apache.camel.http.common.DefaultHttpBinding.doWriteResponse(DefaultHttpBinding.java:432) ~[camel-http-common-3.9.0.jar!/:3.9.0]
    at org.apache.camel.http.common.DefaultHttpBinding.writeResponse(DefaultHttpBinding.java:355) ~[camel-http-common-3.9.0.jar!/:3.9.0]
    at org.apache.camel.http.common.CamelServlet.afterProcess(CamelServlet.java:349) ~[camel-http-common-3.9.0.jar!/:3.9.0]
    at org.apache.camel.http.common.CamelServlet.doExecute(CamelServlet.java:327) ~[camel-http-common-3.9.0.jar!/:3.9.0]
    at org.apache.camel.http.common.CamelServlet.doService(CamelServlet.java:214) ~[camel-http-common-3.9.0.jar!/:3.9.0]
    at org.apache.camel.http.common.CamelServlet.service(CamelServlet.java:130) ~[camel-http-common-3.9.0.jar!/:3.9.0]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.46.jar!/:na]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.5.jar!/:5.3.5]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.5.jar!/:5.3.5]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.5.jar!/:5.3.5]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.5.jar!/:5.3.5]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:97) ~[spring-boot-actuator-2.4.7.jar!/:2.4.7]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.5.jar!/:5.3.5]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.5.jar!/:5.3.5]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.5.jar!/:5.3.5]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1707) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[na:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[na:na]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.46.jar!/:na]
    at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
6qqygrtg

6qqygrtg1#

出于某种原因,当我将bindingModeRestBindingMode.auto更改为RestBindingMode.json时,它起了作用。

相关问题