从b站学习springcloud,现在进行总结,该总结除去了视频中出现的小错误,对有些易错的地方进行了提醒
b站链接:https://www.bilibili.com/video/av55304977
资料链接:
https://pan.baidu.com/s/1o0Aju3IydKA15Vo1pP4z5w
提取码: 21ru
上一节链接:
https://blog.csdn.net/qq_40893824/article/details/103324622
下一节链接:
https://blog.csdn.net/qq_40893824/article/details/103327911
下面的内容总结:子工程→entity的Student→handler→启动类
有个前面两节的讲解,以后我写的会精简一些了
实现细节:
1.创建module工程resttemplate,java下创包com.southwind.entity
将Student复制至entity下
2.southwind下创建启动类RestTemplateApplication
填入代码:
package com.southwind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class RestTemplateApplication {
public static void main(String[] args) {
SpringApplication.run(RestTemplateApplication.class,args);
}
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
/*14-17行对应controller/ReatHandler中RestTemplate调用服务*/
}
3.java下创包controller,里面创建RestHandler
填土代码:
package com.southwind.controller;
import com.southwind.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.Collection;
@RestController
@RequestMapping("/rest")
public class RestHandler {
@Autowired
private RestTemplate restTemplate;
/*增 改*/
@PostMapping("/save")
public void save(@RequestBody Student student){
restTemplate.postForEntity("http://localhost:8010/student/save",student,null);
}
@PutMapping("/update")
public void update(@RequestBody Student student){
restTemplate.put("http://localhost:8010/student/update",student);
}
/*删*/
@DeleteMapping("/deleteById/{id}")
public void deleteById(@PathVariable("id")long id){
restTemplate.delete("http://localhost:8010/student/deleteById/{id}",id);
}
/*查*/
@GetMapping("/findAll")
public Collection<Student> findAll(){
return restTemplate.getForObject("http://localhost:8010/student/findAll",Collection.class);
}
@GetMapping("/findAll2")
public Collection<Student> findAll2(){
return restTemplate.getForEntity("http://localhost:8010/student/findAll",Collection.class).getBody();
}
/*查*/
@GetMapping("/findById/{id}")
public Student findById(@PathVariable("id")long id){
return restTemplate.getForObject("http://localhost:8010/student/findById/{id}",Student.class,id);
}
@GetMapping("/findById2/{id}")
public Student findById2(@PathVariable("id")long id){
return restTemplate.getForEntity("http://localhost:8010/student/findById/{id}",Student.class,id).getBody();
}
}
4.开启 启动类
结果:
运行成功,可以看到端口是8080,因为没有写配置类application,所以端口默认是8080
5.测试:
a.进入 http://localhost:8080/rest/findAll
b.进入 http://localhost:8080/rest/findAll2
c. 进入 http://localhost:8080/rest/findById/1
d.进入 http://localhost:8080/rest/findById2/1
6.进入Postman
a.全查 get→ http://localhost:8080/rest/findAll
b.全查 get→ http://localhost:8080/rest/findAll2
c.查询 get→ http://localhost:8080/rest/findById/3
d. get → http://localhost:8080/rest/findById2/2
e.存数据
post→ http://localhost:8080/rest/save
f.检查
get→ http://localhost:8080/rest/findAll2
g. 更新数据
put→ http://localhost:8080/rest/update
h.检查
get→ http://localhost:8080/rest/findAll
i.删除数据
delete→ http://localhost:8080/rest/deleteById/4
检查:
get→ http://localhost:8080/rest/findAll
检查无误!
上一节链接:
https://blog.csdn.net/qq_40893824/article/details/103324622
下一节链接:
https://blog.csdn.net/qq_40893824/article/details/103327911