Search code examples
springcxfrestful-architecture

Restful web service based on CXF


everyone! Something has confused me a lot. I make a Restful Web service based on CXF,but it does not work.

My web.xml is as follows:

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<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>
    <url-pattern>/service/*</url-pattern>
</servlet-mapping>

<welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

and then is my applicationContext.xml:

<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean id="handleRequest" class="test.cjm.HandleReuqst"/>                                                                <jaxrs:server id="customerService" address="/">
    <jaxrs:serviceBeans>
      <ref bean="handleRequest"/>
    </jaxrs:serviceBeans>

and my HandleRequest.java file presents below:

@Produces("application/json")
@Path("/test")
public class HandleRequest {    
   @GET
   @Path("/Spring")
   public TestVO testSpring(){
       System.out.println("get users:");
   TestVO tv = TestDAO.getPerson();
   return tv;

}

}

The TestDAO.java file

   private static Map<String,TestVO> testVOs;
   static{
       testVOs = new HashMap<String,TestVO>();
   TestVO t1 = new TestVO();
   t1.setId(1);
   t1.setName("cjm");      
   testVOs.put("1", t1);

  }

   public static TestVO getPerson(){
       return testVOs.get("1");
  }

I deploy the program in tomcat named CXFSpring. I don't know where is wrong, Everytime I make a request to localhost:8080/CXFSpring/test/Spring, It just return a 404 status. Could you tell me Where is wrong?


Solution

  • you CXF serving servlet is mapped to /service/*

    try the following URL: localhost:8080/CXFSpring/service/test/Spring