A summary of the conceptual content of Scala spray

Source: Internet
Author: User

Spray is a lightweight Scala library based on Akka that can be used to write REST API services. Understanding Spray's DSL allows you to write a REST API service in a very short period of time, and its deployment does not require a container such as Tomcat, Apche, or the like, to run directly. For each route, spray will sprawn one or more actors, the number of actors is configurable, and we don't need to be concerned with multithreading. Judging from benchmark, spray's performance is very good. In addition, spray provides a suite of test suites, TestKit, which can be used to test the usability of the API locally, and the test function is very powerful, I think it can completely replace the SoapUI and other automated testing tools.

However, the process of learning to use spray is still more painful, for instance

Await.result (statement, 5.seconds)

After writing this line of code, the IDE complains that 5.seconds can't understand it, but where it is, the IDE doesn't prompt. In many cases, the IDE does not complain, but the compilation process goes wrong, saying that a implicit variable cannot be found. Contact spray three months, the most I have a headache is the implicit variable problem. For the header file location of commonly used variables, I would like to write a log for a specific record.

Here are some conceptual understandings, mainly referring to spray doc, which comes with part of your own understanding.

Route

Type Route = RequestContext + = Unit

RequestContext is a local variable in the route, it always exists, and we can call the RequestContext method anywhere in the route. The requestcontext contains the request and response two meanings, which can be obtained from the data sent by the request, as well as the. Complete,. Reject, etc. response semantics. The route definition has no return value, and its return value is unit, because the route uses the fire and forget mode, its benefits are first flexible, there is no limit to the type of return value, followed by this design is completely non-blocking, easy and actor union.

Directive (instruction)

Spray's DSL is a Directive.

def routes = {
Path ("") {
get {
Respondwithmediatype (' text/html ') {
Complete {
Defaultvalues.defaultapi
}
}
}
}
}

Path, GET, Respondwithmediatype, complete all count as a Directive. They nest together to form the semantics of a Route. Generally speaking, Directive has four functions, the first is to copy the RequestContext to the next layer directive,requestcontext in the process of transmission is immutable. Second, it can get the parameters that are included with the requester, such as parameters, FormData, Jsondata, etc., can also complete Marshall, unmarshall operation. Define the path and some logic to filter a request, such as a request that does not conform to any path will be reject, the logic can be the header must reject the cache, post data only allow JSON format and so on. Finally, he can use the return result to requester, can define the type and value of the result, in the above example is text/html and string (DEFAULTAPI)

Reject, Exception, Timeout handling

Requester requests may be malformed, may have insufficient permissions, or packets may be lost, so spray requires "exception" processing. Exception handling can be an explicit custom declaration, and spray also has a default implementation. For those that do not conform to any path (or reject path), there is an error in transmitting data, there is no authentication request, spray calls the implementation of reject handling, and the problem that occurs during the execution of the program invokes the exception handling defined operation. For example, in addition to 0 exception processing. Timeout is the simplest, which defines how long a request will return a time-out error without answering.

Spray is implemented by complete (code, message) and the browser displays code, message. For those more common errors, spray has custom code, message, and those custom errors, we want to write code message.

Json Support

A summary of the conceptual content of Scala spray

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.