上次根据restlet官网搭建了一个restlet的demo。
但在实际应用更加复杂,有时需要和spring整合,这里介绍一下spring整合restlet
1、创建web工程,导入如下jar包
主要是restlet的spring的jar包,以及一些基础jar包
2、修改web.xml
配置servlet为RestletFrameworkServlet,整合spring和restlet
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>restlet</servlet-name> <!--spring整合restlet --> <servlet-class>org.restlet.ext.spring.RestletFrameworkServlet</servlet-class> <init-param> <param-name>org.restlet.component</param-name> <!-- restlet-servlet.xml配置文件里有--> <param-value>restletComponent</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>restlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
3、创建resource代码
创建resource代码类,要继承ServerResource,用@Get和@Post注解来标明处理get请求的方法:getQueryCar和post请求的方法PostQueryCar
get请求通过ServletHttpRequest获取参数,post方法通过Representation和Form获取请求参数。
package com.chen.rest; import org.restlet.data.Form; import org.restlet.representation.Representation; import org.restlet.representation.StringRepresentation; import org.restlet.resource.Get; import org.restlet.resource.Post; import org.restlet.resource.ServerResource; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; /** * @author chenjie * @version 1.0 * @since 2017-02-09 */ public class QueryCar extends ServerResource { /** * get请求 * 通过HttpServletRequest获取请求参数 * */ @Get("xml|json") public Representation getQueryCar() { HttpServletRequest httprequest = ((ServletRequestAttributes) RequestContextHolder .getRequestAttributes()).getRequest(); String id = httprequest.getParameter("id"); String name = httprequest.getParameter("name"); System.out.println("id1="+id); System.out.println("name2="+name); Representation representation = new StringRepresentation("hello get"); return representation; } /** * post请求 * 通过Representation来获取请求参数 * */ @Post public Representation postQueryCar(Representation entity) { HttpServletRequest httprequest = ((ServletRequestAttributes) RequestContextHolder .getRequestAttributes()).getRequest(); Form form = new Form(entity); String id = form.getValues("id"); String name = form.getFirstValue("name"); System.out.println("id2="+id); System.out.println("name2="+name); Representation representation = new StringRepresentation("hello post"); return representation; } }
4、在web.xml下面创建restlet-servlet.xml
<?xml version="1.0"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!--默认配置 --> <bean id="restletComponent" class="org.restlet.ext.spring.SpringComponent"> <property name="defaultTarget" ref="root" /> </bean> <bean id="root" class="org.restlet.ext.spring.SpringRouter"> <property name="attachments"> <map> <!-- 请求配置到这里 --> <entry key="/query"> <bean class="org.restlet.ext.spring.SpringFinder"> <lookup-method name="create" bean="query" /> </bean> </entry> </map> </property> </bean> <!-- 配置bean,注入的话用property标签--> <bean id="query" class="com.chen.rest.QueryCar" scope="prototype"> </bean> </beans>
5、部署服务,启服务测试
发送get请求和post请求测试
http://localhost:8080/query?id=1&name=chen