1、SpringBoot环境搭建
----IDE : Eclipse 文章中版本Version: Oxygen.1a Release (4.7.1a)
----JDK : jdk1.8.0_102
----MAVEN: Eclipse已含插件不需要单独安装
1.1、Eclipse创建maven工程
File-->New-->Maven Project-->Next-->Next
Finish 创建完成
1.2、编辑pom.xml,更新maven依赖
<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>restful</groupId>
<artifactId>restful</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>restful</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- Spring Boot 父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<!-- Spring Boot web依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
2、简单接口实例
2.1、创建SpringBoot启动类
创建maven工程时 eclipse在com.example.rest包下 默认生成了App.java,如果没有手动创建(类名无所谓)。
修改App.java(增加注解SpringBootApplication,添加main方法)为SpringBoot启动类。
package com.example.rest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
*
* @author Administrator
*
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
2.2、 查询用户的简单接口实现
创建UserController.java、User.java 目录接口如下图
User.java
package com.example.rest.inf.model;
public class User {
private Long userId;
private String userName;
private String password;
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
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;
}
}
UserController.java
package com.example.rest.inf.controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.example.rest.inf.model.User;
@RestController
public class UserController {
@RequestMapping(value = "/user/{userId}", method = RequestMethod.GET, produces = { "application/json" })
public User qryUser(@PathVariable(required = true, value = "userId") Long userId) {
User user = new User();
user.setUserId(userId);
user.setUserName("user_"+userId.toString());
return user;
}
}
3、启动SpringBoot,简单测试
浏览器访问 http://127.0.0.1:8080/user/1001
输出:
{"userId":1001,"userName":"user_1001","password":null}