简单小结下CXF跟REST搭配webservice的做法,直接举代码为例子:
1 order.java
package com.example.rest;
import javax.xml.bind.annotation.XmlRootElement;
这个order可以在service中使用就代表当前的类,然后就是简单地调用关系,相当于操作的是json和xml格式的数据,返回的是把这个值当做,相应的json或者xml格式输出输出
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") //xml文件的根节点 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列表
1 order.java
package com.example.rest;
import javax.xml.bind.annotation.XmlRootElement;
这个order可以在service中使用就代表当前的类,然后就是简单地调用关系,相当于操作的是json和xml格式的数据,返回的是把这个值当做,相应的json或者xml格式输出输出
@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") //xml文件的根节点 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列表
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。