没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
第2节 RESTfull API 简单项目的快速搭建
2.1 新建 SpringBoot 项目
2.2spring-boot-starter-web 的引入,其依赖包的学习
2.3spring-boot-devtools 的引入,其依赖包的学习
2.4 源码
1)SpringBootRestfulApiApplication
@SpringBootApplication
public class SpringBootRestfulApiApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootRestfulApiApplication.class, args);
}
}
2)IndexController
@RestController
@RequestMapping(value="/index")
public class IndexController {
@RequestMapping
public String index(){
return "Hello World";
}
@RequestMapping(value="/get")
public Map<String, String> get(@RequestParam String name){
Map<String, String> map = new HashMap<>();
map.put("name", name);
map.put("value", "Hello World");
return map;
}
@RequestMapping(value = "find/{id}/{name}")
public User find(@PathVariable int id, @PathVariable String name){
User user = new User();
user.setId(id);
user.setName(name);
user.setDate(new Date());
return user;
}
}
3)User
public class User {
private int id;
private String name;
private Date date;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
2.5 运行项目
直接运行 main 方法或者使用 maven 命令: spring-boot:run
测试: http://localhost:8080/index
带参数:http://localhost:8080/index/get?name=Jack
带参数有中文:http://localhost:8080/index/get?name=杰克
url 测试:http://localhost:8080/index/find/1/Jack
url 测试:http://localhost:8080/index/find/1/杰克
2.6 打包
命令: clean package
剩余167页未读,继续阅读
ShenPlanck
- 粉丝: 55
- 资源: 343
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0