实现一个简单的SpringBoot示例
首先是pom文件
<?xml version="1.0" encoding="UTF-8"?> <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>cn.studio</groupId> <!--模块名称--> <artifactId>springboot-01</artifactId> <!----> <version>0.0.1-SNAPSHOT</version> <!--以jar的形式--> <packaging>jar</packaging> <name>springboot-01</name> <description>Demo project for Spring Boot</description> <!--spring-boot-starter-parent:包含了大量配置好的依赖管理, 在自己项目添加这些依赖的时候不需要写<version>版本号--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <!--配置的jdk版本是1.8--> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 可以实现热部署 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <!--更换SpringBoot中默认的web容器为jetty--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <!--使用jetty服务器,替换tomcat--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> </dependencies> <build> <plugins> <!--插件--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
另外在application.properties中可以修改端口号
server.port=9090
编写一个controller
package cn.studio; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController //可以是返回json格式的 public class Springboot01Application { //在页面上的访问路径 @RequestMapping("/hello") public Object hello(){ //返回的内容 return "hello,world,哈哈哈"; } public static void main(String[] args) { SpringApplication.run(Springboot01Application.class, args); } }
测试的时候执行main方法
也可以是使用命令 telnet localhost 端口号