简体   繁体   中英

Play-Rest API with Java - basic response

I'm new in the server-side , and I'm trying to learn how to use play in rest api with java and restangular. I made a project for java in the intellij. I want the GET request to return an html page and not html.scala page. how do I change this function that it will return the app/views/index.html instead the app/views/index.html.scala

also if someone have a good website to learn from, it will help a lot

the function in java :

package controllers;

import play.*;
import play.mvc.*;

import views.html.*;

public class Application extends Controller {

    public static Result index() {
        return ok(index.render("Your new application is ready."));
    }

}

the routes page :

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

# Home page
GET     /                           controllers.Application.index

# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file               controllers.Assets.at(path="/public", file)

Play use twirl as a template engine. I am not sure if you can easely change it to some other template engine but I think you can. Any way, as I see you are looking for a way just output simple HTML file. You can do this with default Assets controller:

this string in your route

GET     /assets/*file               controllers.Assets.at(path="/public", file)

would handle any files in the public. So if you will add to the public directory the simple /html/hello.html file then Play will render it by the url /assets/html/hello.html . You can change URL as you like in the rote file

I found a similar question with good answer with a lot of examples. you can find it here . The easiest way is to make a GET request for the / and to send the destination also, like this:

# Home page
GET     /                           controllers.Assets.at(path="/public",file="index.html") 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM