war in tomcat with apache cxf jax-rs not working

1.1k views Asked by At

I have a rest service that does not work from tomcat container.Bellow are my pom.xml and classes.I would really appreciate some help.

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<artifactId>testcxf</artifactId>    
<packaging>war</packaging>

testcxf

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
        <version>3.2.2</version>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

the spring boot class:

@SpringBootApplication
public class TestcxfApplication extends SpringBootServletInitializer{

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Application.class);
}

public static void main(String[] args) {
    SpringApplication.run(TestcxfApplication.class, args);
}
}

the server class:

@Configuration
public class webs {
@Autowired
private Bus bus;

@Bean
public Server cxfRestServer(Hello hello) {
    JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean();
    factory.setBus(bus);
    factory.setProvider(new JacksonJsonProvider());
    factory.setServiceBean(hello);
    factory.setAddress("/rest");
    return factory.create();
}   
}

and the hello class.

The log is like this:

Starting TestcxfApplication v1.5.10.RELEASE on Marius with PID 34204 (started by MARIUS$ in C:\Program Files\Apache Software Foundation\Tomcat 8.5) No active profile set, falling back to default profiles: default Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4c65df69: startup date [Mon Feb 12 16:17:09 EET 2018]; root of context hierarchy Root WebApplicationContext: initialization completed in 288 ms Mapping filter: 'errorPageFilter' to: [/*] Started TestcxfApplication in 0.879 seconds (JVM running for 4.531)

It says started but I can't access resources.I get 404 resources not found.

1

There are 1 answers

0
ootero On

This shouldn't be needed:

@Configuration
public class webs {
@Autowired
private Bus bus;

@Bean
public Server cxfRestServer(Hello hello) {
    JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean();
    factory.setBus(bus);
    factory.setProvider(new JacksonJsonProvider());
    factory.setServiceBean(hello);
    factory.setAddress("/rest");
    return factory.create();
}   
}

Let the CXF Spring Boot starter to handle it. Try also adding:

<dependency>
  <groupId>org.apache.cxf</groupId>
  <artifactId>cxf-rt-rs-service-description</artifactId>
  <version>${cxf.version}</version>
</dependency>

and this config in application.yml:

# Spring MVC dispatcher servlet path needs to be different than CXF's
server.servlet-path: /

# http://cxf.apache.org/docs/springboot.html#SpringBoot-SpringBootCXFJAX-RSStarter
cxf:
  path: /api # CXFServlet URL pattern
  jaxrs:
    component-scan: true

I have detailed this configuration / setup in my blog at Implementing APIs using Spring Boot, CXF and Swagger.