SpringBoot学习笔记(一、HelloSpringBoot)

一、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.

这是官方文档的介绍。
Spring Boot(英文中是“引导”的意思),是用来简化Spring应用的搭建到开发的过程。应用开箱即用。
在学习SSM(H)的过程中,需要做大量的配置工作,其实很多配置行为本身只是手段,并不是目的。 基于这个考虑,把该简化的简化,该省略的省略,开发人员只用关心提供业务功能就行了,这就是 SpringBoot。
换言之,SpringBoot可以简单地看成简化了的、按照约定开发的SSM(H),可以使开发速度大大提升。

二、使用Eclipse创建一个Maven项目
这一步的前提是你的电脑完成了Maven的相关配置。
详情如图:
在这里插入图片描述

三、具体代码
1、pom.xml

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>edu.hpu</groupId>
  <artifactId>SpringBoot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringBoot</name>
  <!-- Spring Boot 启动父依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
    </parent>
    <dependencies>
        <!-- Spring Boot web依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
</project>

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!";
   }
}

@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);

	}

}

四、运行
运行也比较简单,将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]

这个错误的解决办法查了一下,最后找到一个靠谱的,错误原因是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

猜你喜欢

转载自blog.csdn.net/sinat_40770656/article/details/89328235