使用Spring Tool Suite4创建Spring Boot Web项目

准备工作:

 (1)安装并配置好Spring Tool Suite

创建步骤:

(1)File->New->Spring Starter Project,输入项目名

                         使用Spring Tool Suite4创建Spring Boot Web项目_第1张图片

(2)勾选如下两项,点击Finish等待创建成功。

                         使用Spring Tool Suite4创建Spring Boot Web项目_第2张图片

(3)其中1为启动项目的文件,直接以app方式Run起来即可;2为html/css/js等静态资源的存储目录;3为App的配置文件。

                                              使用Spring Tool Suite4创建Spring Boot Web项目_第3张图片

(4)新建包com.example.web.controller,然后在包中创建文件TestController.java,内容为:

package com.example.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//这里很重要
@Controller
public class TestController {

	// 配置请求地址
	@RequestMapping(value = "/test")
	@ResponseBody
	public String test() {
		System.out.println("hello world");
		// 返回字符串ok给前端
		return "ok";
	}
}

(5)配置启动类,将

@SpringBootApplication

更改为:

@SpringBootApplication(scanBasePackages = "com.example.web.controller")

(6)在src/main/resources/static目录下创建index.html文件,内容为:





Insert title here


Hello world!

(7)创建src/main/resources/application.yml,内容为:

server:
  servlet:
    context-path: /test
  port: 80
  use-forward-headers: true
  tomcat:
    remote-ip-header: X-Real-IP
    protocol-header: X-Forwarded-Proto

其中80为访问端口,/test为访问基础路径

(8)配置src/main/resources/application.properties使其支持访问html文件

# 定位页面的目录到static/下
 spring.mvc.view.prefix=/
 spring.mvc.view.suffix=.html

(9)Run As Java Application或Spring Boot App直接运行启动文件:SpringbootDemo1Application.java  打印出下列日志则说明启动成功


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.6.RELEASE)

2019-08-02 15:47:30.442  INFO 788 --- [  restartedMain] c.e.demo.SpringbootDemo1Application      : Starting SpringbootDemo1Application on slave1 with PID 788 (D:\Eclipse-Spring\project\springboot-demo-1\target\classes started by Administrator in D:\Eclipse-Spring\project\springboot-demo-1)
2019-08-02 15:47:30.450  INFO 788 --- [  restartedMain] c.e.demo.SpringbootDemo1Application      : No active profile set, falling back to default profiles: default
2019-08-02 15:47:30.644  INFO 788 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2019-08-02 15:47:30.644  INFO 788 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2019-08-02 15:47:33.403  INFO 788 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 80 (http)
2019-08-02 15:47:33.489  INFO 788 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-08-02 15:47:33.489  INFO 788 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.21]
2019-08-02 15:47:33.771  INFO 788 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/test]   : Initializing Spring embedded WebApplicationContext
2019-08-02 15:47:33.771  INFO 788 --- [  restartedMain] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3127 ms
2019-08-02 15:47:34.390  INFO 788 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-08-02 15:47:34.689  INFO 788 --- [  restartedMain] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page: class path resource [static/index.html]
2019-08-02 15:47:34.784  INFO 788 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2019-08-02 15:47:34.891  INFO 788 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 80 (http) with context path '/test'
2019-08-02 15:47:34.895  INFO 788 --- [  restartedMain] c.e.demo.SpringbootDemo1Application      : Started SpringbootDemo1Application in 5.28 seconds (JVM running for 6.453)

(10)浏览器访问地址:http://localhost/test/index.html

使用Spring Tool Suite4创建Spring Boot Web项目_第4张图片

访问http://localhost/test/test

 使用Spring Tool Suite4创建Spring Boot Web项目_第5张图片

控制台打印出hello world

 

 

最后说一下出现404错误的原因:

使用Spring Tool Suite4创建Spring Boot Web项目_第6张图片

极有可能是这三个文件没有配置好,

SpringbootDemo1Application.java文件中的

@SpringBootApplication

一定要配置成:

@SpringBootApplication(scanBasePackages = "com.example.web.controller")

其它两个文件要配置成上面的内容,大概意思就是这样。

用过Python和Java的Web框架,最后发现Spring Boot创建项目的配置的确比Python的简单,并且Spring Boot还可以不依靠Tomcat容器就启动Web项目,感觉好神奇的样子。

你可能感兴趣的:(Java)