上次根據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