CXF+SPRING+REST 10分钟教程

CXF跟spring联合起来,搞rest webservice的确很方便的.下面快速学习下,用到的是CXF,SPRING和MAVEN 

1 MAVEN配置 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-context</artifactId> 
    <version>3.0.1.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-web</artifactId> 
    <version>3.0.1.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>org.apache.cxf</groupId> 
    <artifactId>cxf-rt-frontend-jaxrs</artifactId> 
    <version>2.2.3</version> 
</dependency> 
2 COMPILER也改下 
<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-compiler-plugin</artifactId> 
    <version>2.1</version> 
    <configuration> 
        <source>1.6</source> 
        <target>1.6</target> 
    </configuration> 
</plugin> 
... 

3 timeservice 
  import org.springframework.format.datetime.DateFormatter; 
import org.springframework.stereotype.Service; 

import java.util.Calendar; 
import java.util.Locale; 

@Service("timeService") 
public class TimeService { 
    public String getDateTime() 
    { 
        DateFormatter formatter = new DateFormatter("dd/MM/yyyy hh:mm:ss"); 
        return formatter.print(Calendar.getInstance().getTime(), Locale.getDefault()); 
    } 


4 CXF使用JAX-RS 去实现REST 
  @Service("timeService") 
@Path("/time") 
public class TimeService { 
    @GET 
    @Produces("text/plain") 
    public String getDateTime() 
    { 
       ... 
    } 


5 配置WEB.XML 
  <?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE web-app PUBLIC 
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
        "http://java.sun.com/dtd/web-app_2_3.dtd"> 
<web-app> 
    <context-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value>WEB-INF/beans.xml</param-value> 
    </context-param> 
    <listener> 
        <listener-class> 
            org.springframework.web.context.ContextLoaderListener 
        </listener-class> 
    </listener> 
    <servlet> 
        <servlet-name>CXFServlet</servlet-name> 
        <display-name>CXF Servlet</display-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>/rest/*</url-pattern> 
    </servlet-mapping> 
</web-app> 


6 配置CXF配置文件 
   <?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:jaxrs="http://cxf.apache.org/jaxrs" 
       xmlns:context="http://www.springframework.org/schema/context" 
       xsi:schemaLocation=" 
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
       http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd 
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 

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

    <context:component-scan base-package="be.insaneprogramming.cxf"/> 

    <jaxrs:server id="restContainer" address="/"> 
        <jaxrs:serviceBeans> 
            <ref bean="timeService"/> 
        </jaxrs:serviceBeans> 
    </jaxrs:server> 
</beans> 

猜你喜欢

转载自ibadboy.iteye.com/blog/1054527