Spring boot--2 minutes Build Spring Web MVC rest style HelloWorld

Source: Internet
Author: User
Tags groovy script

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
  1. <!--Inherit defaults from Spring Boot--
  2. <parent>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-parent</artifactId>
  5. <version>0.5. 0.build-snapshot</version>
  6. </parent>
  7. <!--Add Typical dependencies for a Web application --
  8. <dependencies>
  9. <dependency>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-web</artifactId>
  12. </dependency>
  13. </dependencies>
  14. <!--package as a executable JAR--
  15. <build>
  16. <plugins>
  17. <plugin>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-maven-plugin</artifactId>
  20. </plugin>
  21. </plugins>
  22. </build>
  23. <!--allow access to Spring milestones and snapshots-
  24. <!--(you don't need this if you're using anything after 0.5. 0.RELEASE) --
  25. <repositories>
  26. <repository>
  27. <id>spring-snapshots</id>
  28. <url>http://repo.spring.io/snapshot</url>
  29. <snapshots><enabled>true</enabled></snapshots>
  30. </repository>
  31. <repository>
  32. <id>spring-milestones</id>
  33. <url>http://repo.spring.io/milestone</url>
  34. <snapshots><enabled>true</enabled></snapshots>
  35. </repository>
  36. </repositories>
  37. <pluginRepositories>
  38. <pluginRepository>
  39. <id>spring-snapshots</id>
  40. <url>http://repo.spring.io/snapshot</url>
  41. </pluginRepository>
  42. <pluginRepository>
  43. <id>spring-milestones</id>
  44. <url>http://repo.spring.io/milestone</url>
  45. </pluginRepository>
  46. </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
  1. Package com.sishuok.entity;
  2. /**
  3. * <p>user:zhang Kaitao
  4. * <p>date:13-12-22
  5. * <p>version:1.0
  6. */
  7. Public class User {
  8. private Long ID;
  9. private String name;
  10. Public Long getId () {
  11. return ID;
  12. }
  13. public void SetId (Long id) {
  14. this.id = ID;
  15. }
  16. Public String GetName () {
  17. return name;
  18. }
  19. public void SetName (String name) {
  20. this.name = name;
  21. }
  22. @Override
  23. public Boolean equals (Object o) {
  24. if (this= O) return true;
  25. if (o = = Null | | getclass ()! = O.getclass ()) return false;
  26. User user = (user) O;
  27. if (id! = null?!id.equals (user.id): user.id! = null) return false;
  28. return true;
  29. }
  30. @Override
  31. public int hashcode () {
  32. return ID ! = null? Id.hashcode (): 0;
  33. }
  34. }

Controller Java Code
  1. Package Com.sishuok.controller;
  2. Import Com.sishuok.entity.User;
  3. Import org.springframework.boot.SpringApplication;
  4. Import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  5. Import org.springframework.web.bind.annotation.PathVariable;
  6. Import org.springframework.web.bind.annotation.RequestMapping;
  7. Import Org.springframework.web.bind.annotation.RestController;
  8. /**
  9. * <p>user:zhang Kaitao
  10. * <p>date:13-12-22
  11. * <p>version:1.0
  12. */
  13. @EnableAutoConfiguration
  14. @RestController
  15. @RequestMapping ("/user")
  16. Public class Usercontroller {
  17. @RequestMapping ("/{id}")
  18. Public User View (@PathVariable ("id") Long ID) {
  19. User user = new user ();
  20. User.setid (ID);
  21. User.setname ("Zhang");
  22. return user;
  23. }
  24. //public static void Main (string[] args) {
  25. //Springapplication.run (Usercontroller.class);
  26. //}  
  27. }

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
  1. Package Com.sishuok;
  2. Import Com.sishuok.controller.UserController;
  3. Import org.springframework.boot.SpringApplication;
  4. Import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  5. Import Org.springframework.context.annotation.ComponentScan;
  6. Import org.springframework.context.annotation.Configuration;
  7. /**
  8. * <p>user:zhang Kaitao
  9. * <p>date:13-12-22
  10. * <p>version:1.0
  11. */
  12. @Configuration
  13. @ComponentScan
  14. @EnableAutoConfiguration
  15. Public class Application {
  16. public static void Main (string[] args) {
  17. Springapplication.run (Application.   Class);
  18. }
  19. }

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

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.