zoukankan      html  css  js  c++  java
  • Spring Boot入门程序

    一、环境准备

    1、配置maven环境jdk版本为1.8

    在E:DevelopingDevToolsapache-maven-3.6.1confsettings.xml文件下新增配置

    <profile>
    	<id>jdk-1.8</id>
    	<activation>
    		<activeByDefault>true</activeByDefault>
    		<jdk>1.8</jdk>
    	</activation>
    	
    	<properties>
    		<maven.compiler.source>1.8</maven.compiler.source>
    		<maven.compiler.target>1.8</maven.compiler.target>
    		<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
    	</properties>
    </profile>
    

    2、整合Eclipse和Maven

    • 配置工作空间字体(fonts)
    • 配置工作空间编码格式为UTF-8(workspace)
    • 配置jdk(installed JREs)
    • 配置maven安装位置和本地仓库位置(maven)

    二、入门程序

    • 新建maven项目,打为jar包即可

    • 导入springboot相关依赖

      <!-- Inherit defaults from Spring Boot -->
      <parent>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-parent</artifactId>
          <version>1.5.22.RELEASE</version>
      </parent>
      
      <dependencies>
          <dependency>
              <!-- Import dependency management from Spring Boot -->
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-web</artifactId>
          </dependency>
      </dependencies>
      

      三、编写一个主程序,用于启动spring boot应用

      package com.fei;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      
      /**
       * @SpringBootApplication 来标注一个主程序类,说明这是一个spring boot应用
       * @author Del
       *
       */
      @SpringBootApplication
      public class HelloWorldMainApplication {
      	public static void main(String[] args) {
      
      		// 启动springboot应用
      		SpringApplication.run(HelloWorldMainApplication.class, args);
      	}
      }
      

      四、编写相关的Controller、Service

      package com.fei.controller;
      
      import org.springframework.stereotype.Controller;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.ResponseBody;
      
      @Controller
      public class HelloWorldController {
      
      	@ResponseBody
      	@RequestMapping("/hello")
      	public String hello() {
      		return "Hello World!";
      	}
      }
      

      五、像运行一般Java程序那样运行主程序

      在浏览器输入地址http://localhost:8080/hello即可看到Hello World!

      六、简化部署工作

      导入插件

      <build>
          <plugins>
              <!-- 这个插件,可以将应用打包成一个可执行的jar包 -->
              <plugin>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-maven-plugin</artifactId>
              </plugin>
          </plugins>
      </build>
      

      打包,cmd窗口,通过java -jar spring-boot-01-helloworld-0.0.1-SNAPSHOT.jar的方式运行。

  • 相关阅读:
    IOException while loading persisted sessions:java.io.EOFException
    Android Studio | 详细安装教程
    Android -- 关闭AsyncTask(异步任务)
    钢铁侠传-文言文
    http协议 get/post 请求 解析XML
    HTTP状态码大全
    jquery+ajax 类百度输入框
    这就是知识点
    关于Eclipse+SVN 开发配置
    企业信息化快速开发平台--JeeSite
  • 原文地址:https://www.cnblogs.com/zxfei/p/11588348.html
Copyright © 2011-2022 走看看