Spring Boot应用的快速构建
本文使用Maven方式快速构建Spring Boot应用
几分钟教程
这里, 我借助IDEA快速建立一个maven项目 (你也可以通过手动或者eclipse创建)
1.1 New Project
1.1.1 创建一个新项目, 使用maven的方式
1.1.2 填写一些应用相关的信息, GroupId, ArtifactId
1.1.3 填写项目保存的本地路径, 点击Finish
1.1.4 如上操作, 创建完成一个空的Maven项目
1.2 引入Spring Boot的依赖
1.2.1 编辑pom.xml文件
<?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>
<!-- 映入spring boot 依赖 -->
<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 编写项目启动的主入口代码
在 ./src/java/main
目录新建目录, 例如 info/chiwm/boot
. 所有的代码都在该目录下编辑, 这里定义为 代码根目录
创建java文件 Application.java
作为入口文件, 编码如下
package info.chiwm.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author [email protected]
* @ClassName: Application
* @Description:
* @date 2018/1/3 上午11:29
*/
@SpringBootApplication(scanBasePackages = "info.chiwm.log4j2")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
可以直接Application.java
中编写controller
业务逻辑并且启动.
1.2.3 编写 controller
层业务逻辑
但这里我将controller
模块的逻辑编排在相对应的目录下, 可以使示例代码组织得更加明了
在 `代码根目录` 下创建目录 `controller`, 在该目录下创建示例controller , `JsonController.java`
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 [email protected]
* @ClassName: JsonController
* @Description:
* @date 2018/1/3 下午1:11
*/
@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 启动项目
在IDEA中, RUN Application.java即可启动.
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: 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: default
2018-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 hierarchy
2018-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.23
2018-01-03 13:35:25.155 INFO 78070 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-01-03 13:35:25.155 INFO 78070 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1968 ms
2018-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 hierarchy
2018-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 startup
2018-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 started
2018-01-03 13:55:15.660 INFO 78070 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 34 ms
服务端启动, Maven方式打包, 命令启动也可..