Daniel Daniel - 6 months ago 52
AngularJS Question

Play Framework, routing with Angular [SCALA]

I'm working on a Play Framework / Angular project where my Play Framework server is a REST service only used by an API to get/send/manage JSON in a database, my Angular is used for the client side.

I am trying to make them independent to be able to have them on different machines.

My problem is for the routing : Play Framework tries to do all the routing, and if something is not matched an error is thrown but I would like Angular routes to be used when nothing else is matched (when templates, /api, static files are not matched)

On stack overflow I found this thread : play framework route that matches all
The person ran into exactly the same problem as I, but using Java
The solution was to have a route matching anything else at the end of the file :

GET /*path controllers.Application.matchAll(path)


My problem comes from the fact that the code given for the controller is Java :

public class Application extends Controller {

public static Result matchAll(String path) {
return ok(Application.class.getResourceAsStream("/public/index.html")).as("text/html");
}
}


And here is my attempt in Scala :

object Application extends Controller {
def matchAll(path: String) {
val stream = Application.getClass.getClassLoader.getResourceAsStream("/public/layout.h‌​tml")
Ok(stream).as("text/html");
}
}


So when compiling I get the error :

"Cannot write an instance of java.io.InputStream to HTTP Response"


I found other solutions on the web, but they involve routing the index using PlayFramework.. that makes them less independent so I think this "Java solution" is still the best/fastest approach and I cannot figure out the Scala equivalent.

Thank you for reading, I hope this post will be helpful to others

Answer

Try to use Source:

Ok(Source.fromInputStream(stream).mkString("")).as("text/html")
Comments