Spring Boot入门之HelloWorld

笔记摘要

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。
Spring Boot特性:
- 创建独立的Spring应用程序
- 嵌入的Tomcat,无需部署WAR文件
- 简化Maven配置
- 自动配置Spring
- 提供生产就绪型功能,如指标,健康检查和外部配置
- 开箱即用,没有代码生成,也无需XML配置
Spring Boot特性理解:
- 为基于Spring的开发提供更快的入门体验
- 开箱即用,没有代码生成,也无需XML配置。同时也可以修改默认值来满足特定的需求。
- 提供了一些大型项目中常见的非功能特性,如嵌入式服务器,安全,指标,健康监测,外部配置等。
- Spring Boot并不是Spring功能上的增强,而是提供了一种快速使用Spring的方式。


Eclipse实现HelloWorld

实验过程:

Created with Raphaël 2.1.0 开始 新建Maven工程,设置Group Id为my.spring,Artifact Id为helloworld,其余默认 编辑pom.xml文件,并Update Maven Project 在my.spring.helloworld包下新建MyController类 修改App类和MyController类 运行App类中的main方法 使用浏览器打开http://localhost:8080/helloworld。 结束

pom.xml文件配置
pom.xml文件配置后工程会报错,Update Maven Project即可。

<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>my.spring</groupId>
  <artifactId>helloworld</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>helloworld</name>
  <url>http://maven.apache.org</url>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8.0_144</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>
</project>

MyController类代码

package my.spring.helloworld;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MyController {
    @RequestMapping("/helloworld")
    @ResponseBody
    public String hello() {
        return "helloworld";
    }
}

App类代码

package my.spring.helloworld;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Hello world!
 *
 */
@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class, args);
    }
}

BUG记录

  1. 配置pom.xml时节点下参数为1.4.1.RELEASE时,Update Maven Project系统报错,值更换为1.5.4.RELEASE时可正常运行;

  2. MyController类未使用@Controller注解导致HTTP 404错误;

  3. MyController类未使用@ResponseBody注解导致Whitelabel Error Page错误。

    Whitelabel Error Page
    This application has no explicit mapping for /error, so you are seeing this as a fallback.
    Wed Mar 07 21:19:30 CST 2018
    There was an unexpected error (type=Internal Server Error, status=500).
    Circular view path [helloworld]: would dispatch back to the current handler URL [/helloworld] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值