Aprende de la estación b springcloud, ahora resumir la expulsión sumaria de un pequeño error que aparece en el video, algunos de los lugares propensos a errores se recordó
b enlaces salientes: https://www.bilibili.com/video/av55304977
de enlace de datos:
https://pan.baidu.com/s/1o0Aju3IydKA15Vo1pP4z5w
código de extracción: 21ru
En un enlace:
https://blog.csdn.net/qq_40893824/article/details/103324622
sección siguiente enlace:
https://blog.csdn.net/qq_40893824/article/details/103327911
A continuación se resumen de la lista: → entidad sub-proyecto del Estudiante → → gestor de inicio de clases
Hay dos secciones para explicar el frente, yo escribo para agilizar algunos de los
detalles de implementación:
1. Crear un proyecto de módulo resttemplate, java crear paquetes com.southwind.entity bajo
Student copia a la siguiente entidad
bajo 2.southwind crear un RestTemplateApplication clase de arranque
de relleno introducir el código:
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调用服务*/
}
Bajo 3.java controlador de bolsa de registro, que crean RestHandler
llenar el código:
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. Encienda la clase de arranque
resultados:
se ejecuta correctamente, se puede ver el puerto es 8080, porque no hay una aplicación de la clase de escritura de configuración, por lo que el valor predeterminado es el puerto 8080
5. Prueba:
A entra HTTP :. // localhost: 8080 / REST / la findAll
B en el HTTP :. // localhost: 8080 / REST / findAll2
C en el HTTP :. // localhost:. 8080 / REST / la findById / 1
D. introduzca http: // localhost: 8080 / REST / findById2 / 1.
6. el cartero entrar en
una búsqueda completa get → http :. // localhost: 8080 / descanso / la findAll
B búsqueda completa get → http :. // localhost: 8080 / descanso / findAll2
C → consulta HTTP GET:. // localhost: 8080 / REST / la findById / 3.
D → GET HTTP :. // localhost: 8080 / REST / findById2 / 2
. datos E almacenado
post → HTTP: // localhost: 8080 / REST / Guardar
F inspección.
GET → http: // localhost: 8080 / REST / findAll2
. actualización de datos G
PUT → http: // localhost: 8080 / REST / actualización de
comprobación H.
GET → http: // localhost: 8080 / descanso / findAll
i eliminar los datos.
la Borrar → http: // localhost: 8080 RESTO deleteById / 4 / /
cheque:
GET → http: // localhost: 8080 / REST / findAll
cheque y correcta!
En un enlace:
https://blog.csdn.net/qq_40893824/article/details/103324622
sección siguiente enlace:
https://blog.csdn.net/qq_40893824/article/details/103327911