使用CXF创建REST WEBSERVICE

简单小结下CXF跟REST搭配webservice的做法,直接举代码为例子:

1 order.java
  package com.example.rest;

import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement(name = "Order")
public class Order {

    private int orderId;
    private String itemName;
    private int quantity;
    private String customerName;
    private String shippingAddress;

    public int getOrderId() {
        return orderId;
    }
    public void setOrderId(int orderId) {
        this.orderId = orderId;
    }
    public String getItemName() {
        return itemName;
    }
    public void setItemName(String itemName) {
        this.itemName = itemName;
    }
    public int getQuantity() {
        return quantity;
    }
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
    public String getCustomerName() {
        return customerName;
    }
    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }
    public String getShippingAddress() {
        return shippingAddress;
    }
    public void setShippingAddress(String shippingAddress) {
        this.shippingAddress = shippingAddress;
    }

}

2 orderlist.java
   package com.example.rest;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "OrderList")
public class OrderList {

    @XmlElement(name = "order", required = true)
    List <Order> orders;

    public List<Order> getOrder() {
        if (orders == null) {
            orders = new ArrayList<Order>();
        }
        return this.orders;
    }
}


3 orderinof.java的接口
   package com.example.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;



@Path("/Order/")
public interface OrderInfo {

@GET
@Produces ("application/xml")
@Path("{orderId}")
public Order getOrder(@PathParam ("orderId") int officeId);

@GET
@Produces ("application/xml")
@Path ("All")
public OrderList getAllOrders();

}

4 OrderinfoImpl.java接口实现类
   package com.example.rest;

import java.util.ArrayList;
import java.util.List;

public class OrderInfoImpl implements OrderInfo {

    List <Order> list = new ArrayList<Order>();

    OrderInfoImpl(){
        Order order = new Order();
        order.setOrderId(1);
        order.setItemName("Soap");
        order.setQuantity(120);
        order.setCustomerName("Sandeep");
        order.setShippingAddress("Gurgaon");
        list.add(0, order);

        order.setOrderId(2);
        order.setItemName("Shampoo");
        order.setQuantity(50);
        order.setCustomerName("Sandeep");
        order.setShippingAddress("Gurgaon");
        list.add(1, order);
    }

    @Override
    public Order getOrder(int orderId) {
        System.out.println("Inside the GetOrder...");
        if (list.get(0).getOrderId() == orderId) {
            return list.get(0);
        } else if (list.get(1).getOrderId() == orderId) {
            return list.get(1);
        } else {
            return null;
        }
    }

    @Override
    public OrderList getAllOrders() {
        OrderList details = new OrderList();
        for(Order order : list) {
            details.getOrder().add(order);
        }
        return details;
    }
}

CXF的配置
   <beans xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.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-extension-jaxrs-binding.xml">
  <import resource="classpath:META-INF/cxf/cxf-servlet.xml">
  <jaxrs:server address="/" id="connectionService">
   <jaxrs:servicebeans>
              <ref bean="order">
   </ref></jaxrs:servicebeans>
   <jaxrs:extensionmappings>
    <entry key="xml" value="application/xml">
   </entry>
</jaxrs:extensionmappings>
  </jaxrs:server>
<bean class="com.javatch.rest.OrderImpl" id="order">
</bean>
</import>
</import>
</import>
</beans>

web.xml的配置记得加上CXF配置
  <?xml version="1.0"?>
<!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>
  <display-name>RestWithCXF</display-name>

  <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:com/javatch/rest/cxf.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>
  </servlet>
  <servlet-mapping>
   <servlet-name>CXFServlet</servlet-name>

   <url-pattern>/services/*</url-pattern>
  </servlet-mapping>
</web-app>

5 最后运行
  htttp://localhost:8085/reset/services/order/all
返回所有ORDER列表

使用CXF创建REST WEBSERVICE,布布扣,bubuko.com

使用CXF创建REST WEBSERVICE

上一篇:MongoDB常用命令


下一篇:实例讲解Springboot整合MongoDB进行CRUD操作的两种方式