我们以前使用Spring框架的时候,需要首先在pom文件中增加对相关的的依赖,然后新建Spring相关的xml文件,而且往往那些xml文件还不会少。然后继续使用tomcat或者jetty作为容器来运行这个工程。基本上每次创建一个新的项目都是这么一个流程,而我们有时候仅仅想快速的创建一个Spring web工程来测试一些东西,或者是希望能节省时间。

现在我们使用Spring Boot就可以快速的做到这些了。

1. 我们创建一个Maven工程,工程名字为spring-boot-helloworld,然后修改pom.xml文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5.  
  6. <groupId>net.mingyang</groupId>
  7. <artifactId>spring-boot-helloworld</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. <packaging>jar</packaging>
  10.  
  11. <name>spring-boot-helloworld</name>
  12. <description>Demo project for Spring Boot</description>
  13.  
  14. <parent>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-parent</artifactId>
  17. <version>1.4.1.RELEASE</version>
  18. <relativePath/> <!-- lookup parent from repository -->
  19. </parent>
  20.  
  21. <properties>
  22. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  23. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  24. <java.version>1.7</java.version>
  25. </properties>
  26.  
  27. <dependencies>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-web</artifactId>
  31. </dependency>
  32.  
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-test</artifactId>
  36. <scope>test</scope>
  37. </dependency>
  38. </dependencies>
  39.  
  40. <build>
  41. <plugins>
  42. <plugin>
  43. <groupId>org.springframework.boot</groupId>
  44. <artifactId>spring-boot-maven-plugin</artifactId>
  45. </plugin>
  46. </plugins>
  47. </build>
  48.  
  49. </project>

2. 新建一个Controller来接受处理我们的请求:

  1. package net.mingyang.spring_boot_helloworld;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestMethod;
  8. import org.springframework.web.bind.annotation.ResponseBody;
  9.  
  10. @SpringBootApplication
  11. @Controller
  12. public class SimpleController {
  13.  
  14. @RequestMapping(value ="/hello", method = RequestMethod.GET)
  15. @ResponseBody
  16. public String hello(){
  17. return "hello world";
  18. }
  19.  
  20. public static void main(String[] args) {
  21. SpringApplication.run(SimpleController.class, args);
  22. }
  23. }

3. 执行SimleController中的main()方法:

  1. . ____ _ __ _ _
  2. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
  3. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
  4. \\/ ___)| |_)| | | | | || (_| | ) ) ) )
  5. ' |____| .__|_| |_|_| |_\__, | / / / /
  6. =========|_|==============|___/=/_/_/_/
  7. :: Spring Boot :: (v1.4.1.RELEASE)
  8.  
  9. 2016-10-18 08:43:46.946 INFO 4640 --- [ main] n.m.s.SimpleController : Starting SimpleController on D60601653 with PID 4640 (X:\dev\spring-test-suite\spring-boot-helloworld\target\classes started by lbin in X:\dev\spring-test-suite\spring-boot-helloworld)
  10. 2016-10-18 08:43:46.950 INFO 4640 --- [ main] n.m.s.SimpleController : No active profile set, falling back to default profiles: default
  11. 2016-10-18 08:43:47.238 INFO 4640 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@69cc1627: startup date [Tue Oct 18 08:43:47 CST 2016]; root of context hierarchy
  12. 2016-10-18 08:43:49.969 INFO 4640 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
  13. 2016-10-18 08:43:49.983 INFO 4640 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
  14. 2016-10-18 08:43:49.984 INFO 4640 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.5
  15. 2016-10-18 08:43:50.108 INFO 4640 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
  16. 2016-10-18 08:43:50.109 INFO 4640 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2874 ms
  17. 2016-10-18 08:43:50.334 INFO 4640 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
  18. 2016-10-18 08:43:50.340 INFO 4640 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
  19. 2016-10-18 08:43:50.340 INFO 4640 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
  20. 2016-10-18 08:43:50.340 INFO 4640 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
  21. 2016-10-18 08:43:50.340 INFO 4640 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
  22. 2016-10-18 08:43:50.766 INFO 4640 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@69cc1627: startup date [Tue Oct 18 08:43:47 CST 2016]; root of context hierarchy
  23. 2016-10-18 08:43:50.863 INFO 4640 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello],methods=[GET]}" onto public java.lang.String net.mingyang.spring_boot_helloworld.SimpleController.hello()
  24. 2016-10-18 08:43:50.868 INFO 4640 --- [ 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)
  25. 2016-10-18 08:43:50.868 INFO 4640 --- [ 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)
  26. 2016-10-18 08:43:50.912 INFO 4640 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  27. 2016-10-18 08:43:50.912 INFO 4640 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  28. 2016-10-18 08:43:51.029 INFO 4640 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  29. 2016-10-18 08:43:51.767 INFO 4640 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
  30. 2016-10-18 08:43:51.878 INFO 4640 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
  31. 2016-10-18 08:43:51.884 INFO 4640 --- [ main] n.m.s.SimpleController : Started SimpleController in 5.658 seconds (JVM running for 5.99)

