3

I have an application built on spray + akka. using this guide:

http://sysgears.com/articles/building-rest-service-with-scala/

It explains this example: https://github.com/oermolaev/simple-scala-rest-example

The application is working just fine. But when trying to deploy on a webServer I didn't find a way to do that.

I've tried to use xsbt-web-plugin to deploy on Tomcat, got the following input:

 ~container:start

[info] starting server ... Adding Context for target/webapp ...

Starting service Tomcat Starting Servlet Engine:

Apache Tomcat/7.0.34 org.apache.catalina.startup.ContextConfig

getDefaultWebXmlFragment INFO: No global web.xml found

org.apache.coyote.AbstractProtocol start INFO: Starting

ProtocolHandler ["http-nio-8080"]

But Tomcat is returning 404 for all the requests.

Does someone know how can I deploy a spray akka application on Tomcat?

griffon vulture
  • 6,594
  • 6
  • 36
  • 57
  • See this question, there seems to be a problem with spray-servlet figuring out the context path on tomcat automatically, so you need to set it manually: http://stackoverflow.com/questions/29701593/why-do-i-get-the-requested-resource-could-not-be-found-when-accessing-simple – jrudolph Apr 27 '15 at 13:51
  • Have you tried changing `spray.servlet.root-path` in your `application.conf`? – jrudolph Apr 27 '15 at 14:20
  • yes, it helped, thanks. my problem now is what to define as in the web.xml – griffon vulture Apr 27 '15 at 15:20
  • 1
    @griffonvulture - can I ask why you want to deploy Spray on a web server (container)? Spray itself is containerless and can be deployed as a runnable jar file. Have you considered that option ? You can also redirect your traffic from the web-server to the Spray instance. – Soumya Simanta Apr 27 '15 at 16:57

1 Answers1

1

Solved the problem.

This is what you need to make xsbt-plugin work with the spray application:

  1. Set the root-path in application.conf

As @jrudolph pointed: spray servlet doesn't know to figure it out automaticly on tomcat:

spray.servlet {
   boot-class = "com.sysgears.example.boot.Boot"
   root-path = "/rest"
   request-timeout = 10s
 } 
  1. Change class boot to extend webBoot:

boot.scala

class Boot extends WebBoot {
  // create an actor system for application

  val system = ActorSystem("rest-service-example")

  // create and start rest service actor

  val serviceActor = system.actorOf(Props[RestServiceActor], "rest-endpoint")
}
  1. add the web.xml as explained on xsbt-web-plugin:

    src/main/webapp/WEB-INF/web.xml:

    <listener>
        <listener-class>spray.servlet.Initializer</listener-class>
    </listener>
    
    <servlet>
        <servlet-name>SprayConnectorServlet</servlet-name>
        <servlet-class>spray.servlet.Servlet30ConnectorServlet</servlet-class>
        <async-supported>true</async-supported>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>SprayConnectorServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    

For the full change see the comparison on github (The example writer has generously generate this branch for tomcat users)

https://github.com/oermolaev/simple-scala-rest-example/compare/spray-tomcat

griffon vulture
  • 6,594
  • 6
  • 36
  • 57
  • The example is long outdated, though. Maybe you should try setting up your application from the "official" template at https://github.com/spray/spray-template/tree/on_jetty_1.3_scala-2.11 which already contains all these files and also uses the latest version of spray :) – jrudolph Apr 28 '15 at 11:23