<Reslet1>:Reslet+Spring,配置简单web访问

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保持一致
基本结构如图:


[img]http://dl2.iteye.com/upload/attachment/0107/5870/1fe0d2ef-8ef7-36c6-aab3-28924fb986a7.jpg[/img]

下面上代码:
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

======持续更新中=====
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值