SpringBoot学习笔记(一、HelloSpringBoot)

网友投稿 579 2022-05-30

一、Springboot简介

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”…Most Spring Boot applications need very little Spring configuration.

1

这是官方文档的介绍。

Spring Boot(英文中是“引导”的意思),是用来简化Spring应用的搭建到开发的过程。应用开箱即用。

在学习SSM(H)的过程中,需要做大量的配置工作,其实很多配置行为本身只是手段,并不是目的。 基于这个考虑,把该简化的简化,该省略的省略,开发人员只用关心提供业务功能就行了,这就是 SpringBoot。

换言之,SpringBoot可以简单地看成简化了的、按照约定开发的SSM(H),可以使开发速度大大提升。

二、使用Eclipse创建一个Maven项目

这一步的前提是你的电脑完成了Maven的相关配置。

详情如图:

三、具体代码

1、pom.xml

4.0.0 edu.hpu SpringBoot 0.0.1-SNAPSHOT SpringBoot org.springframework.boot spring-boot-starter-parent 1.3.3.RELEASE org.springframework.boot spring-boot-starter-web junit junit 4.12

1

2

3

4

5

SpringBoot学习笔记(一、HelloSpringBoot)

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

2、 Controller层

HelloController.java

import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/hello") public String sayHello() { return "Hello,SpringBoot!"; } }

1

2

3

4

5

6

7

8

9

10

@RestController和@RequestMapping注解是来自SpringMVC的注解,它们不是SpringBoot的特定部分。

(1). @RestController:提供实现了REST API,可以服务JSON,XML或者其他。这里是以String的形式渲染出结果。

(2). @RequestMapping:提供路由信息,”/hello“路径的HTTP Request都会被映射到sayHello方法进行处理。

3、启动应用类

Application.java

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /*Spring Boot启动类 * */ @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

四、运行

运行也比较简单,将Application.java运行一下

好,看到Application运行没有问题,访问网址:http://localhost:8080/hello

好的这次启动没有问题,到这没有完,我对Controller作了点修改,刷新网页,看不到修改内容,那就第二次运行Application.java,结果报了下面这个错误。

java.net.BindException: Address already in use: bind at sun.nio.ch.Net.bind0(Native Method) ~[na:1.8.0_144] at sun.nio.ch.Net.bind(Unknown Source) ~[na:1.8.0_144] at sun.nio.ch.Net.bind(Unknown Source) ~[na:1.8.0_144] at sun.nio.ch.ServerSocketChannelImpl.bind(Unknown Source) ~[na:1.8.0_144] at sun.nio.ch.ServerSocketAdaptor.bind(Unknown Source) ~[na:1.8.0_144] at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:340) ~[tomcat-embed-core-8.0.32.jar:8.0.32] at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:765) ~[tomcat-embed-core-8.0.32.jar:8.0.32] at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:473) ~[tomcat-embed-core-8.0.32.jar:8.0.32] at org.apache.catalina.connector.Connector.startInternal(Connector.java:986) [tomcat-embed-core-8.0.32.jar:8.0.32] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) [tomcat-embed-core-8.0.32.jar:8.0.32] at org.apache.catalina.core.StandardService.addConnector(StandardService.java:239) [tomcat-embed-core-8.0.32.jar:8.0.32] at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.addPreviouslyRemovedConnectors(TomcatEmbeddedServletContainer.java:194) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE] at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:151) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE] at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:293) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE] at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:141) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) [spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE] at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE] at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE] at edu.hpu.springboot.Application.main(Application.java:13) [classes/:na]

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

这个错误的解决办法查了一下,最后找到一个靠谱的,错误原因是8080这个端口被占用,Application虽然跑不起来,http://localhost:8080/hello 照样能访问,关于SpringBoot的运行机制我不太清楚,但大概知道之前运行之后应该生成了什么,占用这个端口。所以查一下:

cmd -> netstat -ano

结果:

找到被哪个进程被占用了,打开任务管理器,点击详细信息

就是它,javaw.exe,结束任务,再次运行Application.java,访问http://localhost:8080/hello ,没有问题,Controller中更改的内容也显示出来了。

参考:

【1】https://www.w3cschool.cn/springboot/springboot-w7c2245h.html

【2】http://how2j.cn/k/springboot/springboot-eclipse/1640.html

【3】https://www.cnblogs.com/13188196we/p/3278153.html

Java Spring Spring Boot

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:如何在JS中使用AJAX技术
下一篇:JAVA 从菜鸟成长为大牛的必经之路
相关文章