您的位置:首页 > 编程语言 > Java开发

springboot快速入门(一)——HelloWorld搭建

jiangbei 2018-02-07 15:03 113 查看

一、起步

  1.先导    凡技术必登其官网的原则,官网走一波https://projects.spring.io/spring-boot/#quick-start     极力推荐一个springboot教程https://gitee.com/didispace/SpringBoot-Learning  2.springboot优点    官网原版:
  FeaturesCreate stand-alone Spring applicationsEmbed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)Provide opinionated 'starter' POMs to simplify your Maven configurationAutomatically configure Spring whenever possibleProvide production-ready features such as metrics, health checks and externalized configurationAbsolutely no code generation and no requirement for XML configuration  翻译:特征创建独立的Spring应用程序直接嵌入Tomcat,Jetty或Undertow(无需部署WAR文件)提供自己的“入门”POM来简化您的Maven配置尽可能自动配置Spring提供生产就绪功能,如指标,运行状况检查和外部配置绝对不会生成代码也不需要XML配置

二、第一个HelloWorld程序

    1.网页工具产生脚手架      参考http://blog.didispace.com/spring-boot-learning-1/      使用eclipse的话,通过sts的new->spring starter project也是OK的    2.使用IDEA内嵌Spring Initializr      File==>New==>Project,选择Spring Initializr,地址指向官网的initial地址,这里较新版本的IDEA已经提供了默认Url:
      
<?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>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>springboot_demo</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.4.7.RELEASE</version><relativePath/> <!-- lookup parent from repository --></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><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
pom.xml      [b]默认项目结构:
        其中,默认生成的类 SpringbootDemoApplication 是启动的主类(加了@SpringBootApplication)        application.properties是springboot的配置文件          4.启动项目      通过IDEA的方式启动:==========推荐        在主类里面右键——>run即可               // 出现ERROR属于正常,因为没有配置任何类!      编写一个测试类:
package com.example.demo;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;/*** 测试demo的controller** @author zcc ON 2018/2/8**/@RestControllerpublic class HelloController {@RequestMapping(value = "/hello", method = RequestMethod.GET)public String hello() {return "Hello Spring Boot!";}}
      重新启动,查看结果:            通过maven方式启动:    进入到项目目录(windows可以在界面进入目录后,通过右键shift在此处打开命令窗口,当然,win10已经有power shell了!)
mvn spring-boot:run
    关闭只需熟悉的Ctrl+C即可!    通过java -jar启动:
先到项目根目录mvn installcd targetjava -jar   xxxx.jar
标签: