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.61\eclipse\workspace\SpringBootTest\target\classes started by Peng in D:\eclipse4.61\eclipse\workspace\SpringBootTest)
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 方法

SpringBoot Maven项目 Helloworld 测试的更多相关文章

  1. 工具IDEA 配置springboot+maven项目

    工具IDEA 配置springboot+maven项目 首先安装IDEA,至于怎么安装就不介绍了.. 第一步 配置maven环境 首先安装maven,先在网上下载一个maven包.在IDEA的sett ...

  2. springboot maven项目,为什么build成功,build path也没错误,project-->clean 也没用,项目上面还是有个红x呢?

    springboot maven项目,为什么build成功,build path也没错误,project-->clean 也没用,项目上面还是有个红x呢? 看错误信息有提示:  Descript ...

  3. IDEA maven项目下测试mybatis例子,使用mappper class或package引入mapper映射文件,总是报错Invalid bound statement(所有配置完全正确)

    困扰几个小时,终于查到解决办法及原因(可以直接到最后看解决方案) 环境就是用IDEA搭建的maven项目,主要jar包引入配置如下 <dependencies> <dependenc ...

  4. springboot maven项目运行常见报错 及ajax请求报错

    如图所示 tomcat运行后直接停止,也不报错 原因:我的原因是controller路径配置重名或者service没有配置@Service 遇见这错找了好久问题,网上也搜不到,特此记录一下 问题2 a ...

  5. maven项目对于测试时“无法加载主类”的解决方案

    1.右键maven项目,选择  build path --------->Configure Build Path  ,执行下列操作,保存即可.

  6. github导入springboot maven项目

    1.在GitHub里force喜欢的项目,获取GitHub项目地址,eclipse---import---project from git---clone uri---next---finish,项目 ...

  7. idea打包springboot+maven项目并发布在linux上

    2018年11月13日我亲测有效的,很简单的,借鉴博客:https://blog.csdn.net/smilecall/article/details/56288972 第一步:随便建一个maven类 ...

  8. springboot maven项目转gradle的完整方法

    1.maven转gradle的方法:在项目根目录下,使用命令行工具,输入如下内容: gradle init --type.pom 2.springboot项目的 build.gradle内容示例如下( ...

  9. springBoot maven项目打成jar包

    springBoot项目打包springBoot项目打包最常用且最简单的方式是用springBoot的打包plugin <plugin> <groupId>org.spring ...

随机推荐

  1. 动态BT跳转

    METHOD eh_onzobject_id_link. ***定义 DATA : lv_crmt_object_guid TYPE crmt_object_guid, lv_index TYPE i ...

  2. OpenJDK换为JDK(CentOS)

    说明:应该来说没必要非把OpenJDK卸载掉,只要让$PATH中我们安装的jdk的目录较OpenJDK所在的/usr/bin先出现就好了:简言之跳过下边的第一步直接从第二步开始更科学一些. 1.卸载O ...

  3. 把旧系统迁移到.Net Core 2.0 日记 (13) --图形验证码

    参考这篇文章: http://www.cnblogs.com/yuangang/p/6000460.html using System; using System.IO; using System.D ...

  4. shiro jwt 构建无状态分布式鉴权体系

    一:JWT 1.令牌构造 JWT(json web token)是可在网络上传输的用于声明某种主张的令牌(token),以JSON 对象为载体的轻量级开放标准(RFC 7519). 一个JWT令牌的定 ...

  5. python3.7 安装

    python3.7 安装 下载安装 cd /usr/localwget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgztar -xvf ...

  6. POJ 1001 Exponentiation(大数运算)

    POJ 1001 Exponentiation 时限:500 ms   内存限制:10000 K 提交材料共计: 179923   接受: 43369 描述:求得数R( 0.0 < R < ...

  7. X的平方

    题目描述: 实现 int sqrt(int x) 函数,计算并返回 x 的平方根. 样例 sqrt(3) = 1 sqrt(4) = 2 sqrt(5) = 2 sqrt(10) = 3 题解: 解法 ...

  8. java this的用法

    this 含义:代表当前对象 用法: 用于返回对象的引用 示例代码 public class Test { public Test f() { return this;//获取当前对象的引用 } pu ...

  9. inode占用100%时硬盘无法写入文件故障处理

    故障现象: 分区无法写入文件. 故障分析: 执行df -h命令发现空间占用不到50%,执行df -hi,发现某分区IUse%值为99%,说明innode已经用完,应该是某些目录下存在大量的小文件导致. ...

  10. day24 模块03_re

    休养生息 --模块03 1.正则表达式 2.在python中使用正则.re 一,正则表达式 (匹配字符串,主要是给字符串使用的) 1)元字符 .  除换行符之外 \w 数字,字母,下划线组成   \W ...