Search code examples
javaspringtomcatrestlet

Restlet's GET POST DELETE method route to the same function


This question is very strange.Details as below.

I use restlet with spring/tomcat in a project to provide REST service. But in some cases, All HTTP methods(POST/DELETE/GET) request goes into the same function, such as the @Get routine, and sometimes all method request goes into the @Post routine. Cases are different every time I restart the tomcat. and sometimes all methods works.

This is my configuration:

web.xml

<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>

applicationContext.xml

<bean id="component" class="org.restlet.ext.spring.SpringComponent">
    <property name="defaultTarget" ref="restRouter" />
</bean>
<bean id="restRouter" class="org.restlet.ext.spring.SpringBeanRouter"></bean>
<bean name="/v1.0/{tenant}/templates/{id}" id="one" class="com.XXX.TemplateResource"/>

TemplateResource extends the ServerResource

@Get
@Override
public Representation get(){
    // some code
}

I think the configuration is ok, and sometimes everything works. So is it something wrong with other thing like eclipe or tomcat? Thank you.


Solution

  • In your applicationContext.xml

    <bean name="/v1.0/{tenant}/templates/{id}" id="one" class="com.XXX.TemplateResource"/>
    

    For SpringBeanRouter, Resources must be scoped prototype, since a new instance must be created for each request.

    See SpringBeanRouter API and restlet-spring-example for details.