Is there any possibility to create a REST service with spring 3.0 without an servlet container? Because I don't want to use an application server.
I tried to create REST services with SimpleHttpInvokerServiceExporter and Spring MVC but I got a java.lang.NoClassDefFoundError: javax/servlet/ServletException
because I don´t use a servlet container. My code looks like this:
<beans>
...
<bean name="serviceFacadeExporter"
class="org.springframework.remoting.httpinvoker.SimpleHttpInvokerServiceExporter">
<property name="service" ref="serviceFacade" />
<property name="serviceInterface" value="facade.ServiceFacade" />
</bean>
<bean id="httpServer"
class="org.springframework.remoting.support.SimpleHttpServerFactoryBean">
<property name="contexts">
<map>
<entry key="/api/" value-ref="serviceFacadeExporter" />
</map>
</property>
<property name="port" value="8082" />
</bean>
...
</beans>
And the service looks like this
@Controller
public class ServiceFacadeImpl implements ServiceFacade {
@Override
@RequestMapping(value = "/protein/search/{searchString}")
public long searchProtein(@PathVariable String searchString) {
return 0;
}
}
Spring MVC requires the Servlet API
You can create a Simple Rest Service using JSE 6 HTTP Server the following way
You create a Resource class
@Path("/helloworld")
public class MyResource {
// The Java method will process HTTP GET requests
@GET
// The Java method will produce content identified by the MIME Media
// type "text/plain"
@Produces("text/plain")
public String getClichedMessage() {
// Return some cliched textual content
return "Hello World";
}
}
You create an Rest Application
public class MyApplication extends javax.ws.rs.core.Application{
public Set<Class<?>> getClasses() {
Set<Class<?>> s = new HashSet<Class<?>>();
s.add(MyResource.class);
return s;
}
}
And that's how you start your Server
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 25);
HttpContext context = server.createContext("/resources");
HttpHandler handler = RuntimeDelegate.getInstance().createEndpoint
(new MyApplication(), HttpHandler.class);
context.setHandler(handler);
server.start();
That's all. No Spring MVC required.
For Testing purposes this works very well, for a productive usage with many requests I would use a WebContainer like Jetty or Tomcat.
A more detailed description of how to build a RESTFul using the Standard JSE 6 HttpServer can be found RESTFul Webservice mit JAX-RS (German)