|
|
|
@ -1,22 +1,33 @@
@@ -1,22 +1,33 @@
|
|
|
|
|
package ru.molokoin.clientserviceteachers.controllers; |
|
|
|
|
|
|
|
|
|
import java.net.URI; |
|
|
|
|
import java.time.Duration; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.Date; |
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
import org.springframework.http.HttpEntity; |
|
|
|
|
import org.springframework.http.HttpHeaders; |
|
|
|
|
import org.springframework.http.HttpMethod; |
|
|
|
|
import org.springframework.http.HttpStatusCode; |
|
|
|
|
import org.springframework.http.MediaType; |
|
|
|
|
import org.springframework.http.ResponseEntity; |
|
|
|
|
import org.springframework.stereotype.Controller; |
|
|
|
|
import org.springframework.ui.Model; |
|
|
|
|
import org.springframework.validation.annotation.Validated; |
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping; |
|
|
|
|
import org.springframework.web.bind.annotation.ModelAttribute; |
|
|
|
|
import org.springframework.web.bind.annotation.RequestBody; |
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping; |
|
|
|
|
import org.springframework.web.bind.annotation.ResponseBody; |
|
|
|
|
import org.springframework.web.bind.annotation.RestController; |
|
|
|
|
import org.springframework.web.client.RestTemplate; |
|
|
|
|
import org.springframework.web.reactive.function.BodyInserters; |
|
|
|
|
import org.springframework.web.reactive.function.client.ClientRequest; |
|
|
|
|
import org.springframework.web.reactive.function.client.ClientResponse; |
|
|
|
|
import org.springframework.web.reactive.function.client.WebClient; |
|
|
|
|
import org.springframework.web.reactive.function.client.WebClientResponseException; |
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException; |
|
|
|
|
import com.fasterxml.jackson.core.type.TypeReference; |
|
|
|
@ -24,11 +35,19 @@ import com.fasterxml.jackson.databind.JsonMappingException;
@@ -24,11 +35,19 @@ import com.fasterxml.jackson.databind.JsonMappingException;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
|
|
|
|
|
|
|
|
import jakarta.servlet.http.HttpServletRequest; |
|
|
|
|
import reactor.core.publisher.Mono; |
|
|
|
|
import ru.molokoin.clientserviceteachers.entities.Teacher; |
|
|
|
|
import ru.molokoin.clientserviceteachers.services.TeacherService; |
|
|
|
|
|
|
|
|
|
import org.springframework.web.bind.annotation.PostMapping; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Controller |
|
|
|
|
@RequestMapping(path = "/") |
|
|
|
|
public class TeachersController { |
|
|
|
|
@Autowired |
|
|
|
|
WebClient client; |
|
|
|
|
|
|
|
|
|
@GetMapping("/teachers/resend") |
|
|
|
|
public String infoResend(){ |
|
|
|
|
RestTemplate restTemplate = new RestTemplate(); |
|
|
|
@ -57,4 +76,73 @@ public class TeachersController {
@@ -57,4 +76,73 @@ public class TeachersController {
|
|
|
|
|
model.addAttribute("teachers", teachers); |
|
|
|
|
return "teachers"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* сохранение записи о новом преподавателе: |
|
|
|
|
* - подготовка сущности записи |
|
|
|
|
* - преобразование записи в json |
|
|
|
|
* - отправка post запроса на сервис-ресурсов |
|
|
|
|
* |
|
|
|
|
* @param entity |
|
|
|
|
* @return |
|
|
|
|
* @throws JsonProcessingException |
|
|
|
|
*/ |
|
|
|
|
// @PostMapping(path = "/teacher/create",
|
|
|
|
|
// consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
|
|
|
|
|
// public String postMethodName(@RequestBody Teacher teacher) throws JsonProcessingException {
|
|
|
|
|
// String url = "http://resource-service-api:8181/teacher/create";
|
|
|
|
|
// // model.addAttribute("teacher", teacher);
|
|
|
|
|
// RestTemplate restTemplate = new RestTemplate();
|
|
|
|
|
// // HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
// // headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
|
// ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
// String json = objectMapper.writeValueAsString(teacher);
|
|
|
|
|
// System.out.println("СООБЩЕНИЕ ИЗ СЕРВИСА-КЛИЕНТОВ:");
|
|
|
|
|
// System.out.println(json);
|
|
|
|
|
// // ResponseEntity<String> response =
|
|
|
|
|
// // restTemplate.postForEntity(url, teacher, Teacher.class);
|
|
|
|
|
// restTemplate.postForEntity(url, json, String.class);
|
|
|
|
|
// return "teachers";
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// @PostMapping(path = "/teacher/add", consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
|
|
|
|
|
@PostMapping( |
|
|
|
|
path = "/teacher/add", |
|
|
|
|
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, |
|
|
|
|
produces = { |
|
|
|
|
MediaType.APPLICATION_JSON_VALUE |
|
|
|
|
}) |
|
|
|
|
public String postTeacher(@ModelAttribute("teacher") @Validated Teacher teacher){ |
|
|
|
|
client.post() |
|
|
|
|
.uri("/teacher/create") |
|
|
|
|
.body(Mono.just(teacher), Teacher.class) |
|
|
|
|
// .bodyValue(BodyInserters.fromValue(teacher))
|
|
|
|
|
.retrieve() |
|
|
|
|
.toBodilessEntity() |
|
|
|
|
.subscribe( |
|
|
|
|
responseEntity -> { |
|
|
|
|
// Handle success response here
|
|
|
|
|
HttpStatusCode status = responseEntity.getStatusCode(); |
|
|
|
|
URI location = responseEntity.getHeaders().getLocation(); |
|
|
|
|
// handle response as necessary
|
|
|
|
|
}, |
|
|
|
|
error -> { |
|
|
|
|
// Handle the error here
|
|
|
|
|
if (error instanceof WebClientResponseException) { |
|
|
|
|
WebClientResponseException ex = (WebClientResponseException) error; |
|
|
|
|
HttpStatusCode status = ex.getStatusCode(); |
|
|
|
|
System.out.println("!!!Error Status Code: " + status.value()); |
|
|
|
|
//...
|
|
|
|
|
} else { |
|
|
|
|
// Handle other types of errors
|
|
|
|
|
System.err.println("!!!An unexpected error occurred: " + error.getMessage()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
); |
|
|
|
|
// .bodyToMono(String.class)
|
|
|
|
|
// .timeout(Duration.ofSeconds(3)) // timeout
|
|
|
|
|
// .block();
|
|
|
|
|
return "redirect:/teachers"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|