The first application of the Spring boot configuration (how to start)

Source: Internet
Author: User
Tags http request json root directory tomcat

In the August 24 article, "Full stack Engineer mobile development Practice ━restful and Spring boot details", I introduced to you the spring boot MAVEN configuration, today we look at Spring Boot's first application, which is the standard of a language--hello World's implementation is also the basis for our subsequent interaction between the app and the server.

1. First create a new Java class in the project that has the spring boot dependency configured, as follows, named Restfuldemo:
Project Catalogue
Restfuldemo class

2. Next, focus on each detail in this class.

(1) @RestController and @requestmapping annotations

The first annotation used above the Restfuldemo class is @restcontroller, which is often referred to as a stereotype (stereotype) annotation. Restcontroller itself is not unique to spring boot, it inherits from the @controller annotation. The controller is used to identify the current class as a director servlet. In this example, our class is a Web @Controller, so spring asks for it when it processes the incoming Web request. Restcontroller equivalent to @responsebody + @Controller. Responsebody is used to write to the body data area of the response object, when the returned data is JSON, XML, and so on, after the object returned by the Controller's method is converted to the specified format by the appropriate httpmessageconverter.

(2) @RequestMapping annotations

Requestmapping annotations are primarily used to provide routing information, which tells spring that any HTTP request from the "/" path should be mapped to the home method. The requestmapping annotation has six properties, and we'll describe her in three categories below.

1) Value, method

Value: Specifies the actual address of the request;

Method: Specifies the type of method requested, GET, POST, PUT, delete, and so on;

2) consumes,produces

Consumes: Specifies the type of submission to process the request (Content-type), such as Application/json, text/html;

Produces: Specifies the type of content returned, only if the specified type is included in the (Accept) type in the request header;

3) Params,headers

Params: Specifies that some parameter values must be included in the request before the method is processed.

Headers: Specifies that certain header values must be included in the request in order for the method to process requests.

(3) @EnableAutoConfiguration annotations

Enableautoconfiguration is spring boot based on the dependencies that we set in POM to automatically configure spring. Because Spring-boot-starter-web adds Tomcat and spring MVC, auto-configuration defaults to developing a web app and setting spring accordingly, but we can also configure it manually.

(4) Springapplication class

In the main method, by invoking the run of the Springapplication class, the business is delegated to spring boot, spring is started, and the automatically configured Tomcat Web server is started accordingly.

3. Operation of the application

There are several ways to run Spring boot, and here I introduce two kinds.

(1) myeclipse start

Right-click the Restfuldemo class->run As->java application
Start complete

If you see a red hint in the graph indicating that the project started normally, you can see "Hello world!" when you enter http://localhost:8080/in the browser. The output.

(2) Start with maven command line

Currently, if MAVEN is configured on our PC, it can be started at the command line. With the use of spring-boot-starter-parent dependencies, we have a very useful run target. command line into the project root directory, enter

MVN Spring-boot:run to launch the app:

Maven Downloads some dependencies, initializes the components, and eventually prints out the information that Tomcat started successfully.

At this point, the Spring boot Hello World app is started perfectly. In the next section, we will build the basic Android client framework, and you are welcome to stay tuned.

http://blog.csdn.net/wu__di/article/details/52374540

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.