Reslet+Spring,配置容易web访问
<Reslet1>:Reslet+Spring,配置简单web访问
Restlet针对每个url指定一个资源。使用spring注解,代码已经跑通,附件项目下载,欢迎留言讨论
注意:web.xml配置component,
param-value要和applicationContext.xml中bean的id保持一致
基本结构如图:

下面上代码:
CustomerDAO
OrderDao
CustomerDAOImpl
OrderDaoImpl
CustomerResource
OrderResource
applicationContext.xml
web.xml
配置好启动tomcat浏览器输入:
http://localhost:8080/test-restlet/order/1
页面显示:
this order id is 1
http://localhost:8080/test-restlet/customer/1
页面显示:
The customer id is 1
======持续更新中=====
Restlet针对每个url指定一个资源。使用spring注解,代码已经跑通,附件项目下载,欢迎留言讨论
注意:web.xml配置component,
<init-param> <param-name>org.restlet.component</param-name> <param-value>component</param-value> </init-param>
param-value要和applicationContext.xml中bean的id保持一致
基本结构如图:
下面上代码:
CustomerDAO
public interface CustomerDAO { public String getCustomerById(String id); }
OrderDao
public interface OrderDao { public abstract String getOrderId(String id); }
CustomerDAOImpl
@Service @Scope("prototype") public class CustomerDAOImpl implements CustomerDAO { public String getCustomerById(String id) { return "The customer id is " +id; } }
OrderDaoImpl
@Service @Scope("prototype") public class OrderDaoImpl implements OrderDao { @Override public String getOrderId(String id) { return "this order id is "+id; } }
CustomerResource
@Controller @Scope("prototype") public class CustomerResource extends ServerResource { public CustomerResource() { // TODO Auto-generated constructor stub } String customerId = ""; @Get public Representation getRepresentation() { customerId = (String) getRequest().getAttributes().get("custId"); String string = customerDAO.getCustomerById(customerId); return new StringRepresentation(string); } @Autowired private CustomerDAO customerDAO; }
OrderResource
@Controller public class OrderResource extends ServerResource{ String orderId = ""; @Get public Representation getRepresentation() { orderId = (String) getRequest().getAttributes().get("orderId"); return new StringRepresentation(orderDao.getOrderId(orderId)); } @Autowired private OrderDao orderDao; }
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-lazy-init="true"> <context:component-scan base-package="com.sioo" /> <bean id="component" class="org.restlet.ext.spring.SpringComponent"> <property name="defaultTarget" ref="restRoute" /> </bean> <bean id="restRoute" class="org.restlet.ext.spring.SpringRouter"> <property name="attachments"> <map> <entry key="/customer/{custId}"> <bean class="org.restlet.ext.spring.SpringFinder"> <lookup-method name="create" bean="customerResource" /> </bean> </entry> <entry key="/order/{orderId}"> <bean class="org.restlet.ext.spring.SpringFinder"> <lookup-method name="create" bean="orderResource" /> </bean> </entry> </map> </property> </bean> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/applicationContext.xml</param-value> </init-param> </servlet> <!--Spring ApplicationContext load --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Spring,avoid leaking memory --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <!-- restlet servlet --> <servlet> <servlet-name>restlet</servlet-name> <servlet-class>org.restlet.ext.spring.SpringServerServlet</servlet-class> <init-param> <param-name>org.restlet.component</param-name> <param-value>component</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>restlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
配置好启动tomcat浏览器输入:
http://localhost:8080/test-restlet/order/1
页面显示:
this order id is 1
http://localhost:8080/test-restlet/customer/1
页面显示:
The customer id is 1
======持续更新中=====