springboot2.x Best Practice "One" springboot2.x first experience

Source: Internet
Author: User

springboot2.x Best Practice Preface

This series of articles, from zero-based contact springboot2.x to new releases, basic introductory use, hot deployment, to the integration of various mainstream frameworks redis4.x, Message Queuing acivemq, ROCKETMQ, etc., Search Framework ElasticSearch5.6 version, to Web-flux reactive programming, to actuator monitoring application information, official best practices, the most complete SpringBoot2. Tutorials

Technical selection and update instructions:

    • Use the latest springboot2.x version

    • JDK1.8

    • Idea or Eclipse editor

    • maven3.x version

    • Other frameworks, such as distributed cache Redis, search framework elasticsearch, Message Queuing rocketmq and ACTIVEMQ, all use the latest version


Information and development tools to prepare:

    • JDK1.8 Download: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

    • maven3.x Download: http://maven.apache.org/download.cgi

    • springboot2.x Official Document: Https://spring.io/projects/spring-boot#overview


What is Springboot?

    learned Javaweb's classmates knew that developing a Web application from the first touch of Servlet to Tomcat, running out of a Hello Wolrld program, was going through a lot of steps, and then using the framework struts, Then is SPRINGMVC, to the present Springboot, after a year or two there will be other web frameworks appear; I wonder if you have ever experienced the evolution of the framework, and then the development of their own projects all the technology will continue to change, transformation, anyway, I have experienced, haha.

What is Springboot, then, is a javaweb development framework, similar to SPRINGMVC, compared to the benefits of other javaweb frameworks, officially simplified development, the Convention is greater than the configuration, you can "just run", can quickly develop Web applications , a few lines of code develop an HTTP interface.

Quickly create springboot2.x Apps

      Learning any framework, starting with Hello World, this is no exception, we first create an HTTP interface that returns JSON data.

springboot official provides a quick tool for creating applications "Spring Initializr", Address: Http://start.spring.io

Through this can help us build a project of the basic framework and directory specifications, operations such as the order

1, after downloading into idea or eclipse inside, springboot default to add a lot of dependent packages, so you need to download from the remote MAVEN repository, the initial import need to download a lot of dependent packages, it will be more slow, However, you can also change the address of the warehouse where the MAVEN warehouse address is domestic.

2, the official Package directory interface description

3. Maven Dependent file explanation

The core profile is on top of the picture, there are a lot of temporary configuration on the bottom, such as the warehouse address can be removed, the following pom file can be removed

<repositories><repository><id>spring-snapshots</id><name>spring snapshots </name><url>https://repo.spring.io/snapshot</url><snapshots><enabled>true</ enabled></snapshots></repository><repository><id>spring-milestones</id>< name>spring milestones</name><url>https://repo.spring.io/milestone</url>< Snapshots><enabled>false</enabled></snapshots></repository></repositories> <pluginrepositories><pluginrepository><id>spring-snapshots</id><name>spring  snapshots</name><url>https://repo.spring.io/snapshot</url><snapshots><enabled >true</enabled></snapshots></pluginRepository><pluginRepository><id> spring-milestones</id><name>spring milestones</name><url>https://repo.spring.io/ milestone</url><snapshots><Enabled>false</enabled></snapshots></pluginrepository></pluginrepositories> 

Writing an HTTP interface

1, create a new package, Domian to hold the entity class, and create a new User.java class

package net.xdclass.demo.domain;/** *  user class  */public  class user {    private int id;    private  string name;    public int getid ()  {         return id;    }    public void  SetId (Int id)  {        this.id = id;     }    public string getname ()  {         return name;    }    public void  SetName (String name)  {        this.name = name;     }} 

2, create a new controller, and create a Usercontroller.java class to add the annotation @restcontroller (used to return the JSON data, if you use @controller, you need to add a @responsebody To return the JSON data), create a new Finduser method, add an annotation to the method, @RequestMapping ("find")

package net.xdclass.demo.controller;import net.xdclass.demo.domain.user;import  org.springframework.web.bind.annotation.requestmapping;import  org.springframework.web.bind.annotation.restcontroller;/** *  using   @RestController   defining a controller  */@RestControllerpublic  class UserController {         /**     *  defines an interface that accepts two parameters      *  @param  id      *  @param  name     *  @return       */     @RequestMapping ("find")     public user  finduser (Int id, string name) {        user  User = new user ();         user.setname (name);         user.setid (ID);         return user;    }} 

3, use cmd or command line tools to enter the project root directory, package the application into an executable jar package, execute the MAVEN package command mvn clean package 

[info][info] results:[info][info] tests run: 1,  failures: 0, errors: 0, skipped: 0[info][info][info] --- maven-jar-plugin :3.0.2:jar  (Default-jar)  @ demo ---[info] building jar: /users/xdclass/ desktop/article/First article/demo/target/demo-0.0.1-snapshot.jar[info][info] --- spring-boot-maven-plugin:2.0.3. release:repackage  (default)  @ demo ---[info] ---------------------------------------- --------------------------------[info] build success[info] ------------------------------------ ------------------------------------[Info] total time: 5.459 s[info] finished at : 2018-07-16t08:34:57+08:00[info] ------------------------------------------------------------------ ------


After the package is successful, a success will appear and then go to the target directory with a xxx.jar (XXX is the name of the project) which is the jar package that can be executed directly.


Start Java-jar Xxxx.jar, start about 10 seconds, the bottom can see the log of successful startup, springboot default port is 8080, so you can directly access

Interface Address: Http://localhost:8080/find?id=1&name= two small D

Responds to a user object, it succeeds.

This is a successful HTTP interface, a Web application can start the normal access and access, if you want to develop other interfaces, continue to write.

Next article, explaining the springboot2.x initiation process and commonly used annotations, the official recommended directory specification


Small series accumulated years of dry documents free of charge, including front end and testing, system architecture, high concurrency processing, optimization, etc.

springboot2.x Best Practice "One" springboot2.x first experience

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.