Learn from the station b springcloud, now summarize the summary removal of a small error appearing in the video, some of the error-prone places were reminded
b outbound links: https://www.bilibili.com/video/av55304977
data link:
https://pan.baidu.com/s/1o0Aju3IydKA15Vo1pP4z5w
extraction code: 21ru
On a link:
https://blog.csdn.net/qq_40893824/article/details/103324622
next section link:
https://blog.csdn.net/qq_40893824/article/details/103327911
The following list summarizes: → entity sub-project of Student → handler → startup class
There are two sections to explain the front, then I will write to streamline some of the
implementation details:
1. Create a module project resttemplate, java create packages com.southwind.entity under
the Student copy to the next entity
under 2.southwind create a startup class RestTemplateApplication
fill enter the code:
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调用服务*/
}
Under 3.java record bag controller, which create RestHandler
filling the code:
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. Turn on the startup class
results:
run successfully, you can see the port is 8080, because there is no write configuration class application, so the default is port 8080
5. Test:
A enters HTTP:. // localhost: 8080 / REST / the findAll
B into the HTTP:. // localhost: 8080 / REST / findAll2
C into the HTTP:. // localhost: 8080 / REST / the findById /. 1
D. enter HTTP: // localhost: 8080 / REST / findById2 /. 1
6. The entering Postman
A full search get → http:. // localhost: 8080 / rest / the findAll
B full search get → http:. // localhost: 8080 / rest / findAll2
. C → query HTTP GET: // localhost: 8080 / REST / the findById /. 3
D → GET HTTP:. // localhost: 8080 / REST / findById2 / 2
E stored data.
POST → HTTP: // localhost: 8080 / REST / Save
F inspection.
GET → HTTP: // localhost: 8080 / REST / findAll2
G updating data.
PUT → HTTP: // localhost: 8080 / REST / update
H check.
GET → HTTP: // localhost: 8080 / rest / findAll
i delete the data.
the Delete → HTTP: // localhost: 8080 / REST / deleteById / 4
check:
GET → HTTP: // localhost: 8080 / REST / findAll
check and correct!
On a link:
https://blog.csdn.net/qq_40893824/article/details/103324622
next section link:
https://blog.csdn.net/qq_40893824/article/details/103327911