SpringBoot教程(三)

如何更快速的建立一个SpringBoot应用?

之前我们都是需要创建一个Maven工程,然后在pom文件中导入依赖(参照官方文档用什么模块就把该模块的starter导入进去),然后需要编写一个主程序,然后在编写一个业务逻辑,这样都太麻烦了

idea支持根据创建SpringBoot创建向导来快速的创建SpringBoot项目
File-->New-->Project...

以前我们选择的是Maven工程,现在我们选择Spring Initializr

我们选择好jdk然后点Next

点Next,接下来是选择模块

我们说SpringBoot把所有的场景都抽取成了一个一个的starter,如果需要哪个场景就导入哪个starter,我们在这里需要什么功能就勾选哪个模块,我们这里选择web,点击Next

名称都不用改,点击Finish,此时向导会联网从SpringBoot创建项目

创建出来的没有用的东西就可以删除掉

我们点进pom.xml
我们看到它自己导入了与咱们前面手动配置的一样的模块

它还加入了SpringBoot的单元测试的模块spring-boot-starter-test


我们选择右下角的“Enable Auto-Import” ,让它自动导包

如果没有导入也可以点击右侧的Maven Projects

然后选中咱们现在的这个项目,然后点击左上角的像刷新的按钮,它的意思是重新导入所有的Maven项目

我们点击进入主程序,我们看到跟我们的写法一样,它都帮你做好了

但是没有Controller,我们自己实现一个Controller,来进行测试


我们后期的一些项目,可能都用REST API的方式,也就是收到浏览器请求后,把数据直接交给浏览器,而不是页面跳转,那么每一个方法后来可能都要加@ResponseBody,太麻烦了,所以我们需要将@ResponseBody加到类的上面,意思是这个类的所有方法返回的数据,都直接写给浏览器(如果是对象就转为json数据),类似于以下这样

如果经常标注@ResponseBody @Controller 也很麻烦,我们可以用@RestController来代替,它是Spring4.2以上新加入的功能

我们点击@RestController,进去看一下,它其实就是@ResponseBody @Controller的合体

我们现在启动主程序,看是否能运行

在启动的时候如果出现类似下面这样的错误
 

2018-09-13 10:24:57.345 ERROR 5908 --- [           main] o.apache.catalina.core.StandardService   : Failed to start connector [Connector[HTTP/1.1-8080]]

org.apache.catalina.LifecycleException: Failed to start component [Connector[HTTP/1.1-8080]]
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
	at org.apache.catalina.core.StandardService.addConnector(StandardService.java:225) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
	at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.addPreviouslyRemovedConnectors(TomcatWebServer.java:256) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start(TomcatWebServer.java:198) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.startWebServer(ServletWebServerApplicationContext.java:300) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:162) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553) [spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:780) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:333) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1277) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1265) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at com.david.springboot01helloworldqiuck.SpringBoot01HelloworldQiuckApplication.main(SpringBoot01HelloworldQiuckApplication.java:10) [classes/:na]
Caused by: org.apache.catalina.LifecycleException: Protocol handler start failed
	at org.apache.catalina.connector.Connector.startInternal(Connector.java:1020) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
	... 13 common frames omitted
Caused by: java.net.BindException: Address already in use: bind
	at sun.nio.ch.Net.bind0(Native Method) ~[na:1.8.0_172]
	at sun.nio.ch.Net.bind(Net.java:433) ~[na:1.8.0_172]
	at sun.nio.ch.Net.bind(Net.java:425) ~[na:1.8.0_172]
	at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223) ~[na:1.8.0_172]
	at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74) ~[na:1.8.0_172]
	at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:219) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
	at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:1151) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
	at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:591) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
	at org.apache.catalina.connector.Connector.startInternal(Connector.java:1018) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
	... 14 common frames omitted

2018-09-13 10:24:57.347  INFO 5908 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2018-09-13 10:24:57.355  INFO 5908 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-09-13 10:24:57.358 ERROR 5908 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

The Tomcat connector configured to listen on port 8080 failed to start. The port may already be in use or the connector may be misconfigured.

Action:

Verify the connector's configuration, identify and stop any process that's listening on port 8080, or configure this application to listen on another port.

2018-09-13 10:24:57.358  INFO 5908 --- [           main] ConfigServletWebServerApplicationContext : Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@3745e5c6: startup date [Thu Sep 13 10:24:55 CST 2018]; root of context hierarchy
2018-09-13 10:24:57.360  INFO 5908 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

Process finished with exit code 1


说明:你的机器的8080端口有人在占用,请排查.
我的出现这个的原因是因为上次用java -jar命令执行了上回打的那个应用的jar包,所以占用了8080端口,然后这次启动的时候就报这个错误.关掉这个cmd窗口以后,再次启动就不报错了.
这次访问http://localhost:8080/hello就返回正确的结果了

在生成的路径中,包含有resources路径,
其中
static: 保存所有的静态资源  js   css   images
templates: 保存所有的模板页面(SpringBoot默认打包jar包,并使用嵌入式的Tomcat,默认是不支持JSP页面的,但是我们可以使用模板引擎(freemarker、thymeleaf),可以在这个文件夹里面放入该模板引擎的页面)
application.properties: SpringBoot应用的配置文件,SpringBoot原来都是默认配置的,自动配置的,但是我们都可以在这里面重新再次指定,可以在这里修改默认的配置


例如:我们更改一下application.properties
 

server.port=8081

我们再次启动服务器
我们发现端口号变为8081了

我们用浏览器访问http://localhost:8080/hello 就什么都没有返回了
我们用http://localhost:8081/hello 就返回成功了

备注:我们的@RequestMapping("/hello")其实可以写为这种形式:
@RequestMapping(value="/hello",method = RequestMethod.GET)  可以指定获取浏览器数据的方法


 

猜你喜欢

转载自blog.csdn.net/dqxiaoxiao/article/details/82666837