지금, 역 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
목록이 요약 다음 : 학생 → 핸들러 → 시작 클래스의 → 엔티티의 하위 프로젝트
전면을 설명하는 두 부분이 있습니다, 그때의 일부 간소화 쓸 것입니다
: 구현 세부 사항
1. 모듈 프로젝트 resttemplate 만들기에서 패키지 com.southwind.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调用服务*/
}
RestHandler 생성 3.java 기록 가방 컨트롤러에서
코드를 작성 :
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();
}
}
시작 클래스를 켭
결과 :
더 쓰기 구성 클래스 응용 프로그램이 없기 때문에 기본 포트 8080 그래서, 당신이 포트는 8080입니다 볼 수 있습니다, 성공적으로 실행
5. 시험 :
A는 입사 HTTP :. // 로컬 호스트 : 8080 / REST /를 findall은
은 HTTP :. // 로컬 호스트로 B : 8080 / REST / findAll2
. 8080 / REST /를 findById 메소드 / 1 : HTTP :. // 로컬 호스트로 C
D. HTTP를 입력 : // localhost를 : 8080 / REST / findById2 / 1.
6. 입력 우체부
전체 검색 GET → HTTP :. // localhost를 : 8080 / 휴식 /를 findall은
B 전체 검색 GET → HTTP :. // localhost를 : 8080 / 나머지 / findAll2
C → 쿼리 HTTP GET :. // 로컬 호스트 : 8080 / REST /를 findById 메소드 / 3.
D → GET HTTP :. // localhost를 : 8080 / REST / findById2 / 2
. E 저장된 데이터
POST → HTTP : // localhost를 : 8080 / REST / 저장
F 검사.
GET → HTTP : // localhost를 : 8080 / REST / findAll2
. G 업데이트 데이터
PUT → HTTP : // localhost를 : 8080 / REST / 업데이트
H 검사.
GET → HTTP : // localhost를 : 8080 / 나머지 / findall은
내가 데이터를 삭제합니다.
→ 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