• SpringBoot Maven项目 Helloworld 测试


    SpringBoot

    化繁为简,简化配置

    SpringBoot官方:http://projects.spring.io/spring-boot/
    SpringBoot使用介绍:http://blog.csdn.net/isea533/article/details/50278205

    Maven项目使用SpringBoot

    建议使用 IDEA ,虽然下面是 Eclipse 测试的例子

    配置SpringBoot

    pom.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
        <modelversion>4.0.0</modelversion>
        <groupid>com.springboot</groupid>
        SpringBootTest</artifactid>
        <packaging>war</packaging>
        <version>0.0.1-SNAPSHOT</version>
        <name>SpringBootTest Maven Webapp</name>
        <url>http://maven.apache.org</url>
     
        <properties>
            <project.build.sourceencoding>UTF-8</project.build.sourceencoding>
            <java.version>1.8</java.version>
            <tomcat.version>7.0.55</tomcat.version>
        </properties>
     
        <parent>
            <groupid>org.springframework.boot</groupid>
            spring-boot-starter-parent</artifactid>
            <version>1.3.0.RELEASE</version>
        </parent>
        <dependencies>
            <dependency>
                <groupid>org.springframework.boot</groupid>
                spring-boot-starter-web</artifactid>
            </dependency>
        </dependencies>
     
    </project>

    运行程序

    运行main方法

    SampleController.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    package com.hello;
     
    import java.util.Date;
     
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
     
    @RestController
    @EnableAutoConfiguration (exclude={DataSourceAutoConfiguration.class})
    public class SampleController {
     
        @RequestMapping("/")
        String home() {
            return "Hello World!";
        }
     
        @RequestMapping("/now")
        String hehe() {
            return "现在时间:" + (new Date()).toLocaleString();
        }
     
        public static void main(String[] args) throws Exception {
            SpringApplication.run(SampleController.class, args);
        }
    }

    控制台输出:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
      .   ____          _            __ _ _
     /\ / ___'_ __ _ _(_)_ __  __ _
    ( ( )\___ | '_ | '_| | '_ / _` |
     \/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::        (v1.3.0.RELEASE)
     
    2016-12-15 16:30:13.765  INFO 20952 --- [           main] com.hello.SampleController               : Starting SampleController on DESKTOP-QSFD0OC with PID 20952 (D:eclipse4.61eclipseworkspaceSpringBootTest argetclasses started by Peng in D:eclipse4.61eclipseworkspaceSpringBootTest)
    2016-12-15 16:30:13.768  INFO 20952 --- [           main] com.hello.SampleController               : No profiles are active
    2016-12-15 16:30:13.803  INFO 20952 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@149494d8: startup date [Thu Dec 15 16:30:13 CST 2016]; root of context hierarchy
    ...

    浏览器上输入地址http://localhost:8080/

    这里写图片描述

    在浏览器上输入地址http://localhost:8080/now

    这里写图片描述

    注意事项

    Tomcat问题一

    这里写图片描述
    Java Build Path中,不要加入 Tomcat 运行环境的依赖库,已经在pom.xml中指定了 Tomcat 版本(不指定默认为 Tomcat 8.0.28),从 maven 的依赖包中能找到 Tomcat 的jar包,SpringBoot 自己配有 Tomcat 。

    可以参考:http://blog.csdn.net/zhang168/article/details/51423905

    Tomcat问题二

    运行 main 方法,出现 Tomcat 不能启动的错误。如下面的异常信息

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    java.net.BindException: Address already in use: bind
        at sun.nio.ch.Net.bind0(Native Method) ~[na:1.8.0_91]
        at sun.nio.ch.Net.bind(Unknown Source) ~[na:1.8.0_91]
        at sun.nio.ch.Net.bind(Unknown Source) ~[na:1.8.0_91]
    org.apache.catalina.LifecycleException: Failed to start component [Connector[org.apache.coyote.http11.Http11NioProtocol-8080]]
     
    Caused by: org.apache.catalina.LifecycleException: service.getName(): "Tomcat";  Protocol handler start failed
        at org.apache.catalina.connector.Connector.startInternal(Connector.java:1014) ~[tomcat-embed-core-7.0.55.jar:7.0.55]
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ~[tomcat-embed-core-7.0.55.jar:7.0.55]
        ... 13 common frames omitted
    Caused by: java.net.BindException: Address already in use: bind
     
     Unable to start embedded Tomcat servlet container
     
     Caused by: java.lang.IllegalStateException: Tomcat connector in failed state

    原因是你运行了程序一次,javaw.exe 会占用你的端口,可以结束该进程。重新运行 main 方法
    这里写图片描述

  • 相关阅读:
    YGC问题排查,又让我涨姿势了!
    AI时代,还不了解大数据?
    实战篇:一个核心系统 3 万行代码的重构之旅
    监控系统选型,这篇不可不读!
    实时离线一体大数据在资产租赁saas服务中使用
    基于监控服务打造微服务治理生态体系
    CDH6.3.2升级Hive到4.0.0
    强化学习 9 —— DQN 改进算法DDQN、Dueling DQN tensorflow 2.0 实现
    强化学习 8 —— DQN 算法 Tensorflow 2.0 实现
    强化学习 7——Deep Q-Learning(DQN)公式推导
  • 原文地址:https://www.cnblogs.com/cnblog-long/p/7235163.html
Copyright © 2020-2023  润新知