查看日志可以发现默认使用的是tomcat,端口绑定在8080,访问:http://localhost:8080/hello,就可以看到我们代码中输出的字样了。

是不是很简单?

Spring Boot快速开发Web项目的更多相关文章

  1. 10个Spring Boot快速开发的项目,接私活利器(快速、高效)

    本文为大家精选了 码云 上优秀的 Spring Boot 语言开源项目,涵盖了企业级系统框架.文件文档系统.秒杀系统.微服务化系统.后台管理系统等,希望能够给大家带来一点帮助:) 1.项目名称:分布式 ...

  2. spring boot + Thymeleaf开发web项目

    "Spring boot非常适合Web应用程序开发.您可以轻松创建自包含的HTTP应用.web服务器采用嵌入式Tomcat,或者Jetty等.大多数情况下Web应用程序将使用 spring- ...

  3. Spring Boot 是 Spring 的一套快速配置脚手架,可以基于Spring Boot 快速开发单个微服务

    Spring Boot 是 Spring 的一套快速配置脚手架,可以基于Spring Boot 快速开发单个微服务,Spring Cloud是一个基于Spring Boot实现的云应用开发工具:Spr ...

  4. Spring Boot构建的Web项目如何在服务端校验表单输入

    本文首发于个人网站:Spring Boot构建的Web项目如何在服务端校验表单输入 这个例子用于演示在Spring Boot应用中如何验证Web 应用的输入,我们将会建立一个简单的Spring MVC ...

  5. Spring Boot快速建立HelloWorld项目

    Spring Boot使我们更容易去创建基于Spring的独立和产品级的可以”即时运行“的应用和服务.支持约定大于配置,目的是尽可能快地构建和运行Spring应用. 构建环境 JDK 6+ Maven ...

  6. Spring Boot快速搭建Web工程

    先想一下,正常我们想要创建一个web服务,首先需要下载tomcat,创建web工程,配置各种web.xml,引入spring的配置,各种配置文件一顿倒腾.....下载有了spring boot,你创建 ...

  7. spring boot之创建web项目并访问jsp页面

    1,创建spring boot的web项目 刚创建好的项目路径如下: 2,pom中要有下面的依赖 <dependency> <groupId>org.springframewo ...

  8. 在Idea创建Spring Boot + MyBatis的web项目

    创建步骤如下 选择Spring initializr  2. 修改group 与 atifact id,点击next 3. dependencies里面选择Web->Web , SQL -> ...

  9. Spring Boot部署之 web项目war包运行

    传统的部署方式:将项目打成war包,放入tomcat 的webapps目录下面,启动tomcat,即可访问. 具体打war包流程: 1.pom.xml配置文件修改: 2.改造启动类,如果是war包发布 ...

随机推荐

  1. Ubuntu vi 常用命令集合

    :w 保存文件但不退出vi:w file 将修改另外保存到file中,不退出vi:w! 强制保存,不推出vi:wq 保存文件并退出vi:wq! 强制保存文件,并退出viq: 不保存文件,退出vi:q! ...

  2. UE4 去除不正确的水面倒影以及不完整镜头轮廓

    最近在做的项目遇到了一点点问题,出现了如下效果 视角对着湖面移动会出现一个显示不完整的轮廓(比较长的蓝色矩形),详细一点就是下图这样,以及近处物体的倒影(从光照的照射角度来看是不应该出现的) 一开始就 ...

  3. CCI4.5/LintCode Validate Binary Search Tree

    Validate BST是指按中序遍历niorder后左<node<右: 第一种方法: 先按inoreder遍历, 再放进ArrayList里用循环看是不是从小到大排序: 注意: 设置成员 ...

  4. Java笔记7-多态父类静态

    多态的应用-面向父类编程 1.对象的编译时类型写成父类 2.方法的返回类型写成父类 3.方法的参数类型写成父类 编译时类型:对象的声明时类型,在于编译期间 运行时类型:new运算符后面的类型 编译时类 ...

  5. Servlet---RequestDispatcher.include方法

    为了实现代码重用,需要将某些代码和数据放在一个或多个Servlet中,以供其他Servlet使用,提供了RequestDispatcher.include方法,首先通过getServletContex ...

  6. Flask-SQLAlchemy 的操作

    from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) db = SQLAlchemy(app) ================= ...

  7. golang 文件操作

    package main import (     "bytes"     "fmt"     "io"     "os" ...

  8. kbengine0.4.20源代码分析(一)

    基于kbengine 0.4.2 MMOPG服务端是一种高品质的工程项目,品读开源的kbe是一种乐趣.本文档我带童鞋们一起领略一下.囿于我知识面和经验方面所限,文中所述之处难免有错误存在,还请读童鞋们 ...

  9. c# List去重

    1 list如果数据是值类型,比如list<int> 这种,添加linq之后就可以使用list = list.Distinct().ToList(); 2 如果是数据是引用类型,比如中间是 ...

  10. unity panel删除drawcall失败导致的残留影像

    ngui panel 被隐藏或者删除的时候调用ondisable,清空drawcall,如果这个操作是在ontriggerenter等物理操作中就会删除不掉导致留下残影 解决方式 : 讲这些操转移到协 ...