Spring Boot quick Configuration
Rapid Spring Boot application Construction
This article uses Maven to quickly build a Spring Boot Application
Several minutes tutorial
Here, I use IDEA to quickly create a maven Project (you can also create it manually or by using eclipse)
1.1 New Project1.1.1 create a New project using maven
1.1.2 enter application-related information, such as GroupId and ArtifactId.
1.1.3 enter the local path for saving the project and click Finish
1.1.4 as shown above, an empty Maven project is created
1.2 introduce Spring Boot dependency 1.2.1 to edit the pom. xml file
<? Xml version = "1.0" encoding = "UTF-8"?> <Project xmlns = "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> info. chiwm </groupId> <artifactId> log4j2 </artifactId> <version> 1.0-SNAPSHOT </version> <! -- Reflect the spring boot dependency --> <parent> <groupId> org. springframework. boot </groupId> <artifactId> spring-boot-starter-parent </artifactId> <version> 1.5.9.RELEASE </version> </parent> <dependencies> <dependency> <groupId> org. springframework. boot </groupId> <artifactId> spring-boot-starter-web </artifactId> </dependency> </dependencies> </project>
1.2.2 compile the main entry code for project startup
In./src/java/main
Directory to create a directory, suchinfo/chiwm/boot
. All code is edited in this directory, which is definedCode root directory
Create a java FileApplication.java
As the entry file, the encoding is as follows:
Package info. chiwm. boot; import org. springframework. boot. springApplication; import org. springframework. boot. autoconfigure. springBootApplication;/*** @ author chiwm@kuyumall.com * @ ClassName: Application * @ Description: * @ date 2018/1/3 am */@ SpringBootApplication (scanBasePackages = "info. chiwm. log4j2 ") public class Application {public static void main (String [] args) {SpringApplication. run (Application. class, args );}}
You can directlyApplication.java
Writecontroller
The business logic is started.
1.2.3 write
controller
Layer business logic
But here I willcontroller
The logic of the module is arranged in the corresponding directory, so that the sample code can be organized more clearly.
Create the 'controller' directory under 'Code root directory', and create the example controller, 'jsoncontroller. Java' in this directory'
Package info. chiwm. boot. controller; import org. springframework. web. bind. annotation. requestMapping; import org. springframework. web. bind. annotation. requestMethod; import org. springframework. web. bind. annotation. responseBody; import org. springframework. web. bind. annotation. restController; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse;/*** @ author wanminchi@gmail.com * @ ClassName: JsonController * @ Description: * @ date 2018/1/3 */@ RestController @ RequestMapping ("/json ") public class JsonController {@ RequestMapping (value = "/get", method = RequestMethod. GET) @ ResponseBody public String getJson (HttpServletRequest req, HttpServletResponse res) {return "{\" name \ ": \" chi \"}";}}
1.3 start the project
In IDEA, RUN Application. java can be started.
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.9.RELEASE)2018-01-03 13:35:23.092 INFO 78070 --- [ main] info.chiwm.boot.Application : Starting Application on chiwanmindeMacBook-Pro.local with PID 78070 (/Users/chiwanmin/code/java/boot/target/classes started by chiwanmin in /Users/chiwanmin/code/java/boot)2018-01-03 13:35:23.096 INFO 78070 --- [ main] info.chiwm.boot.Application : No active profile set, falling back to default profiles: default2018-01-03 13:35:23.192 INFO 78070 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@74a10858: startup date [Wed Jan 03 13:35:23 CST 2018]; root of context hierarchy2018-01-03 13:35:25.083 INFO 78070 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)2018-01-03 13:35:25.092 INFO 78070 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]2018-01-03 13:35:25.093 INFO 78070 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.232018-01-03 13:35:25.155 INFO 78070 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext2018-01-03 13:35:25.155 INFO 78070 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1968 ms2018-01-03 13:35:25.258 INFO 78070 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]2018-01-03 13:35:25.261 INFO 78070 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]2018-01-03 13:35:25.261 INFO 78070 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]2018-01-03 13:35:25.261 INFO 78070 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]2018-01-03 13:35:25.261 INFO 78070 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]2018-01-03 13:35:25.645 INFO 78070 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@74a10858: startup date [Wed Jan 03 13:35:23 CST 2018]; root of context hierarchy2018-01-03 13:35:25.712 INFO 78070 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/json/get],methods=[GET]}" onto public java.lang.String info.chiwm.boot.controller.JsonController.getJson(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)2018-01-03 13:35:25.715 INFO 78070 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)2018-01-03 13:35:25.715 INFO 78070 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)2018-01-03 13:35:25.740 INFO 78070 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2018-01-03 13:35:25.741 INFO 78070 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2018-01-03 13:35:25.776 INFO 78070 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2018-01-03 13:35:25.912 INFO 78070 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup2018-01-03 13:35:25.993 INFO 78070 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)2018-01-03 13:35:25.999 INFO 78070 --- [ main] info.chiwm.boot.Application : Started Application in 3.522 seconds (JVM running for 4.575)2018-01-03 13:55:15.625 INFO 78070 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'2018-01-03 13:55:15.626 INFO 78070 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started2018-01-03 13:55:15.660 INFO 78070 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 34 ms
Start the server, package it in Maven mode, and start the command ..