A previous article, "5 minutes Building Spring Web MVC rest Style HelloWorld", describes the common way to develop a spring Web MVC Web service. Next look at how to build one quickly using spring boot.
Spring boot makes it easier for us to create a spring-based standalone and product-level application and service that can be "run on the Fly". The support contract is larger than the configuration to build and run the spring application as quickly as possible.
Before we created a spring-based project, we needed to consider which spring dependencies and third-party dependencies to add. With spring boot, we can start the spring application with minimal dependencies. Most spring boot applications require very little configuration to run, such as the ability to create standalone, standalone large Java applications, and then run a startup with Java-jar or a traditional war deployment. It also provides command-line tools to run the spring script directly (such as groovy script). That is, spring boot makes spring applications simpler from configuration to run, but does not provide enhancements to spring itself (that is, additional functionality).
Objective:
Make all spring development faster and allow more people to make the spring start experience faster and provide "starter" pom to simplify our MAVEN configuration (that is, use spring boot only with maven/ Gradle such a dependency management tool to be able to perform its capabilities), unlike before, building a SPRINGMVC project requires a lot of configuration, etc.
Out-of-the-box, quickly start requirements development without being affected by other aspects (if spring is automatically configured)
Provides a number of non-functional common large project class features (such as embedded server, security, metrics, health Check, extranet configuration), such as the ability to embed Tomcat/jetty directly (no need to deploy war packages separately)
No code generation, no XML configuration required
My build Environment
JDK 7
Maven 3
Servlet3 container
Create a project
Start by using MAVEN to create a normal Maven app that doesn't have to be web-ready.
Add spring boot-related POM configuration
Add the following configuration in Pom.xml
Java code
- <!--Inherit defaults from Spring Boot--
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>0.5. 0.build-snapshot</version>
- </parent>
- <!--Add Typical dependencies for a Web application --
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- </dependencies>
- <!--package as a executable JAR--
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- <!--allow access to Spring milestones and snapshots-
- <!--(you don't need this if you're using anything after 0.5. 0.RELEASE) --
- <repositories>
- <repository>
- <id>spring-snapshots</id>
- <url>http://repo.spring.io/snapshot</url>
- <snapshots><enabled>true</enabled></snapshots>
- </repository>
- <repository>
- <id>spring-milestones</id>
- <url>http://repo.spring.io/milestone</url>
- <snapshots><enabled>true</enabled></snapshots>
- </repository>
- </repositories>
- <pluginRepositories>
- <pluginRepository>
- <id>spring-snapshots</id>
- <url>http://repo.spring.io/snapshot</url>
- </pluginRepository>
- <pluginRepository>
- <id>spring-milestones</id>
- <url>http://repo.spring.io/milestone</url>
- </pluginRepository>
- </pluginRepositories>
After inheriting spring-boot-starter-parent, we can inherit some of the default dependencies, so that we do not need to add a bunch of corresponding dependencies to minimize the dependency configuration; Spring-boot-starter-web provides support for the Web. Spring-boot-maven-plugin provides a plug-in that directly runs the project, and we can run it directly mvn Spring-boot:run.
Entity Java code
- Package com.sishuok.entity;
- /**
- * <p>user:zhang Kaitao
- * <p>date:13-12-22
- * <p>version:1.0
- */
- Public class User {
- private Long ID;
- private String name;
- Public Long getId () {
- return ID;
- }
- public void SetId (Long id) {
- this.id = ID;
- }
- Public String GetName () {
- return name;
- }
- public void SetName (String name) {
- this.name = name;
- }
- @Override
- public Boolean equals (Object o) {
- if (this= O) return true;
- if (o = = Null | | getclass ()! = O.getclass ()) return false;
- User user = (user) O;
- if (id! = null?!id.equals (user.id): user.id! = null) return false;
- return true;
- }
- @Override
- public int hashcode () {
- return ID ! = null? Id.hashcode (): 0;
- }
- }
Controller Java Code
- Package Com.sishuok.controller;
- Import Com.sishuok.entity.User;
- Import org.springframework.boot.SpringApplication;
- Import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
- Import org.springframework.web.bind.annotation.PathVariable;
- Import org.springframework.web.bind.annotation.RequestMapping;
- Import Org.springframework.web.bind.annotation.RestController;
- /**
- * <p>user:zhang Kaitao
- * <p>date:13-12-22
- * <p>version:1.0
- */
- @EnableAutoConfiguration
- @RestController
- @RequestMapping ("/user")
- Public class Usercontroller {
- @RequestMapping ("/{id}")
- Public User View (@PathVariable ("id") Long ID) {
- User user = new user ();
- User.setid (ID);
- User.setname ("Zhang");
- return user;
- }
- //public static void Main (string[] args) {
- //Springapplication.run (Usercontroller.class);
- //}
- }
Run
The first way
Turn on automatic configuration by adding @enableautoconfiguration to Usercontroller and then through Springapplication.run (Usercontroller.class); Run this controller ; It is convenient to run only one controller in this way;
The second way
Enable annotation scanning and automatically register the corresponding annotation bean via @[email protected]
Java code
- Package Com.sishuok;
- Import Com.sishuok.controller.UserController;
- Import org.springframework.boot.SpringApplication;
- Import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
- Import Org.springframework.context.annotation.ComponentScan;
- Import org.springframework.context.annotation.Configuration;
- /**
- * <p>user:zhang Kaitao
- * <p>date:13-12-22
- * <p>version:1.0
- */
- @Configuration
- @ComponentScan
- @EnableAutoConfiguration
- Public class Application {
- public static void Main (string[] args) {
- Springapplication.run (Application. Class);
- }
- }
In this way, a basic restful Web application is built.
You can see the JSON results by entering HTTP://LOCALHOST:8080/USER/1 in the address bar.
If you look at their dependencies, you'll find that the dependencies are automatically added (whether you're using/not), but it's really fast to develop an app, and for beginners who want to learn/experience spring, building a project model quickly can be considered in this way. Of course, if you don't want to rely on so many jar packages, you can remove the parent and add dependencies yourself.
Welcome to join Spring Group 134755960 for communication.
Reference
Https://github.com/spring-projects/spring-boot
Spring boot--2 minutes Build Spring Web MVC rest style HelloWorld