第一步:创建一个普通的maven项目

第二步:配置springboot基础依赖配置(即配置pom和启动类)

POM配置

<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>spring-boot-example</groupId>
<artifactId>spring-boot-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-example</name>
<description>spring-boot-example</description> <!-- 继承父包 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.19.RELEASE</version>
<relativePath></relativePath>
</parent> <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> <properties>
<java.version>1.8</java.version>
</properties> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

配置启动类

@RestController      // @RestController  相当于 @Controller 加上  @ResponseBody
@EnableAutoConfiguration
public class HelloController { public static void main(String[] args) {
SpringApplication.run(HelloController.class, args);
} @RequestMapping("/example/hello")
public int hello(){
return 1;
} }

最后,启动springboot应用

方式一:eclipse中直接运行main方法

方式二:命令行执行  java -jar spring-boot-example.jar &

方式三:打包成war文件,放tomcat中运行

结果如下图所示:

发布的http服务测试:http://localhost:8080/example/hello,测试成功!,如下图所示

关于打包成war形式,放入tomcat中运行需要注意:

第一步:修改pom文件:新增黑色字体部分内容

<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>spring-boot-example</groupId>
<artifactId>spring-boot-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-example</name>
<description>spring-boot-example</description>
<packaging>war</packaging> <!-- 继承父包 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.19.RELEASE</version>
<relativePath></relativePath>
</parent> <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> <!-- 确保内置servlet container 不会干涉发布该war包的servlet container,方案是标记内置servlet container 的依赖为 provided -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

</dependencies>

第二步:更改程序入口类 Application.java 使其继承SpringBootServletInitializer,并重写configure方法,(即加粗字体部分)

@RestController      // @RestController  相当于 @Controller 加上  @ResponseBody
@SpringBootApplication
public class HelloController extends SpringBootServletInitializer{ @Override
public SpringApplicationBuilder configure(SpringApplicationBuilder application){
return application.sources(HelloController.class);
}
public static void main(String[] args) {
SpringApplication.run(HelloController.class, args);
} @RequestMapping("/example/hello")
public int hello(){
return 1;
}
}

用 tomcat验证结果:

spring boot 入门之 helloworld的更多相关文章

  1. Spring Boot入门 and Spring Boot与ActiveMQ整合

    1.Spring Boot入门 1.1什么是Spring Boot Spring 诞生时是 Java 企业版(Java Enterprise Edition,JEE,也称 J2EE)的轻量级代替品.无 ...

  2. Spring Boot 入门之基础篇(一)

    原文地址:Spring Boot 入门之基础篇(一) 博客地址:http://www.extlight.com 一.前言 Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是 ...

  3. 161103、Spring Boot 入门

    Spring Boot 入门 spring Boot是Spring社区较新的一个项目.该项目的目的是帮助开发者更容易的创建基于Spring的应用程序和服务,让更多人的人更快的对Spring进行入门体验 ...

  4. spring boot 入门操作(二)

    spring boot入门操作 使用FastJson解析json数据 pom dependencies里添加fastjson依赖 <dependency> <groupId>c ...

  5. spring boot 入门操作(三)

    spring boot入门操作 devtools热部署 pom dependencies里添加依赖 <dependency> <groupId>org.springframew ...

  6. Spring Boot入门教程1、使用Spring Boot构建第一个Web应用程序

    一.前言 什么是Spring Boot?Spring Boot就是一个让你使用Spring构建应用时减少配置的一个框架.约定优于配置,一定程度上提高了开发效率.https://zhuanlan.zhi ...

  7. Spring Boot入门教程2-1、使用Spring Boot+MyBatis访问数据库(CURD)注解版

    一.前言 什么是MyBatis?MyBatis是目前Java平台最为流行的ORM框架https://baike.baidu.com/item/MyBatis/2824918 本篇开发环境1.操作系统: ...

  8. Spring Boot 入门教程

    Spring Boot 入门教程,包含且不仅限于使用Spring Boot构建API.使用Thymeleaf模板引擎以及Freemarker模板引擎渲染视图.使用MyBatis操作数据库等等.本教程示 ...

  9. Spring Boot入门(五):使用JDBC访问MySql数据库

    本系列博客记录自己学习Spring Boot的历程,如帮助到你,不胜荣幸,如有错误,欢迎指正! 在程序开发的过程中,操作数据库是必不可少的部分,前面几篇博客中,也一直未涉及到数据库的操作,本篇博客 就 ...

随机推荐

  1. 解决springboot jar包冲突

    直接导入springboot父项依赖,其它相关springboot依赖version不用写,由spring自动依赖. <parent> <groupId>org.springf ...

  2. 关于使用jwt编写接口时候对token判断时候错误的机制处理

    前言:php在使用接口时候很多时候都是需要带token的,如果不对token进行校验那么别人就能够随意编写一个token进入你的接口拿数据,应该怎样处理呢? //生成token public func ...

  3. ORACLE not available

    1 先看oracle的监听和oracle的服务是否都启动了. 启动oracle监听:cmd命令行窗口下,输入lsnrctl start,回车即启动监听. 2 查看oracle的sid叫什么,比如创建数 ...

  4. SimpleDateFormat 线程不安全及解决方案

    SimpleDateFormat定义 SimpleDateFormat 是一个以与语言环境有关的方式来格式化和解析日期的具体类.它允许进行格式化(日期 -> 文本).解析(文本 -> 日期 ...

  5. Linux 文件格式转码工具

    Linux 系统下文件编码转换格式工具 ICONV 下载 https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.15.tar.gz 源码安装: $ ./con ...

  6. 背景上实现阴影——linear-gradient

    /*从元素顶部有条阴影,两种方式,第二种更好,能控制阴影的宽度*/background-image: linear-gradient(0deg, rgba(226, 226, 226, 0) 97%, ...

  7. 97w网站邀请码

    我是一个老粉丝了. 如果想要97w网站邀请码可以留下联系方式,给我发过去哦. 最好是邮箱. http://www.97w.net/tag.html

  8. python 中 dlib库的安装

    安装 dlib 库的时候需要用到 CMake 进行本地编译,而Cmake又是基于Visual Studio运行的,我在装这个库的时候,各种找不到教程,就想着分享一下自己的经验. 32位 python3 ...

  9. Linux安装Jdk Tomcat MySQL

    Jdk安装 Tomcat安装 Mysql安装 Jdk安装 官网下载 http://www.oracle.com/technetwork/java/javase/downloads/index.html ...

  10. Windows Server 2012安装.NET Framework 3.5

    1 文件获取地址 链接:https://pan.baidu.com/s/1o7wykEKKrzbVslSeHbBigA 提取码:e59o 2 安装原因 在Windows Server 2012上安装一 ...