springboot之helloworld


本例将采用maven管理,代码托管在github上,地址:https://github.com/wolf909867753/springboot

1。创建maven-module,并在pom.xml中添加springboot依赖

  

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>helloWorld</artifactId>
    <packaging>war</packaging>
    <name>springboot-helloWorld Demo</name>
    <url>http://maven.apache.org</url>

    <!-- Spring Boot 启动父依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>

    <dependencies>
        <!-- Spring Boot web依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>helloWorld</finalName>
    </build>
</project>


2.创建helloworldController类
@RestController
public class HelloWorld {

    @RequestMapping("/")
    public String helloWorld() {
        return "Hello World!";
    }

}

3.测试类
@SpringBootApplication
public class HelloWorldTest {
        public static void main(String[] args) {
            SpringApplication.run(HelloWorldTest.class,args);
        }
}
运行main方法,访问http://127.0.0.1:8080后页面输出Hello World!

小结:
/**
 * 一个最简单的Web应用 测试类参考test目录下的HelloWorldTest.java
 * 使用Spring Boot框架可以大大加速Web应用的开发过程,首先在Maven项目依赖中引入spring-boot-starter-web

 *  运行应用:mvn spring-boot:run或在IDE中运行main()方法,
 *  在浏览器中访问http://localhost:8080,Hello World!就出现在了页面中。
 *  只用了区区十几行Java代码,一个Hello World应用就可以正确运行了,那么这段代码究竟做了什么呢?
 *  们从程序的入口SpringApplication.run(HelloWorld.class, args);开始分析:
 *  SpringApplication是Spring Boot框架中描述Spring应用的类,
 *  它的run()方法会创建一个Spring应用上下文(Application Context)。另一方面它会扫描当前应用类路径上的依赖,
 *  例如本例中发现spring-webmvc(由 spring-boot-starter-web传递引入)在类路径中,那么Spring Boot会判断这是一个Web应用,
 *  并启动一个内嵌的Servlet容器(默认是Tomcat)用于处理HTTP请求。
 *  Spring WebMvc框架会将Servlet容器里收到的HTTP请求根据路径分发给对应的@Controller类进行处理,
 *  @RestController是一类特殊的@Controller,它的返回值直接作为HTTP Response的Body部分返回给浏览器。
 *  @RequestMapping注解表明该方法处理那些URL对应的HTTP请求,也就是我们常说的URL路由(routing),请求的分发工作是有Spring完成的。
 *  例如上面的代码中http://localhost:8080/根路径就被路由至helloWorld()方法进行处理。
 *  如果访问http://localhost:8080/hello,则会出现404 Not Found错误,因为我们并没有编写任何方法来处理/hello请求。
 *
 * @SpringBootApplication -----Spring Boot应用启动类
 * 在本例中也可以去掉@SpringBootApplication 直接使用以下代码也可以运行
 * Created by bysocket on 16/4/26.
    @SpringBootApplication
    public class HelloWorldTest {
        public static void main(String[] args) {
            SpringApplication.run(HelloWorldTest.class,args);
        }
    }
 * 如果将@RestController ----> @Controller,运行main method将会报以下Error
 * Whitelabel Error Page
    This application has no explicit mapping for /error, so you are seeing this as a fallback.
    Sat Jun 17 14:30:28 CST 2017
    There was an unexpected error (type=Not Found, status=404).
    No message available
 *
 */



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值