<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 41  文章 - 29  trackbacks - 0
    <2008年11月>
    2627282930311
    2345678
    9101112131415
    16171819202122
    23242526272829
    30123456

    常用鏈接

    留言簿(5)

    隨筆分類(28)

    隨筆檔案(23)

    收藏夾(6)

    Inside JVM

    Java

    java performance

    Solr

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    Recently, Rest architecture style or Restful web service is a hot topic for SOA, web service design and architecture style. But unfortunately, there are not many articles introducing how to develop a real restful web services. So, this series of articles try to introduce how to develop a Restful web services with CXF. The reason why i choose CXF is just because we are using CXF to develop SOAP web service, and CXF 2.1.X already supports JSR-311 API version 0.8 and CXF 2.2 plans to support JSR-311 1.0 API.

    Some of the examples and comments are directly copies from CXF Restful page (http://cwiki.apache.org/CXF20DOC/restful-services.html), thanks for CXF team's great work.

    This article will focus on a simplest service and how to config it as a Restful web service in tomcat. My tomcat version is 6.0.X.

    Software: Tomcat 6.0.x, CXF 2.1
    1. use CXF in Tomcat. please refer to CXF doc. It is pretty easy.
    2. the major configuration file for CXF is  WEB-INF/cxf-servlet.xml, we will add related configuration into this file.

    <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi
    ="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:jaxrs
    ="http://cxf.apache.org/jaxrs"   <!-- This is the name space for jax rs -->
          xmlns:jaxws
    ="http://cxf.apache.org/jaxws"
          xmlns:soap
    ="http://cxf.apache.org/bindings/soap"
          xsi:schemaLocation
    ="
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
          http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
          http://cxf.apache.org/jaxrs                                            
    <!-- This is the schema location for CXF jax rs -->
          http://cxf.apache.org/schemas/jaxrs.xsd                     <!-- This is the schema location for CXF jax rs xsd -->
          http://cxf.apache.org/jaxws
          http://cxf.apache.org/schemas/jaxws.xsd"
    >

        
    <!-- JAX-RS -->
      
    <jaxrs:server id="customerService" address="/">    <!--NOTE: the address is relative address, the whole URL pattern will be http://localhost:8080/web app/services/, and the services  -->
        <jaxrs:serviceBeans>
           
    <bean class="com.starcite.commonsearch.example.rs.CustomerServiceImpl" />    <!-- this is the class path, cannot be interface-->
        
    </jaxrs:serviceBeans>
      
    </jaxrs:server>

    </beans>

    3. Sample Service interface & implementation class
    @Path("/customerservice/")            <!-- URI for this service, the whole URI will be http://localhost:8080/web app/services/customerservice/ -->
    public interface CustomerService {

        @GET                                    <!-- i guess @GET and @Path annotation is pretty easy to understand, if not, please read
    http://m.tkk7.com/justinchen/archive/2008/11/05/238760.html -->
        @Path("/customers/")       <!-- URI for this service, the whole URI will be http://localhost:8080/web app/services/customerservice/customers/ -->
        public Customer getCustomers();
        
        @GET
        @Path(
    "/customers/{id}/")    <!-- URI for this service, the whole URI will be http://localhost:8080/web app/services/customerservice/customers/123/ etc -->
        public Customer getCustomer(@PathParam("id") String id);  <!-- @PathParam is the parameter extracts the value of a URI template parameter. -->

        @PUT
        @Path(
    "/customers/")
        
    public Response updateCustomer(Customer customer);
        
        @POST
        @Path(
    "/customers/")
        
    public Response addCustomer(Customer customer);
        
        @DELETE
        @Path(
    "/customers/{id}/")
        
    public Response deleteCustomer(@PathParam("id") String id);
        
        @Path(
    "/orders/{orderId}/")
        
    public Order getOrder(@PathParam("orderId") String orderId);
        
    }

    Service impl class code. NOTE: there are no need any annotation. But also, there are a little bit different on return value comparing to SOAP web service. In SOAP web service, we can directly use any java class. But here, we used some special class, like Response, we will disucss this more in other articles.
    public class CustomerServiceImpl implements CustomerService {
        
        
    private long currentId = 123;
        Map
    <Long, Customer> customers = new HashMap<Long, Customer>();
        Map
    <Long, Order> orders = new HashMap<Long, Order>();

        
    public CustomerServiceImpl() {
            init();
        }

        
    public Customer getCustomers() {
            System.out.println(
    "----invoking getCustomer, Customer id is: XXX " ); 
            
            Customer c 
    = customers.get(123);
            
            
    return c;
        }
        
        
    public Customer getCustomer(String id) {
            System.out.println(
    "----invoking getCustomer, Customer id is: " + id);
            
    long idNumber = Long.parseLong(id);
            Customer c 
    = customers.get(idNumber);
            
    return c;
        }

        
    public Response updateCustomer(Customer customer) {
            System.out.println(
    "----invoking updateCustomer, Customer name is: " + customer.getName());
            Customer c 
    = customers.get(customer.getId());
            Response r;
            
    if (c != null) {
                customers.put(customer.getId(), customer);
                r 
    = Response.ok().build();
            } 
    else {
                r 
    = Response.notModified().build();
            }

            
    return r;
        }

        
    public Response addCustomer(Customer customer) {
            System.out.println(
    "----invoking addCustomer, Customer name is: " + customer.getName());
            customer.setId(
    ++currentId);

            customers.put(customer.getId(), customer);

            
    return Response.ok(customer).build();
        }

        
    public Response deleteCustomer(String id) {
            System.out.println(
    "----invoking deleteCustomer, Customer id is: " + id);
            
    long idNumber = Long.parseLong(id);
            Customer c 
    = customers.get(idNumber);

            Response r;
            
    if (c != null) {
                r 
    = Response.ok().build();
                customers.remove(idNumber);
            } 
    else {
                r 
    = Response.notModified().build();
            }

            
    return r;
        }

        
    public Order getOrder(String orderId) {
            System.out.println(
    "----invoking getOrder, Order id is: " + orderId);
            
    long idNumber = Long.parseLong(orderId);
            Order c 
    = orders.get(idNumber);
            
    return c;
        }

        
    final void init() {
            Customer c 
    = new Customer();
            c.setName(
    "John");
            c.setId(
    123);
            customers.put(c.getId(), c);

            Order o 
    = new Order();
            o.setDescription(
    "order 223");
            o.setId(
    223);
            orders.put(o.getId(), o);
        }

    }

    4. POJO Class, such as Customer. POJO class is pretty similar, we can also use JAXB to covert object to XML.
    @XmlRootElement(name = "Customer")
    public class Customer {
        
    private long id;
        
    private String name;

        
    public long getId() {
            
    return id;
        }

        
    public void setId(long id) {
            
    this.id = id;
        }

        
    public String getName() {
            
    return name;
        }

        
    public void setName(String name) {
            
    this.name = name;
        }
    }

    5. Now, you can deploy this web app as normal web app. and after you start tomcat, you are able to try the following link in browser -
    http://localhost:8080/vendor_search/services/customerservice/customers/ Or
    http://localhost:8080/vendor_search/services/customerservice/customers/123/

    the browser will return a XML for a cusomer object.

    In next articles, we will implement more complex user case to prove the Restful web service. 
    posted on 2008-11-05 18:24 Justin Chen 閱讀(3506) 評論(1)  編輯  收藏 所屬分類: Rest

    FeedBack:
    # re: Create Restful Web Service With CXF 2.1.X/CXF 2.2, Part 1: Create a service in Tomcat[未登錄] 2009-03-10 20:28 Jason
    public Customer getCustomers() {
    System.out.println("----invoking getCustomer, Customer id is: XXX " );

    Customer c = customers.get(123);

    return c;
    }

    according to CXF's suggestions, we need to define a collection to hold a set of Customer.So the codes will be
    public Customers getCustomers() {

    Customers c = customers.getAll();

    return c;
    }  回復(fù)  更多評論
      
    主站蜘蛛池模板: 1000部啪啪毛片免费看| 人妻免费一区二区三区最新| 91精品免费观看| 相泽亚洲一区中文字幕| 美女视频黄a视频全免费网站色| 四虎成人免费观看在线网址| 亚洲成年网站在线观看| 97无码免费人妻超级碰碰夜夜| 亚洲AV综合色区无码二区偷拍| 免费国产成人高清在线观看网站| 亚洲乱码一二三四区国产| 4虎永免费最新永久免费地址| 亚洲春黄在线观看| 少妇高潮太爽了在线观看免费| 亚洲三级视频在线观看| 成人影片麻豆国产影片免费观看| 亚洲最大天堂无码精品区| 免费被黄网站在观看| 边摸边吃奶边做爽免费视频99| 亚洲毛片网址在线观看中文字幕| 国产精品免费αv视频| 久久精品国产亚洲av麻| 最近中文字幕mv免费高清视频8| 亚洲成a人片在线观| 破了亲妺妺的处免费视频国产| 羞羞视频免费网站含羞草| 亚洲精品成人片在线观看精品字幕| 暖暖在线视频免费视频| 亚洲人成日本在线观看| 狠狠久久永久免费观看| 黄色短视频免费看| 日本久久久久亚洲中字幕| 最近中文字幕mv手机免费高清| 羞羞视频网站免费入口| 久久亚洲精品成人| 四虎在线免费播放| 国产免费人成视频在线播放播 | 三年片在线观看免费观看高清电影 | 日本免费xxxx| 国产在亚洲线视频观看| 亚洲αv在线精品糸列|