具有自定义上下文路径的执行器的PCF集成

时间:2018-11-20 18:42:41

标签: spring spring-mvc spring-boot spring-boot-actuator

真的在这里挣扎。 我们正在PCF上运行一些Spring Boot服务(1.5.x)。现在,我尝试通过执行器使PCF集成工作。 这给我带来了超出我预期的麻烦。问题在于,我们的许多服务都具有自定义server.contextPath(例如/ api / vX)。通过这种设计,我们现在在此contextPath上也有/ cloudfoundryapplication端点(很遗憾,PCF无法找到它)。

我已经在Spring Boot Repository中发现了这个问题。 这是描述问题并提供修复程序https://github.com/spring-projects/spring-boot/issues/9081的问题的链接。

@Bean
public TomcatEmbeddedServletContainerFactory servletContainerFactory() {
    return new TomcatEmbeddedServletContainerFactory() {
        protected void prepareContext(Host host, ServletContextInitializer[] initializers) {
            super.prepareContext(host, initializers);
            StandardContext child = new StandardContext();
            child.addLifecycleListener(new FixContextListener());
            child.setPath("/cloudfoundryapplication");
            ServletContainerInitializer initializer = ActuatorConfig.this.getServletContextInitializer(this.getContextPath());
            child.addServletContainerInitializer(initializer, Collections.emptySet());
            child.setCrossContext(true);
            host.addChild(child);
        }
    };
}

private ServletContainerInitializer getServletContextInitializer(String contextPath) {
    return (c, context) -> {
        Servlet servlet = new GenericServlet() {
            public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
                ServletContext context = req.getServletContext().getContext(contextPath);
                context.getRequestDispatcher("/cloudfoundryapplication").forward(req, res);
            }
        };
        context.addServlet("cloudfoundry", servlet).addMapping(new String[]{"/*"});
    };
}

我尝试过-而且很好。 现在的问题是,当我创建该Bean时,访问HttpServletResponse的RestControllers的行为有所不同。我们的集成测试直接发现的一种情况是我们无法设置例如通过setter的contentType。 例如

public ResponseEntity<byte[]> getSomeImage(HttpServletResponse response, ..){
    ... getData
    response.setContentType("image/png");
    return ResponseEntity.ok().body(bytes);
}

被忽略:IntegrationTest中的错误是

Expected content-type "image/png" doesn't match actual content-type "application/octet-stream;charset=UTF-8".

没有bean,它就像以前一样工作。

0 个答案:

没有答案