我是否应该将完整的网页作为html/文本传递给其余的GET调用 [英] Should I pass a full webpage as html/text for a rest GET call

查看:41
本文介绍了我是否应该将完整的网页作为html/文本传递给其余的GET调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个提供动态内容的网站.服务器/浏览器之间通过REST进行的所有通信. PostgreSQL用作数据存储.

I am building a website that would serve dynamic content. All communication between server/browser in through REST. PostgreSQL is used as a datastore.

我的问题是对于任何GET请求,我是否应该动态构建html(以及动态内容).

My question is for any GET request, should i be building the html on the fly (along with the dynamic content).

例如

@GET
@Produces(MediaType.TEXT_HTML)
public String getAllEmployee() {
    // employees fetched from the data base
    String html = "<HTML></head> blah blah";
    return html;
}

我的问题是html是否应即时构建并发送回浏览器. 大型网站(例如linkedin)又如何运作?他们会即时生成html页面并发回该页面吗?

My question is should the html be built on the fly and and sent back to the browser. Also how does big websites like linkedin works? Do they generate the html page on the fly and send back the page?

我可以想到的另一种方法是发送嵌入了AJAX请求的准系统html.然后,ajax请求从服务器获取动态内容.

Another method I could think of is send the barebones html with AJAX request embedded into it. And then the ajax request fetches the dynamic content from the server.

推荐答案

REST的核心优势之一是将表示形式(编码)与所访问的基础资源分离.

One of the core benefits of REST is its separation of representation (encoding) from the underlying resource being accessed.

如果客户端通过Accept标头要求将HTML作为首选项,则返回HTML是完全可以的.如果客户端表明它希望使用JSON或XML或明年想实现的其他任何超duper编码,则您的服务器可以返回该格式,而您的URI方案将保持不变.

It's perfectly fine to return HTML if the client requests it as a preference via the Accept header. If the client indicates that it prefers JSON or XML or whatever other super-duper encoding is dreamed up next year, then your server can return that format instead, and your URI scheme won't change one bit.

最重要的是,不要永远将REST API绑定到单一编码格式.利用HTTP内容协商为您提供的出色的灵活性,使您成为API服务提供商,这样您就可以使您的API客户端能够选择最合适的格式来满足他们的需求.

Most importantly, don't forever tie your REST API to a single encoding format. Take advantage of the wonderful flexibility that HTTP content negotiation offers you as an API service provider, and that way you can give your API clients the ability to pick and choose the most appropriate format for their needs.

这篇关于我是否应该将完整的网页作为html/文本传递给其余的GET调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