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

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

  1. 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
  2.     at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
  3.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
  4.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
  5.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
  6.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
  7.     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
  8.     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
  9.     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
  10.     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
  11.     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
  12.     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
  13.     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
  14.     at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
  15.     at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
  16.     at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
  17.     at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
  18.     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
  19.     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
  20.     at com.trek.Application.main(Application.java:10) [bin/:na]
  21. 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
  22.     at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
  23.     at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
  24.     ... 18 common frames omitted
  25. Caused by: org.apache.cxf.service.factory.ServiceConstructionException: null
  26.     at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:219) ~[cxf-rt-frontend-jaxrs-3.1.6.jar:3.1.6]
  27.     at org.apache.cxf.jaxrs.spring.AbstractSpringConfigurationFactory.createJaxRsServer(AbstractSpringConfigurationFactory.java:54) ~[cxf-rt-frontend-jaxrs-3.1.6.jar:3.1.6]
  28.     at org.apache.cxf.jaxrs.spring.SpringComponentScanServer.jaxRsServer(SpringComponentScanServer.java:29) ~[cxf-rt-frontend-jaxrs-3.1.6.jar:3.1.6]
  29.     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_73]
  30.     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_73]
  31.     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_73]
  32.     at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_73]
  33.     at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
  34.     ... 19 common frames omitted
  35. Caused by: org.apache.cxf.service.factory.ServiceConstructionException: No resource classes found
  36.     at org.apache.cxf.jaxrs.AbstractJAXRSFactoryBean.checkResources(AbstractJAXRSFactoryBean.java:317) ~[cxf-rt-frontend-jaxrs-3.1.6.jar:3.1.6]
  37.     at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:159) ~[cxf-rt-frontend-jaxrs-3.1.6.jar:3.1.6]
  38.     ... 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

  1.  
  2. package com.example.Person;
  3.  
  4. import org.springframework.stereotype.Service;
  5.  
  6. @Service("personService")
  7. public class PersonServiceEndpoint {
  8.  
  9. public Person readPersonInfoWithUsername(String username) {
  10.         Person testPerson = new Person();
  11.         testPerson.setName(username);
  12.         testPerson.setCity("Los Angeles");
  13.         return testPerson ;
  14.     }
  15. }

PersonService.java

  1. package com.example.Person;
  2.  
  3. import javax.ws.rs.Consumes;
  4. import javax.ws.rs.GET;
  5. import javax.ws.rs.Path;
  6. import javax.ws.rs.PathParam;
  7. import javax.ws.rs.Produces;
  8.  
  9. @Path("/person")
  10. public interface PersonService {
  11.  
  12. @GET
  13. @Path("/{username}")
  14. @Consumes({"application/json", "application/xml"})
  15. @Produces({"application/json", "application/xml"})
  16. 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:

  1. package com.example.Person;
  2.  
  3. import org.springframework.stereotype.Service;
  4.  
  5. @Service("personService")
  6. public class PersonServiceEndpoint implements PersonService {
  7.  
  8.     public Person readPersonInfoWithUsername(String username) {
  9.         Person testPerson = new Person();
  10.         testPerson.setName(username);
  11.         testPerson.setCity("Los Angeles");
  12.         return testPerson ;
  13.     }
  14. }

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.