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. 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 方法
这里写图片描述

转载于:https://www.cnblogs.com/cnblog-long/p/7235163.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值