2 u010717479 u010717479 于 2016.01.15 22:52 提问

关于cxf resutful 中出现No resource classes found的问题

用cxf开发restful服务,出现以下问题,希望大家能给予帮助,在此先谢过。

 Caused by: org.apache.cxf.service.factory.ServiceConstructionException
    at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:219)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1706)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1645)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
    ... 62 more
Caused by: org.apache.cxf.service.factory.ServiceConstructionException: No resource classes found
    at org.apache.cxf.jaxrs.AbstractJAXRSFactoryBean.checkResources(AbstractJAXRSFactoryBean.java:317)
    at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:159)
    ... 69 more

7个回答

CSDNXIAOD
CSDNXIAOD   2016.01.15 22:54

CXF中,出现 No services have been found.
Android出错问题:AndroidManifest.xml:2: error: No resource identifier found for attribute 'installLocatio
error: No resource identifier found for attribute 问题
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

u010717479
u010717479   2016.01.15 22:54

我的配置如下:
(1)、web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
         id="WebApp_ID" version="3.1">
  <display-name>Archetype Created Web Application</display-name>
  <!-- B:配置中文编码问题的过滤器 -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- E:配置中文编码问题的过滤器 -->

    <!-- B:配置Spring配置文件的名称和位置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:cxf-server.xml
        </param-value>
    </context-param>
    <!-- E:配置Spring配置文件的名称和位置 -->

    <!-- B:启动IOC容器的ServletContextListener -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- E:启动IOC容器的ServletContextListener -->

    <!-- B:CXF servlet -->
    <servlet>
      <servlet-name>CXFServlet</servlet-name>
      <servlet-class>
         org.apache.cxf.transport.servlet.CXFServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet-mapping>
      <servlet-name>CXFServlet</servlet-name>
      <!-- 匹配下/services/*的所有请求 -->
      <url-pattern>/services/*</url-pattern>
   </servlet-mapping>
   <!-- E:CXF servlet -->
</web-app>

u010717479
u010717479   2016.01.15 22:55

(2)、pom.xml

 <?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.github.mervyn</groupId>
    <artifactId>java-learn</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>org.github.mervyn.webServices_CXF_springRestful</groupId>
  <artifactId>java-learn-webServices_CXF_springRestful</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <packaging>war</packaging>
  <name>java-learn-webServices_CXF_springRestful Maven Webapp</name>
  <url>http://maven.apache.org</url>

   <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.version>4.2.3.RELEASE</spring.version>
    <cxf.version>3.1.4</cxf.version>
    <jackson.version>1.9.13</jackson.version>
    <jsr311.version>1.1.1</jsr311.version>
    <!-- jetty context path -->
    <jetty.contextPath>java-learn-webServices_CXF_springRestful</jetty.contextPath>
  </properties>

  <dependencies>
   <!-- B:spring依赖 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
    </dependency>   
    <!-- E:spring依赖 -->

    <!-- B:cxf依赖 -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>${cxf.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>${cxf.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http-jetty</artifactId>
        <version>${cxf.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-rs-extension-providers</artifactId>
        <version>${cxf.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxrs</artifactId>
        <version>${cxf.version}</version>
    </dependency>
    <!-- E:cxf依赖 -->

    <!-- B:javax.ws.rs -->
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>jsr311-api</artifactId>
        <version>${jsr311.version}</version>
    </dependency>
    <!-- E:javax.ws.rs -->


    <!-- B:jackson -->
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-jaxrs</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <!-- E:jackson -->

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
    <finalName>java-learn-webServices_CXF_springRestful</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <encoding>utf8</encoding>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

        <plugin>
          <groupId>org.eclipse.jetty</groupId>
          <artifactId>jetty-maven-plugin</artifactId>
          <version>9.0.0.v20130308</version>
            <configuration>

                <webApp>
                  <contextPath>/${jetty.contextPath}</contextPath>
                </webApp>
                <!-- 热部署时间 -->
                <scanIntervalSeconds>5</scanIntervalSeconds>
                <webAppSourceDirectory>${basedir}/src/main/webapp</webAppSourceDirectory>
            </configuration>
        </plugin>
    </plugins>
  </build>

</project>

u010717479
u010717479   2016.01.15 22:55

(3)、HelloWS接口

 package org.github.mervyn.webServices_CXF_springRestful;


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;
import javax.ws.rs.core.MediaType;


public interface IHelloWS {

    @GET
    @Path("/HelloWS/{id}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces({MediaType.APPLICATION_JSON,"text/html; charset=UTF-8"})
    String getUserById(@PathParam("id")int id);
}
u010717479
u010717479   2016.01.15 22:56

(4)、HelloWSImpl

 package org.github.mervyn.webServices_CXF_springRestful;


import java.io.IOException;
import java.io.StringWriter;

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;
import javax.ws.rs.core.MediaType;

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.ObjectMapper;


public class HelloWSImpl implements IHelloWS {

    @Override
    /*@GET
    @Path("/HelloWS/{id}")
    //@Consumes(MediaType.APPLICATION_JSON)
    @Produces({MediaType.APPLICATION_JSON,"text/html; charset=UTF-8"})*/
    public String getUserById(@PathParam("id")int id){
        System.out.println("id为: " + id);
        User user = new User();
        user.setId(id);
        user.setName("mervyn");
         ObjectMapper mapper = new ObjectMapper();  
         StringWriter sw = new StringWriter(); 
         JsonGenerator gen = null;
         try {
             gen = new JsonFactory().createJsonGenerator(sw);
             mapper.writeValue(gen, user);
             gen.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String json = sw.toString();
        return json;
    }

}
u010717479
u010717479   2016.01.15 22:56

(5)cxf-server.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:core="http://cxf.apache.org/core"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    xsi:schemaLocation="http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">

    <import resource="classpath*:META-INF/cxf/cxf.xml" />
    <import resource="classpath*:META-INF/cxf/cxf-servlet.xml" /> 

    <bean id="helloWSImpl" class="org.github.mervyn.webServices_CXF_springRestful.HelloWSImpl" />

    <jaxrs:server id="iHelloWS" address="/">
        <jaxrs:serviceBeans>
            <ref bean="helloWSImpl"/>
        </jaxrs:serviceBeans>
    </jaxrs:server>



</beans>
u010717479
u010717479   2016.01.16 15:08

问题已解决。是因为自己粗心。cxf实现restful风格的web services 接口上写上注解,同时也要在接口的实现类中添加上相应的注解。图片说明

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!