最も簡単なrestlet webserviceの作成

6230 ワード

OperationServer 1メソッド:
public class OperationServer1 extends ServerResource {
	static String s = "operation1";

	@Get()
	public Representation doGet(Representation entity){		
		s = "Get Operation1";
		StringRepresentation entityRsp=new StringRepresentation(s, MediaType.TEXT_XML);
		return entityRsp;
		
	}
	
	@Post()
	public Representation doPost(Representation entity) {
		s = "Post Operation1";
		StringRepresentation entityRsp=new StringRepresentation(s, MediaType.TEXT_XML);
		return entityRsp;

    }
	
	@Put()
	public Representation doPut(Representation entity){
		s = "Put Operation1";
		StringRepresentation entityRsp=new StringRepresentation(s, MediaType.TEXT_XML);
		return entityRsp;
		
	}

	@Delete()
	public Representation doDelete(Representation entity){
		s = "Delete Operation1";
		StringRepresentation entityRsp=new StringRepresentation(s, MediaType.TEXT_XML);
		return entityRsp;
	}
	
}

ここでextends Server Resourceを欠くことはできません.そうしないと、cannot be cast to orgとエラーが発生します.restlet.resource.ServerResource
WEB-INF下web.xml

<!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>restClientExample</display-name>
	
		<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/xml/restApi.xml
          </param-value>
	</context-param>
	
		<context-param>
		<param-name>logbackConfigLocation</param-name>
		<param-value>/WEB-INF/xml/logback.xml</param-value>
	</context-param>
	
	<filter>  
    <filter-name>Spring character encoding filter</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>  

<filter-mapping>  
    <filter-name>Spring character encoding filter</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping> 

	

	<listener>
		<listener-class>ch.qos.logback.classic.servlet.LogbackConfigListener</listener-class>
	</listener>

	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<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>security_component</param-value>
		</init-param>
	</servlet>

	<servlet-mapping>
		<servlet-name>restlet</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>
</web-app>

このうちwebappタグの下の各タグの順序は要求通りにしないとThe content of element type「web-app」must match」(icon?,display-name?,description?,distributable?,context-param*
xmlディレクトリ下restApi.xml:

<?xml version="1.0" encoding="UTF-8"?>  
  
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> 
   <beans>
    <bean id="security_component" class="org.restlet.ext.spring.SpringComponent">  
        <property name="defaultTarget" ref="security_restRoute" />  
    </bean>  
       <bean id="security_restRoute" class="org.restlet.ext.spring.SpringRouter">  
        <property name="attachments">  
            <map>  
                <entry key="/operation1">  
                  <bean class="org.restlet.ext.spring.SpringFinder">
                 <lookup-method name="create" bean="Operation1" /> 
                  </bean>  
                </entry>
                <entry key="/operation2">  
                  <bean class="org.restlet.ext.spring.SpringFinder">
                 <lookup-method name="create" bean="Operation2" /> 
                  </bean>  
                </entry>
            </map>  
        </property>  
    </bean> 

	<bean id="Operation1" class="server.OperationServer1" scope="prototype">
	</bean>
	<bean id="Operation2" class="server.OperationServer2" scope="prototype">
	</bean>
</beans>

もちろん、restApiで新しいjavaクラスを追加するインタフェースは複数あります.xmlもそれに応じて新しいbeanを追加すればよい.
実際に共通クラス(すべてのURIが使用する)がある場合は、サーバクラスを新しく書いてサーバResourceを継承し、このseverクラスでこれらの共通クラスを初期化し、他のURIがこのserverクラスを継承する必要があります.
ではrestApi.xmlは次のように変更する必要があります.
<bean id="fatherSever" class="sever.sever">
    	<property name=" " ref=" "/>
    </bean>

	<bean id="Operation1" class="server.OperationServer1" scope="prototype" parent="fatherSever">
	</bean>