使用Apache CXF搭建REST风格的Web Service

1)编写domain实体类

package com.domain;

import java.io.Serializable;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="UserInfo")
public class User implements Serializable,Comparable<User>{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private int id;
    private String name;
    private String email;
    private String address;

    public User(){}

    @XmlElement
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    @XmlElement
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @XmlElement
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }

    @XmlElement
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return this.name+","+this.email+","+this.address;
    }

    @Override
    public int compareTo(User other) {
        return this.id - other.id;
    }
}

2)业务处理接口

package com.service;

import com.domain.User;

public interface IUserService {
    public User getUserById(int id);
}

3)业务处理实现类

package com.service;

import com.domain.User;

public class UserService implements IUserService{
    public User getUserById(int id){
        User user = new User();
        user.setId(1);
        user.setName("Rickesy");
        user.setAddress("china");
        user.setEmail("Rickesy@163.com");
        return user;
    };
}

3)Web Service接口

package com.rest;

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 com.domain.User;

@Path(value="/")
public interface IRESTService {

    @GET
    @Path("user/{id}")
    @Produces({MediaType.APPLICATION_XML,
        MediaType.APPLICATION_JSON})
    public User getUserById(@PathParam("id") int id);
}

4)Web Service实现类

package com.rest;

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 com.domain.User;
import com.service.UserService;

@Path(value="/")
public class RESTService implements IRESTService{
    private User user;
    private IUserService userService;

    @GET
    @Path("user/{id}")
    @Produces({MediaType.APPLICATION_XML,
        MediaType.APPLICATION_JSON})
    public User getUserById(@PathParam("id") int id){
        return userService.getUserById(id);
    }

    public IUserService getUserService() {
        return userService;
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }   
}

5)配置applicationContext.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:p="http://www.springframework.org/schema/p"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.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="user" class="com.domain.User"></bean>
    <bean id="userService" class="com.service.UserService"></bean>

    <bean id="restService" class="com.rest.RESTService">
        <property name="user" ref="user"></property>
        <property name="userService" ref="userService"></property>
    </bean>

    <jaxrs:server id="restServiceContainer" address="/rest">
        <jaxrs:serviceBeans>
            <ref bean="restService" />
        </jaxrs:serviceBeans>
        <jaxrs:extensionMappings>
            <entry key="json" value="application/json" />
            <entry key="xml" value="application/xml" />
        </jaxrs:extensionMappings>
        <jaxrs:languageMappings>
            <entry key="en" value="en-gb"/>  
        </jaxrs:languageMappings>
    </jaxrs:server>
</beans>

6)配置web.xml

<listener>
    <listene-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
 </listener>
 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>
        org.apache.cxf.transport.servlet.CXFServlet
    </servlet-class>
</servlet>
<servlet-mapping>   
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

7)客户端测试

package com.client;

import javax.ws.rs.core.MediaType;
import org.apache.cxf.jaxrs.client.WebClient;

public class Client {
    static WebClient client = WebClient.create(
        "http://localhost:8080/RESTWebService/rest");

    public static void main(String[] args) {
        User user = client.path("/user/{id}", 
            1).accept(MediaType.APPLICATION_XML).get(User.class);
        System.out.println(user);
    }
}
posted @ 2015-03-06 17:14  Rickesy  阅读(311)  评论(0编辑  收藏  举报