为什么 ContainerResponseFilter 不能与 Camel Rest 组件一起使用?

huangapple go评论88阅读模式
英文:

Why ContainerResponseFilter dosen't work with Camel Rest component?

问题

I'm using Quarkus 2.13 with Camel 3.18. I have rest endpoint set, which is working as expected:

restConfiguration()
   .component("platform-http")
   .skipBindingOnErrorCode(false)
   .bindingMode(RestBindingMode.json);

I did write ContainerResponseFilter to modify headers of outgoing response, but response is not handle via this filter:

@JBossLog
@Provider
public class CustomContainerResponseFilter implements ContainerResponseFilter {    

    @Override
    public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
            throws IOException {

        responseContext.getHeaders().add("X-Some-Header", "Hello from Response Filter");
// I want to filter out disallowed-headers
    }
}

Filter is working fine with standar controller, but when using Camel Endpoint with platform-http, response is not handle via ContainerResponseFilter.

@Path("/hello")
public class HelloController {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayHello() {
        return "Hello, Quarkus!";
    }
}

Update:

My goal is to filter out disallowed-headers. For that I've create my list and just want to remove headers which are not on the list.

英文:

I'm using Quarkus 2.13 with Camel 3.18. I have rest endpoint set, which is working as expected:

restConfiguration()
   .component("platform-http")
   .skipBindingOnErrorCode(false)
   .bindingMode(RestBindingMode.json);

I did write ContainerResponseFilter to modify headers of outgoing response, but response is not handle via this filter:

@JBossLog
@Provider
public class CustomContainerResponseFilter implements ContainerResponseFilter {    

    @Override
    public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
            throws IOException {

        responseContext.getHeaders().add("X-Some-Header", "Hello from Response Filter");
// I want to filter out disallowed-headers
    }
}

Filter is working fine with standar controller, but when using Camel Endpoint with platform-http, response is not handle via ContainerResponseFilter.

@Path("/hello")
public class HelloController {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayHello() {
        return "Hello, Quarkus!";
    }
}

Update:

My goal is to filter out disallowed-headers. For that I've create my list and just want to remove headers which are not on the list.

答案1

得分: 1

如果您想设置自定义响应头,您可以在Camel路由中执行以下操作:

public class RestRoutes extends RouteBuilder {
    @Override
    public void configure() {
        rest("/rest")
                .get()
                .to("direct:myRoute");

        from("direct:myRoute")
                .setHeader("my-custom").constant("my-value");
    }
}

或者,由于platform-http基于Vert.x Web构建,您可以使用quarkus-reactive-routes来拦截请求并向响应添加自定义头。

@RouteFilter(100)
void myFilter(RoutingContext rc) {
    rc.response().putHeader("my-custom", "my-value");
    rc.next();
}

更多信息可以在这里找到:

https://quarkus.io/guides/reactive-routes#intercepting-http-requests

英文:

If you want to set a custom response header, then you can do it in your Camel route.

public class RestRoutes extends RouteBuilder {
    @Override
    public void configure() {
        rest("/rest")
                .get()
                .to("direct:myRoute");

        from("direct:myRoute")
                .setHeader("my-custom").constant("my-value");
    }
}

Alternatively, since platform-http builds on Vert.x Web, you could use quarkus-reactive-routes to intercept requests and add a custom header to the response.

@RouteFilter(100)
void myFilter(RoutingContext rc) {
    rc.response().putHeader("my-custom", "my-value");
    rc.next();
}

More information can be found here:

https://quarkus.io/guides/reactive-routes#intercepting-http-requests

huangapple
  • 本文由 发表于 2023年7月24日 15:50:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76752372.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定