简体   繁体   English

带有Java的Play-Rest API-基本响应

[英]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. 我是服务器端的新手,我正在尝试学习如何在rest api和Java和restangular中使用play。 I made a project for java in the intellij. 我在intellij中为Java创建了一个项目。 I want the GET request to return an html page and not html.scala page. 我希望GET请求返回一个html页面而不是html.scala页面。 how do I change this function that it will return the app/views/index.html instead the app/views/index.html.scala 我如何更改此函数,它将返回app / views / index.html而不是app / views / index.html.scala

also if someone have a good website to learn from, it will help a lot 如果有人有一个很好的网站可以学习,那也会有很大帮助

the function in java : 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. 无论如何,正如我所看到的,您正在寻找一种只输出简单HTML文件的方法。 You can do this with default Assets controller: 您可以使用默认的Assets控制器执行此操作:

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 . 因此,如果您将简单的/html/hello.html文件添加到公共目录,则Play将通过url /assets/html/hello.html呈现它。 You can change URL as you like in the rote file 您可以在死记硬背文件中随意更改URL

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: 最简单的方法是对/进行GET请求,并发送目的地,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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