Using CXF with Spring Boot Actuator
Asked Answered
I

5

7

I am working on a web service host application in which using cxf with spring boot. when I register cxf servlet with following code web service side works and I can see published wsdls.

However after setting cxf servlet Spring boot actuator and rest endpoints not working and returning 404. How can I solve this issue ?

@Bean
public ServletRegistrationBean cxfServlet() {
    return new ServletRegistrationBean(new CXFServlet(), "/soap-api/*");
}
Industry answered 29/6, 2016 at 6:13 Comment(2)
Do you still have spring-webmvc on the classpath? It's usually pulled in via a dependency on spring-boot-starter-web.Fro
When It was not working I added spring-webmvc, but it didnt resolve error.Industry
I
1

Although I dont know the reason, when I set a name like below it starts working.

@Bean
public ServletRegistrationBean cxfServlet() {
    ServletRegistrationBean cxf = new ServletRegistrationBean(new CXFServlet(), "/soap-api/*");
    cxf.setName("cxfServlet");
    return cxf;
}
Industry answered 29/6, 2016 at 8:5 Comment(1)
Hi, can you share a working example? Java has many alternatives for web services. I tried as much as Edison and still no solution. Thank you in advance.Commend
V
1

Here is simple spring boot configuration I use.

@Configuration
@Import(value = { JaxRsConfig.class })
public class CxfRestConfig {

    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        return new ServletRegistrationBean(new CXFServlet(), "/cxf/*");
    }

    @Component
    public class CustomSpringComponentScanServer
            extends AbstractSpringComponentScanServer {

        @Override
        protected String getAddress() {
            return "/api";
        }

        @Bean
        public Server jaxRsServer() {
            super.getFeatures().add(new LoggingFeature());
            return super.createJaxRsServer();
        }

    }

}

Note: With ComponentScanner you need to annotate your service class with Spring annotations along with @Path Annotation at class level.

If you do not want list of apis in http://localhost:8080/cxf you can directly remove the custom class I had written and you can import directly as shown below.

@Import(value = { JaxRsConfig.class, SpringComponentScanServer.class }) 
Vlad answered 29/6, 2016 at 14:7 Comment(0)
M
1

I was getting the same problem with Kotlin and this post indirectly helped me. My code was like this

@Bean
fun dispatcherServlet(): ServletRegistrationBean<CXFServlet>? {
    return ServletRegistrationBean(CXFServlet(), "/*")
}

After changing the method name from dispatcherServlet to cxfServlet the actuator magically started to work.

@Bean
fun cxfServlet(): ServletRegistrationBean<CXFServlet>? {
    return ServletRegistrationBean(CXFServlet(), "/*")
}

I guess it was conflicting with some Spring default servlet.

Marras answered 26/7, 2021 at 1:14 Comment(1)
Applies to Java (of course) but that was the only change that made actuator work again for me.Vorous
U
0

I looks like there is a clash between servlets.

You can check it in your logs. There should be:

2017-04-01 15:34:04,029 [restartedMain] INFO  o.s.b.w.s.ServletRegistrationBean - Mapping servlet: 'CXFServlet' to [/soap-api/*]

2017-04-01 15:34:04,031 [restartedMain] INFO  o.s.b.w.s.ServletRegistrationBean - Mapping servlet: 'dispatcherServlet' to [/]

There should be exactly two servlets and the path should be different.

If there is one missing the enpoints won't work.

dispatcherServlet is spring default one to handle actuator metrics

Uninstructed answered 1/4, 2017 at 13:52 Comment(0)
U
0

Its works for me

  @Bean
  @Primary
  public DispatcherServletPath dispatcherServletPath() {
    return () -> "";
  }
  
  @Bean
  public DispatcherServlet dispatcherServletActuator() {
    return new DispatcherServlet();
  }

  @Bean
  public ServletRegistrationBean<CXFServlet> dispatcherServlet() {
    ServletRegistrationBean<CXFServlet> cxf = new ServletRegistrationBean<>(new CXFServlet(), "/v1/*");
    cxf.setName("cxfServlet");
    return cxf;
    
  }
Underexpose answered 23/2, 2024 at 16:4 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Epiboly

© 2022 - 2025 — McMap. All rights reserved.