- Download demo from official website, address: http://start.spring.io/
We're going to build a maven-based, Java, Web project, and then click Generate Project Download project (need to have a foundation for MAVEN)
- Download the unzip and import it into Eclipse (other Ides are also available, as long as MAVEN is supported)
- This directory is still more familiar, this way to mention the resources directory of the static and templates directory: Static resources for storage, compared to slices, CSS, JS, and so on, and templates for the Web page template file
- MAVEN Configuration Analysis
<?XML version= "1.0" encoding= "UTF-8"?><Projectxmlns= "http://maven.apache.org/POM/4.0.0"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupId>Com.example</groupId> <Artifactid>Demo</Artifactid> <version>0.0.1-snapshot</version> <Packaging>Jar</Packaging> <name>Demo</name> <Description>Demo Project for Spring Boot</Description> <Parent> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-parent</Artifactid> <version>2.0.3.RELEASE</version> <RelativePath/> <!--Lookup parent from repository - </Parent> <Properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </Properties> <Dependencies> <Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-web</Artifactid> </Dependency> <Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-test</Artifactid> <Scope>Test</Scope> </Dependency> </Dependencies> <Build> <Plugins> <plugin> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-maven-plugin</Artifactid> </plugin> </Plugins> </Build></Project>
Analysis points:
- <packaging>jar</packaging>:spring-boot defaults to packaging the Web app as a jar, rather than as a traditional war form, because the default Web module dependency will contain embedded Tomcat , so that our application jar itself has the ability to provide Web services;
- The parent project, which is specified as a 2.0.3.RELEASE version of Spring-boot-starter-parent, defines the underlying dependencies of the spring boot version and the contents of some of the default configurations in the parent project. such as the location of the configuration file application.properties;
- The build part of the project, which introduces the spring boot maven plugin, is very useful and can help us to start and stop the application conveniently, so that we do not have to go to the Brahma class or package it as a jar to run the microservices at development time, only through MVN spring-boot: The Run command quickly launches the Spring boot application ( I try to get rid of the build and start the service by starting with the following three startup methods, which can be started by running the main function and the MVN spring-boot:run. But after the install, and then Java-jar Xxx.jar mode can not start )
- The target depends on the dependencies configuration:
- Spring-boot-starter-web: Full stack Web development module with embedded tomcat, spring MVC
- Spring-boot-starter-test: Generic test module with JUnit, Hamcrest, Mockito
- The Web and test modules referenced here are referred to as starter POMs in the Spring boot ecosystem. Starter Poms is a series of light-weight dependencies, a one-stop spring-related technology solution, developers in the use and integration of modules, no longer need to search the sample code in the dependency configuration to replicate the use, only need to introduce the corresponding module package. For example, when developing the Web, you introduce Spring-boot-starter-web, and when you want to access database capabilities, Then introduce SPRING-BOOT-STARTER-JDBC or better SPRING-BOOT-STARTER-DATA-JPA. When building applications using Spring-boot, the integration of functional modules is no longer like the way traditional spring applications are developed, but requires a lot of dependency configuration in Pom.xml, which makes functional module integration very lightweight by using starter Poms-defined dependency packages. , easy to understand and use.
- Spring-boot's starter Poms is named by Spring-boot-starter-*, which represents a particular piece of application functionality.
- Implementing the RESTful API: creating a RESTful API in Spring-boot the same implementation code as the spring MVC application, but without having to do a lot of configuration, like Spring MVC does, and start writing the controller content directly like this
- New package, named Com.example.demo.web, can be modified to its own path according to the actual build situation (beginners need to pay attention to this path, otherwise it will not be accessed)
- New Hellocontroller Class
Package Com.example.demo.web; Import org.springframework.web.bind.annotation.RequestMapping; Import Org.springframework.web.bind.annotation.RestController; @RestController Public class Hellocontroller { @RequestMapping ("/hello") public String index () { return "Hello World"; }}
Launch the app and access the Http://localhost:8080/hello through the browser, we can see that the expected results are returned: Hello World
- There are a number of ways to start Spring-boot applications (the following three types are available):
- Unit Test
PackageCom.example.demo;ImportOrg.junit.Before;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;Importorg.springframework.boot.SpringBootConfiguration;Importorg.springframework.boot.autoconfigure.SpringBootApplication;Importorg.springframework.boot.test.context.SpringBootTest;ImportOrg.springframework.http.MediaType;ImportOrg.springframework.test.context.junit4.SpringJUnit4ClassRunner;ImportOrg.springframework.test.context.junit4.SpringRunner;Importorg.springframework.test.context.web.WebAppConfiguration;ImportORG.SPRINGFRAMEWORK.TEST.WEB.SERVLET.MOCKMVC;Importorg.springframework.test.web.servlet.request.MockMvcRequestBuilders;Importorg.springframework.test.web.servlet.setup.MockMvcBuilders;Import StaticOrg.hamcrest.Matchers.equalTo;Import Staticorg.springframework.test.web.servlet.result.MockMvcResultMatchers.content;Import StaticOrg.springframework.test.web.servlet.result.MockMvcResultMatchers.status;ImportCom.example.demo.web.HelloController;ImportCom.fasterxml.jackson.databind.Module.SetupContext;ImportCh.qos.logback.core.status.Status;//@RunWith (Springrunner.class)//@SpringBootTest@RunWith (Springjunit4classrunner.class) @SpringBootTest (Classes= DemoApplication.class) @WebAppConfiguration Public classdemoapplicationtests {PrivateMOCKMVC MVC; @Before Public voidSetUp ()throwsException {MVC= Mockmvcbuilders.standalonesetup (NewHellocontroller ()). build (); } @Test Public voidContextloads ()throwsException {mvc.perform (Mockmvcrequestbuilders.get ("/hello"). Accept (Mediatype.application_json)). Andexpect (Status (). IsOk ()). Andexpect (Content (). String (Equalto ( c5>"Hello World"))); }}
Analytical:
- @RunWith (Springjunit4classrunner.class): Introduction of Spring support for JUNIT4
- @SpringBootTest (classes = demoapplication.class): Specifies the startup class for spring boot
- @WebAppConfiguration: Open the configuration of the Web app for simulating ServletContext
- Mockmvc object: An interface originating request used to impersonate a calling controller, in a Hello test case defined by @test, the Perform function performs a request call, and the accept is used to execute the received data type. Andexpect used to determine the expected value of an interface return
- @Before: JUnit defines what is preloaded before a test case @test content is executed, which is used to initialize the Hellocontroller simulation
- Note the following static references are introduced to make the status, content, Equalto functions available
PS: Learning from "Spring Cloud micro-service Combat"
Second, Spring-boot Quick Start