添加相关依赖
<?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>wonder</groupId> <artifactId>skyRainbow</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <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> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
创建程序启动入口文件
package lf; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication // SpingBoot 相关注解,等于@Configuration、@EnableAutoConfiguration、@ComponentScan三个注解一起的作用 public class SkyRainbowApplication { public static void main(String[] args) { /** * Spring boot 程序入口 */ SpringApplication.run(SkyRainbowApplication.class,args); } }
创建相关类文件
package lf.entity; public class SRUser { private String username; private String password; private String email; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
package lf.controller; import lf.entity.SRUser; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller // 控制器注解 @RequestMapping("/lf/user") public class UserController { /** * 获取用户信息 */ @ResponseBody // 返回json数据的注解 @RequestMapping(value = "/info",method = RequestMethod.GET) public SRUser getUserInfo(){ SRUser user = new SRUser(); user.setUsername("甘雨路"); user.setPassword("123456"); user.setEmail("1498222322@qq.com"); return user; } }
打开启动文件,并运行
在浏览器上输入http://localhost:8080/lf/user/info 即可
其中要注意启动文件要放在外层,否则无法自动扫描所有文件: