一,第一步创建Maven工程,步骤如下
二,pom文件添加SpringBoot 依赖,我们第一次实例演示web 所以一并添加web starter:
<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.springboot.webdemo</groupId><artifactId>FirstDemo</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.13.RELEASE</version><relativePath /></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></dependencies>
</project>
3,主要文件内容:
创建启动类SpringbootApplication:
package com.springboot;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class SpringbootApplication {public static void main(String[] args){SpringApplication.run(SpringbootApplication.class, args);}
}
创建Controller 类:
package com.springboot.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class FirstController {@GetMapping("/helloWord")public String helloWord(){return "hello Word";}
}
在jsrc/mian/resource下创建SpringBoot配置文件application.properties
server.port=8080
项目目录结构:
├─src
│ │
│ ├─main
│ │ ├─java
│ │ │ └─com
│ │ │ └─springboot
│ │ │ │ SpringbootApplication.java
│ │ │ │
│ │ │ └─controller
│ │ │ FirstController.java
│ │ │
│ │ └─resources
│ │ application.properties
│ │
│ └─test
│ ├─java
│ └─resources
├─ pom.xml
4,启动SpringBoot 容器
在启动类文件上右键-》run as => java application
打印日志如下:
. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: (v1.5.6.RELEASE)2018-05-22 22:07:57.992 INFO 84724 --- [ main] c.x.springboot.SpringbootApplication : Starting SpringbootApplication on lenovo-PC with PID 84724 (F:\asiainfo\projectName\target\classes started by lenovo in F:\asiainfo\projectName)
2018-05-22 22:07:57.997 INFO 84724 --- [ main] c.x.springboot.SpringbootApplication : No active profile set, falling back to default profiles: default
2018-05-22 22:07:58.074 INFO 84724 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3439f68d: startup date [Tue May 22 22:07:58 CST 2018]; root of context hierarchy
2018-05-22 22:08:00.011 INFO 84724 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 9090 (http)
2018-05-22 22:08:00.022 INFO 84724 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-05-22 22:08:00.023 INFO 84724 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.16
2018-05-22 22:08:00.144 INFO 84724 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-05-22 22:08:00.144 INFO 84724 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2077 ms
2018-05-22 22:08:00.325 INFO 84724 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2018-05-22 22:08:00.331 INFO 84724 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-05-22 22:08:00.332 INFO 84724 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-05-22 22:08:00.332 INFO 84724 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-05-22 22:08:00.332 INFO 84724 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-05-22 22:08:00.716 INFO 84724 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3439f68d: startup date [Tue May 22 22:07:58 CST 2018]; root of context hierarchy
2018-05-22 22:08:00.806 INFO 84724 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/helloWord],methods=[GET]}" onto public java.lang.String com.xiaohui.springboot.controller.TestController.helloWord()
2018-05-22 22:08:00.810 INFO 84724 --- [ 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-05-22 22:08:00.810 INFO 84724 --- [ 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-05-22 22:08:00.847 INFO 84724 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-22 22:08:00.847 INFO 84724 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-22 22:08:00.898 INFO 84724 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-22 22:08:01.099 INFO 84724 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-05-22 22:08:01.209 INFO 84724 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 9090 (http)
2018-05-22 22:08:01.215 INFO 84724 --- [ main] c.x.springboot.SpringbootApplication : Started SpringbootApplication in 3.6 seconds (JVM running for 3.972)
2018-05-22 22:08:10.834 INFO 84724 --- [nio-9090-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-05-22 22:08:10.835 INFO 84724 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2018-05-22 22:08:10.858 INFO 84724 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 23 ms
5,访问请求:http://127.0.0.1:8080/helloWorld
结果如图:
6,遇到的问题:
在启动的过程中有可能启动失败提示:Unable to start embedded container; nested exception is java.lang.NoSuchMethodError: org.apache.tomcat.util.scan.StandardJarScanner.setJarScanFilter
问题有可能为springBoot 与 tomcat容器中jar包冲突。移除掉tomcat的jar包就可以参考:链接
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态