前言
最近研究websocket 实时通信的问题,对于构建环境问题,带来困扰,本人做前端,对于后端接触最早在2015年左右吧!当时是.net 平台,近年来,Java 发展日益激增,慢慢有所接触,为了方便接下来的websoket的实践,在这里,以另外一种方式构建后端环境,【本人新手,老手勿喷,新手参考】
以gradle 构建为例
1.系统环境
2.JAVA 环境
3.gradle 环境
4.开发工具IDE
前期的准备工作完成啦,接下来开始创建项目
- 打开网站Spring Initializr
- 按照下图填写信息
- 下载之后解压,打开ecplise,导入
- 导入完成的结构
- 启动工程
注意: 启动会出现启动失败
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.0.M3)
2019-05-16 14:15:34.174 INFO 4636 --- [ main] com.stone.web.WebApplication : Starting WebApplication on WIN-5I8015M1EB5 with PID 4636 (D:\dest\web\bin\main started by Administrator in D:\dest\web)
2019-05-16 14:15:34.177 INFO 4636 --- [ main] com.stone.web.WebApplication : No active profile set, falling back to default profiles: default
2019-05-16 14:15:34.560 INFO 4636 --- [ main] com.stone.web.WebApplication : Started WebApplication in 0.663 seconds (JVM running for 0.988)
- 修改配置文件 加入如下代码
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.1.5.RELEASE'
- 刷新Gradle
- 成功之后,在重新启动,出现下面的信息
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.0.M3)
2019-05-16 14:32:46.190 INFO 6432 --- [ main] com.stone.web.WebApplication : Starting WebApplication on WIN-5I8015M1EB5 with PID 6432 (D:\dest\web\bin\main started by Administrator in D:\dest\web)
2019-05-16 14:32:46.194 INFO 6432 --- [ main] com.stone.web.WebApplication : No active profile set, falling back to default profiles: default
2019-05-16 14:32:47.154 INFO 6432 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-05-16 14:32:47.190 INFO 6432 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-05-16 14:32:47.192 INFO 6432 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.19]
2019-05-16 14:32:47.334 INFO 6432 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-05-16 14:32:47.334 INFO 6432 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1083 ms
2019-05-16 14:32:47.597 INFO 6432 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-05-16 14:32:47.764 INFO 6432 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-05-16 14:32:47.766 INFO 6432 --- [ main] com.stone.web.WebApplication : Started WebApplication in 1.891 seconds (JVM running for 2.251)
- 浏览器直接访问 http://127.0.0.1:8080, 看到如下界面。
- 接下来增加控制器Controller
- TestController 的代码
package com.stone.web;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping("/test")
public String test () {
return "test gradle";
}
}
- 重新运行项目,输入 http://127.0.0.1:8080/test