java鬼混笔记:springboot 1、第一个springboot项目例子

来源:互联网 发布:js获取input file的流 编辑:程序博客网 时间:2024/05/17 03:46

//////////////

springboot是什么?

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。 --百度百科


第一个springboot项目的创建(来自于官网)

首先创建一个maven项目,然后在pom.xml文件添加以下代码:

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.7.RELEASE</version></parent><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>

然后创建一个Example.java

import org.springframework.boot.*;import org.springframework.boot.autoconfigure.*;import org.springframework.stereotype.*;import org.springframework.web.bind.annotation.*;@RestController(包含responbody)@EnableAutoConfiguration(自动配置)
//Enable auto-configuration of the Spring Application Context, attempting to guess and configure beans that you are likely to need. Auto-configuration classes are usually applied based on your classpath and what beans you have defined.public class Example {    @RequestMapping("/")    String home() {        return "Hello World!";    }    public static void main(String[] args) throws Exception {        SpringApplication.run(Example.class, args);    }}

接着在Example.java中右键--run as Java Application(平时那种执行java方式),然后控制输出:

  .   ____          _            __ _ _ /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/  ___)| |_)| | | | | || (_| |  ) ) ) )  '  |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot ::        (v1.5.7.RELEASE)2017-10-15 17:28:28.915  INFO 151304 --- [           main] f.Example                                : Starting Example on YWJ with PID 151304 (G:\eclipseworkspace\f\target\classes started by Administrator in G:\eclipseworkspace\f)2017-10-15 17:28:28.946  INFO 151304 --- [           main] f.Example                                : No active profile set, falling back to default profiles: default2017-10-15 17:28:29.197  INFO 151304 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@133e16fd: startup date [Sun Oct 15 17:28:29 CST 2017]; root of context hierarchy2017-10-15 17:28:31.572  INFO 151304 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)2017-10-15 17:28:31.600  INFO 151304 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]2017-10-15 17:28:31.600  INFO 151304 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.202017-10-15 17:28:31.803  INFO 151304 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext2017-10-15 17:28:31.803  INFO 151304 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2747 ms2017-10-15 17:28:32.022  INFO 151304 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]2017-10-15 17:28:32.022  INFO 151304 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]2017-10-15 17:28:32.022  INFO 151304 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]2017-10-15 17:28:32.022  INFO 151304 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]2017-10-15 17:28:32.022  INFO 151304 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]2017-10-15 17:28:32.459  INFO 151304 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@133e16fd: startup date [Sun Oct 15 17:28:29 CST 2017]; root of context hierarchy2017-10-15 17:28:32.537  INFO 151304 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String f.Example.home()2017-10-15 17:28:32.537  INFO 151304 --- [           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)2017-10-15 17:28:32.537  INFO 151304 --- [           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)2017-10-15 17:28:32.569  INFO 151304 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2017-10-15 17:28:32.569  INFO 151304 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2017-10-15 17:28:32.600  INFO 151304 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2017-10-15 17:28:32.737  INFO 151304 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup2017-10-15 17:28:32.847  INFO 151304 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)2017-10-15 17:28:32.862  INFO 151304 --- [           main] f.Example                                : Started Example in 4.808 seconds (JVM running for 7.576)

看倒数第2行输出:Tomcat started on port(s): 8080 (http),也这是这个项目访问的端口是8080
现在打开浏览器,输入 http://127.0.0.1:8080/ 显示:Hello World!


OK,就这么简单。

阅读全文
0 0
原创粉丝点击