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.html")
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
尝试使用来源:
Ok(Source.fromInputStream(stream).mkString("")).as("text/html")
@Salem had a good answer (here) to solve my compiling issue. I just had to make some changes afterwards to make it work. Here is the full code !
package controllers
import play.api.mvc._
import scala.io.Source
class FrontEndController extends Controller {
def matchAll(path: String) = Action {
val stream = FrontEndController.getClass.getClassLoader.getResourceAsStream("/public/layout.html")
Ok(Source.fromInputStream(stream).mkString("")).as("text/html")
}
}
object FrontEndController {}
I changed the name of my controller "Application" to "FrontEndController" because it's the controller that will be called when any API and static files has already been tested, hope it won't confuse anyone.
Feel free to point out any bad Scala practices ! I am only starting :)
链接地址: http://www.djcxy.com/p/64456.html上一篇: 如何制作div滑块?