开发者社区 问答 正文

中英文对照 介绍Play Framework 框架 路由(Route) 400 请求报错

路由

h1. HTTP routing

路由组件负责将HTTP请求交给对应的action处理(一个控制器的静态公共方法)

The router is the component in charge of translating incoming HTTP Requests into action calls (a static, public method of a Controller).

一个HTTP请求在MVC框架里被当做一个事件看待。事件包含2个方面的信息

  请求的路径(例如/clients/1524,/photos/list),包含查询字符串(参数字符串)

  HTTP方法(GET,POST,PUT,DELETE)

An HTTP request is seen as an event by the MVC framework. The event contains two major pieces of information:

* The Request path (such as /clients/1542, /photos/list), including the query string.

* The HTTP method (GET, POST, PUT, DELETE)

关于REST

h2. <a>About REST</a>

表述性状态转移(REST)是一种类似互联网的分布式超媒体软件架构风格,

Representational state transfer (REST) is a style of software architecture for distributed hypermedia systems such as the World Wide Web.

REST的几个关键性地方设计准则:

应用功能分散在资源中

每个资源使用一个唯一的URI来寻址

所有资源在客户端和资源之间使用一个统一的接口来转移状态

REST states a few key design principles:

* Application functionality is divided into resources

* Every resource is uniquely addressable using an URI

* All resources share a uniform interface for the transfer of state between client and resource. 

如果你使用过HTTP,这些接口定义了一些可用的HTTP方法。

这些协议用于访问资源的状态:

*客户端-服务端

*无状态

*缓存

*分层

If you’re using HTTP, these interfaces are defined by the set of available HTTP methods. The protocol used to access the resource state is:

* Client-server

* Stateless

* Cacheable

* Layered

如果一个应用遵循了REST的主要设计准则,那么这个应用就是REST风格的。

Play框架使构建REST风格的应用变得更容易:

*Play的路由解释URI和HTTP方法,将一个请求匹配给一个Java调用。基于正则表达式的URI模式匹配给你更过的灵活性。

*协议时无状态的,意味着你不能在2次成功的请求之间在服务器上保存任何状态。

*Play把HTTP当做关键特性,这样框架可以让你接触到HTTP的所有信息。

If an application follows the main REST design principles, the application is RESTFul. The Play framework makes it easy to build RESTFul applications:

* The Play router interprets both URI and HTTP methods to route a request to a Java call. Regular expressions-based URI patterns give you even more flexibility. 

* The protocol is stateless. This means you can’t save any state on the server between two successive requests.

* Play considers HTTP as a key feature, thus the framework gives you full access to HTTP information.

Route文件语法

h2. <a name="syntax">The routes file syntax</a>

conf/toutes文件是Router使用的配置文件。该文件显示了应用所需的所有route。

每一个route由HTTP方法和UTI模式匹配和一个Java调用关联。

The **conf/routes** file is the configuration file used by the Router. This file lists all the routes needed by the application. Each route consists of an HTTP method + URI pattern associated with a Java call.

让我们看一下,一个的route的定义就像这样。

Let’s see what a route definition looks like:

bc. GET    /clients/{id}             Clients.show           

每一个route以一个HTTP方法开始,后面跟着URI模式,最后的是Java调用定义。

Each route starts with the HTTP method, followed by the URI pattern. The last element of a route is the Java call definition.

我们可以给route文件增加注释,以#开头

You can add a comment to the route file, with the **"#"** character.

bc. # Display a client

GET    /clients/{id}             Clients.show      

     

HTTP方法

h3. The HTTP method

HTTP方法可以是任何HTTP所支持的有效的方法。GET,POST,PUT,DELETE,HEAD

The HTTP method can be any one of the any valid methods supported by HTTP:

* **GET**

* **POST**

* **PUT**

* **DELETE**

* **HEAD**

如果使用*作为方法,则这个route可以和任何请求的方法相匹配

If you specify * as method, this route will match the HTTP Request for any method.

bc. *   /clients/{id}             Clients.show           

这些route可以独立的接受请求

This route will accept independently:

bc. GET /clients/1541

PUT /clients/1212

URI的模式

h3. The URI Pattern

URI模式定义了route(路由)中有一部分可以成为动态的,动态的部分必须包含在"{}" 中

The URI pattern defines the request path needed by the route Some parts of the route can be dynamic. Any dynamic part must be specified within braces {…}.

例如/clients/all可以匹配/clients/all,但是/clients/{id}可以独立的匹配/clients/12121,或者/clients/todo

bc. /clients/all

exactly matches:

bc. /clients/all

but…

bc. /clients/{id}

independently matches:

bc. /clients/12121

/clients/toto

一个URI模式可以不止一个动态的部分

A URI pattern may have more than one dynamic part:

例如 /clients/{id}/accounts/{accountId}

展开
收起
kun坤 2020-05-29 15:05:45 787 分享 版权
1 条回答
写回答
取消 提交回答