Issues/Troubleshooting

Issue: ‘jaxRsServer’ threw an exception?! No resource classes found?

April 28, 2016

Resolving “Failed to instantiate [org.apache.cxf.endpoint.Server]: Factory method ‘jaxRsServer’ threw exception;…”!

You may have come across something like this when experimenting with Spring Boot, Apache CXF and Gradle:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jaxRsServer' defined in class path resource [org/apache/cxf/jaxrs/spring/SpringComponentScanServer.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.cxf.endpoint.Server]: Factory method 'jaxRsServer' threw exception; nested exception is org.apache.cxf.service.factory.ServiceConstructionException
     at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
     at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
     at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
     at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
     at com.trek.Application.main(Application.java:10) [bin/:na]
 Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.cxf.endpoint.Server]: Factory method 'jaxRsServer' threw exception; nested exception is org.apache.cxf.service.factory.ServiceConstructionException
     at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
     at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
     ... 18 common frames omitted
 Caused by: org.apache.cxf.service.factory.ServiceConstructionException: null
     at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:219) ~[cxf-rt-frontend-jaxrs-3.1.6.jar:3.1.6]
     at org.apache.cxf.jaxrs.spring.AbstractSpringConfigurationFactory.createJaxRsServer(AbstractSpringConfigurationFactory.java:54) ~[cxf-rt-frontend-jaxrs-3.1.6.jar:3.1.6]
     at org.apache.cxf.jaxrs.spring.SpringComponentScanServer.jaxRsServer(SpringComponentScanServer.java:29) ~[cxf-rt-frontend-jaxrs-3.1.6.jar:3.1.6]
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_73]
     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_73]
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_73]
     at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_73]
     at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
     ... 19 common frames omitted
 Caused by: org.apache.cxf.service.factory.ServiceConstructionException: No resource classes found
     at org.apache.cxf.jaxrs.AbstractJAXRSFactoryBean.checkResources(AbstractJAXRSFactoryBean.java:317) ~[cxf-rt-frontend-jaxrs-3.1.6.jar:3.1.6]
     at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:159) ~[cxf-rt-frontend-jaxrs-3.1.6.jar:3.1.6]
     ... 26 common frames omitted

Resolution to the above:
Make sure that whatever classes you have, that are going to be beans (@Service, @Component, etc.), that do not have the JAX-RS annotations such as @Path, those bean classes without the JAX-RS annotations will need to implement the interface or class that is serving as the service class.  Confused?  Sorry, I wish I could explain it a bit better, but here’s an example.  

Imagine you have a PersonService interface and a PersonServiceEndpoint class like so: (By the way, the below will throw the error)

PersonServiceEndpoint.java

package com.example.Person;

import org.springframework.stereotype.Service;

@Service(“personService”)
public class PersonServiceEndpoint {

public Person readPersonInfoWithUsername(String username) {
        Person testPerson = new Person();
        testPerson.setName(username);
        testPerson.setCity(“Los Angeles”);
        return testPerson ;
    }
}

PersonService.java

package com.example.Person;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

@Path(“/person”)
public interface PersonService {

@GET
@Path(“/{username}”)
@Consumes({“application/json”, “application/xml”})
@Produces({“application/json”, “application/xml”})
public Person readPersonInfoWithUsername(@PathParam(“username”) String username); }

What’s missing from the PersonServiceEndpoint class? The implementation of the PersonService interface. The following code is correct:
package com.example.Person;

import org.springframework.stereotype.Service;

@Service(“personService”)
public class PersonServiceEndpoint implements PersonService {

    public Person readPersonInfoWithUsername(String username) {
        Person testPerson = new Person();
        testPerson.setName(username);
        testPerson.setCity(“Los Angeles”);
        return testPerson ;
    }
}

What’s missing from the PersonServiceEndpoint class? The implementation of the PersonService interface. The following code is correct:

package com.example.Person;

import org.springframework.stereotype.Service;

@Service(“personService”)
public class PersonServiceEndpoint implements PersonService {

    public Person readPersonInfoWithUsername(String username) {
        Person testPerson = new Person();
        testPerson.setName(username);
        testPerson.setCity(“Los Angeles”);
        return testPerson ;
    }
}

Another possible solution is to move the @Service back to the PersonService class too, but you’ll have refactor that class. Just make sure that your beans are declared properly so that JAX-RS can find it.

You Might Also Like