REST service with Spring 3 on JDK6 HTTP Server

Go To StackoverFlow.com

2

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;
  }
}
2012-04-05 22:00
by martin s
First REST means HTTP.So you need servlet container to serve your REST requests - sathis 2012-04-06 07:00
I want to use the Jdk6 Http server. So I have http without a servlet container - martin s 2012-04-06 10:32
Why don't you want to run in a servlet container - Cem Catikkas 2012-04-08 01:20


2

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)

2012-04-06 15:43
by andih
thanks that helped me a lot but it would be interesting to know if there is a way to solve this problem with spring remoting - martin s 2012-04-06 21:28
Yeah... second that I would love too see this resolved with Spring and Spring MVC - Cristiano Fontes 2013-04-05 11:56
Ads