Go: Rest Introduction and spring implementation

Source: Internet
Author: User
Tags log log

A Roy Fielding

2000 rest was put forward by Roy Fielding, and I had a few impressions of Roy Fielding.

One is the royfielding as the author of the HTTP protocol, the HTTP protocol has not been released for a long time to say that many people in the world for the use of HTTP is wrong, so he said people should use rest.

Second, it is not long before royfielding as the Enlightenment of rest thought, in rest being accepted and widely used not long enough to jump to say that many people in this world are using the rest is wrong.

So I picked Plato's sentence in PPT as the subtitle, "Thought is always the ruler of the Universe".

Two Rest

The rest itself is much more than I thought, and a few key points are outlined below:

1. Meet the following constraints:

Constraints code
    1. Client–server
    2. Stateless
    3. Cacheable
    4. Layered system
    5. Code on Demand (optional)
    6. Uniform interface

2. Principles for designing interfaces

Principles Code
    1. Identification of resources
    2. Manipulation of resources through these representations
    3. self-descriptive messages
    4. Hypermedia as the engine of application state

3.Rest goals you want to achieve

Key Goals Code
    1. Scalability of component Interactions
    2. Generality of interfaces
    3. Independent deployment of components
    4. Intermediary components to reduce latency, enforce security and encapsulate legacy systems

All of the above is from the wiki, a little bit of tidying up. I feel the above content is very deep, so I simply listed the important place that I think understand rest.

4.Rest for us

Wrote every things is a Resource.
Every resouce has a ID.
We Can use Http meothod (get/post/put/delete) Handle Resource.

Simply put, when you design the interface again,

The first thing that comes to mind is what resources I want to provide.

The second thought is what the presentation of this resource is.

The third thing that comes to mind is what the operations on this resource encapsulate

I think that's enough. Rest and soap than there are too many benefits, but also conducive to SEO (thanks to the search group of the two great God said two points, one is the URL path itself accounted for a higher weight than parameters, one is the URL link itself weight is relatively high.)

If it is to design a Keyboard wizard interface (previously mentioned in the Blog Keyboard Wizard, the main function here is to find pinyin in the "Q" beginning of the product list), in the previous period of time around the popular comparison method written out three different styles of the URL interface design

Wrote the general style/prompt/list.do?query=g&count=10&searchfrom=product

Literary style
/prompt/product/g/10

2 B style/?method=getproductpromt&query=g&count=10&searchfrom=product

There have been a lot of people arguing that rest is useless, and there are a lot of people talking about the application of rest, and someone is asking more practical questions, such as how to solve multiple parameters.

I think the rest itself provides a way to make this way more artistic and really depends on how you use him. Design rest interface is an art form, the simplest example, I want to see a person's time period published article list, may have the following several design ways:

1./person/xdyl/20000101-20000202

2./person/xdyl/start/20000101/end/20000202

3./person/xdyl?start=20000101&end/20000202

4./person/xdyl/20000101/20000202/

Which way is good to see a person likes it. Search for a bit SPRINGMVC also did not see this solution, before thought there would be/{a}-{b}/such a way.

If other friends have a good solution also please give more guidance.

Three Spring MVC implementations.

I think the implementation of spring MVC is simple. About two places are involved.

How the 1.Spring itself supports getting variables from path.

2. How does the system differentiate whether a request should be blocked by spring or a static resource that should be accessed directly?

The first question is simple. The post code is as follows:

Java code
  1. @Controller
  2. @RequestMapping (value = "/contact")
  3. Public class Contactcontroller {
  4. private static final log log = Logfactory.getlog (Contactcontroller.   Class);
  5. @RequestMapping (value = "/{contact}", method = Requestmethod.get)
  6. Public String getcontactdetail (@PathVariable Long contact, model model) {
  7. Contact C = this.contactService.getContact (contact);
  8. if (c = = null) {
  9. c = new Contact ();
  10. }
  11. Model.addattribute ("code", 0);
  12. Model.addattribute ("Contact", c);
  13. return "/contact/detail/jmodel";
  14. }
  15. }

Additions and deletions to modify the "method" to correspond to the HTTP Four ways (post,delete,put,get) is good.

Variables can be obtained directly through @pathvariable.

The second question I understand is simple. Use Urlrewriter to divide all requests into two types. Dynamic requests are prefixed with a prefix "/app/", and the spring interceptor is configured to intercept only such requests. The static resource begins with the prefix "/r/" and the request path is not changed.

So any request will be obediently divided into two parts, is to "/r/" start will not go to spring, not to "/r/" beginning all turn into "/app/", to spring processing.

The main configuration is as follows

Web. Xml wrote <filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--spring only intercepts/app/requests--
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>

Urlrewrite.xml wrote <urlrewrite default-match-type= "wildcard" >
<rule>
<from>/r/**</from>
<to>/r/$1</to>
</rule>
<rule>
<from>/**</from>
<to>/app/$1</to>
</rule>
<outbound-rule>
<from>/app/**</from>
<to>/$1</to>
</outbound-rule>
</urlrewrite>

Urlrewrite can be replaced with the same function as any other, Apache horse is the most annoying.

Finally, I enclose the urlrewriter pom file I used. I don't remember where I saw it, just put it up.

Pom.xml wrote
<dependency>
<groupId>org.tuckey</groupId>
<artifactId>urlrewritefilter</artifactId>
<version>3.1.0</version>
</dependency>

Over, this thing is not a very detailed description of the spring configuration. I remember the first time there was a lot of problems. But I think the source code if the public, there is nothing wrong with it ~

Wait and see when you can open the source of labs.

Go: Rest Introduction and spring implementation

Related Article

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.