您好,登錄后才能下訂單哦!
這篇文章主要介紹springboot中如何構(gòu)建簡單項(xiàng)目,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
概述
相信對(duì)于Java開發(fā)者而言,spring和springMvc兩個(gè)框架一定不陌生,這兩個(gè)框架需要我們手動(dòng)配置的地方非常多,各種的xml文件,properties文件,構(gòu)建一個(gè)項(xiàng)目還是挺復(fù)雜的,在這種情況下,springboot應(yīng)運(yùn)而生,他能夠快速的構(gòu)建spring項(xiàng)目,而且讓項(xiàng)目正常運(yùn)行起來的配置文件非常少,甚至只需要幾個(gè)注解就可以運(yùn)行整個(gè)項(xiàng)目。
總的說來,springboot項(xiàng)目可以打成jar包獨(dú)立運(yùn)行部署,因?yàn)樗鼉?nèi)嵌servlet容器,之前spring,springMvc需要的大量依賴,可以通過starter來幫助我們簡化配置,當(dāng)然還有其他好多優(yōu)點(diǎn),這里就不一一贅述,小伙伴們可以自行搜索解答。
簡單項(xiàng)目構(gòu)建
工具
eclipse maven
首先,我們新建一個(gè)maven項(xiàng)目,在eclipse左側(cè)右擊選擇new----》other,選擇新建Maven project
輸入group Id,artifact Id,點(diǎn)擊完成
這樣一個(gè)簡單的項(xiàng)目架子就完成了,但是啥都沒有,項(xiàng)目結(jié)構(gòu)如下圖所示:
下面我們就開始配置搭建springboot項(xiàng)目。
1.添加依賴
完整porm代碼如下:
<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>com.cfxmn.springboot</groupId> <artifactId>springbootDemo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <!-- 通過繼承spring-boot-starter-parent項(xiàng)目來獲得一些合理的默認(rèn)配置 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- Spring Boot Web 依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Boot Test 依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 使用Lombok可以減少很多重復(fù)代碼的書寫。比如說getter/setter/toString等方法的編寫 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> </project>
下面我們新建一些包和添加項(xiàng)目的啟動(dòng)類,如下圖所示:
其中,控制器DemoController的內(nèi)容非常簡單,內(nèi)容如下:
package com.cfxmn.springboot.springbootDemo.controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import lombok.extern.slf4j.Slf4j; @RestController @Slf4j public class DemoController { @PostMapping("/demo") public void demoTest() { // 這邊簡單起見,打印一下日志 log.info("success call"); } }
可能有些同學(xué)對(duì)其中的幾個(gè)注解有些疑問,我這邊簡單說明下,
1.RestController
這個(gè)注解其實(shí)就是@ResponseBody + @Controller
2.PostMapping
這個(gè)注解其實(shí)就是@RequestMapping("xxxxxx", Method=RequestMethod.POST)
這兩個(gè)其實(shí)都是組合注解,簡化使用
我們?cè)賮砜纯?,?xiàng)目的啟動(dòng)類SpringbootDemoApplication的內(nèi)容:
package com.cfxmn.springboot.springbootDemo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringbootDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringbootDemoApplication.class, args); } }
是的,你沒看錯(cuò),只要運(yùn)行這個(gè)main方法,就能啟動(dòng)這個(gè)spring項(xiàng)目,具體是怎么啟動(dòng)的容器,我們之后再分析,其實(shí)主要就是在注解SpringBootApplication上。
下面我們就來運(yùn)行下,看下啟動(dòng)日志:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.6.RELEASE) 2018-10-25 23:52:41.985 INFO 1700 --- [ main] c.c.s.s.SpringbootDemoApplication : Starting SpringbootDemoApplication on DESKTOP-KB78HJK with PID 1700 (E:\workspace\springbootDemo\target\classes started by gepengfa in E:\workspace\springbootDemo) 2018-10-25 23:52:41.990 INFO 1700 --- [ main] c.c.s.s.SpringbootDemoApplication : No active profile set, falling back to default profiles: default 2018-10-25 23:52:42.088 INFO 1700 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7f416310: startup date [Thu Oct 25 23:52:42 CST 2018]; root of context hierarchy 2018-10-25 23:52:44.561 INFO 1700 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 2018-10-25 23:52:44.584 INFO 1700 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2018-10-25 23:52:44.588 INFO 1700 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.16 2018-10-25 23:52:44.813 INFO 1700 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2018-10-25 23:52:44.813 INFO 1700 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2733 ms 2018-10-25 23:52:45.074 INFO 1700 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 2018-10-25 23:52:45.083 INFO 1700 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2018-10-25 23:52:45.083 INFO 1700 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2018-10-25 23:52:45.083 INFO 1700 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2018-10-25 23:52:45.085 INFO 1700 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2018-10-25 23:52:45.582 INFO 1700 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7f416310: startup date [Thu Oct 25 23:52:42 CST 2018]; root of context hierarchy 2018-10-25 23:52:45.705 INFO 1700 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/demo],methods=[POST]}" onto public void com.cfxmn.springboot.springbootDemo.controller.DemoController.demoTest() 2018-10-25 23:52:45.710 INFO 1700 --- [ 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-10-25 23:52:45.711 INFO 1700 --- [ 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-10-25 23:52:45.759 INFO 1700 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-10-25 23:52:45.759 INFO 1700 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-10-25 23:52:45.817 INFO 1700 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-10-25 23:52:46.321 INFO 1700 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2018-10-25 23:52:46.529 INFO 1700 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 2018-10-25 23:52:46.599 INFO 1700 --- [ main] c.c.s.s.SpringbootDemoApplication : Started SpringbootDemoApplication in 5.092 seconds (JVM running for 5.764)
從啟動(dòng)日志標(biāo)黃的部分可以看出,項(xiàng)目啟動(dòng)成功了,訪問端口默認(rèn)是8080(這個(gè)端口是可以改動(dòng)的)
下面我們通過postMan請(qǐng)求下,
查看控制臺(tái)
2018-10-25 23:59:26.385 INFO 1700 --- [nio-8080-exec-2] c.c.s.s.controller.DemoController : success call
說明調(diào)用成功。
到此,一個(gè)簡單的springboot項(xiàng)目就構(gòu)建完成了,但這只是一個(gè)空的架子,內(nèi)容還可載豐富。
以上是“springboot中如何構(gòu)建簡單項(xiàng)目”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。