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
|
<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> <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 。
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
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 百万级群聊的设计实践
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期