Spring Boot (ii) Quick experience Web development

Source: Internet
Author: User
Tags line web stub

Spring-boot-starter-web is Spring Boot's support for web development, including RESTful, parameter checking, and using Tomcat as an inline container.
Support for 1.json
2. Request a reference
With Spirng Boot, you can easily make some restrictions on requests, such as allowing access only to POST requests for security purposes. Just add a configuration to the method:

@RequestMapping (name= "/getuser", method= public User GetUser () {      ...}

The Spring WEB layer supports a variety of method arguments, such as the incoming property name, which is also supported for direct use of object reception.

@RequestMapping (name= "/getuser", method= public String getUser (user user) {    ...}

Such a notation, as long as the user's properties are automatically populated into the user object. There is another way to pass the argument.
Using a URL to pass the parameter, this form of the parameter address bar will be more beautiful.

@RequestMapping (value= "Get/{name}", method= public User get (@PathVariable String name) {    User User=new  User ();    User.setname (name);     return user;}

3. Parameter check
As in ordinary javaweb, through the form of annotations, no longer elaborated
4. Custom Filter
We often use Filters in our projects to record request logs, exclude characters with XSS threats, perform permission validation, and so on. Spring Boot automatically adds the
Orderedcharacterencodingfilter and Hiddenhttpmethodfilter, and you can customize the Filter.
Custom Filter Two steps:
1) Implement the Filter interface to implement the DoFilter method
2) Add the @Configuration annotations to the filter chain by adding the custom filter
To create a new Myfilter class, use the DoFilter () method:

 Public classMyfilterImplementsFilter {@Override Public voiddestroy () {   //TODO auto-generated Method Stub} @Override Public voidDoFilter (ServletRequest srequest, Servletresponse sresponse, Filte rchain filterchain)throwsIOException, servletexception {   //TODO auto-generated Method StubHttpServletRequest request =(httpservletrequest) srequest; System.out.println ("This is Myfilter,url:" +Request.getrequesturi ());    Filterchain.dofilter (Srequest, sresponse); } @Override Public voidInit (Filterconfig arg0)throwsservletexception {      //TODO auto-generated Method Stub   } }

To add a custom filter to the filter chain:

@Configuration Public classwebconfiguration {@Bean PublicRemoteipfilter Remoteipfilter () {return NewRemoteipfilter (); } @Bean PublicFilterregistrationbean testfilterregistration () {Filterregistrationbean registration=NewFilterregistrationbean (); Registration.setfilter (Newmyfilter ()); Registration.addurlpatterns ("/*"); Registration.addinitparameter ("ParamName", "Paramvalue"); Registration.setname ("Myfilter"); Registration.setorder (1); returnregistration; } }

When you have finished adding the project and accessing any URL, you will see the console print the following information:

This is Myfilter,url:/xxx
Note that all URLs have been monitored by myfilter, and this feature is often used for session validation during actual use.
Determine if the user is logged on.
5. Custom property
Configuration in Application.properties

com.neo.title= Pure smile com.neo.description= sharing life and technology

Custom Configuration classes:

 Public class neoproperties {@Value ("${com.neo.title}"private  String title; @Value ("${ Com.neo.description} "private//}

Write unit test into validation:

@RunWith (Springrunner.  Classpublicclass  propertiestest {     private  Neoproperties properties;      publicvoidthrows  Exception {     System.out.println ("title:" + properties.gettitle ());     SYSTEM.OUT.PRINTLN ("description:" +properties.getdescription ());}   }

Output results after running test:
Text title: Pure Smile Description: Sharing life and technology

Summarize
Spring Boot integrates parameter checking, inline containers, Restful, and JSON content, which are essential in the development of in-line web.
Spring Boot has packaged these technologies, making it easy to use these technologies during Web project development, reducing the technical difficulty of developing Web projects.

Spring Boot (ii) Quick experience Web development

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.