Что-то начало работать
This commit is contained in:
@ -1,12 +1,19 @@
|
||||
package ru.cft.task.restClient;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.stage.Stage;
|
||||
import org.springframework.boot.autoconfigure.security.SecurityProperties;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
import org.springframework.web.client.HttpStatusCodeException;
|
||||
import org.springframework.web.client.RestClientException;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
@ -55,44 +62,61 @@ public class RestActionsController {
|
||||
}
|
||||
|
||||
public void createAction() {
|
||||
EmailRecord emailRecord = new EmailRecord();
|
||||
emailRecord.setName("Test");
|
||||
emailRecord.setEmail("aaa");
|
||||
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
HttpEntity<EmailRecord> request = new HttpEntity<>(emailRecord);
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
||||
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
|
||||
map.add("name", name.getText());
|
||||
map.add("email", email.getText());
|
||||
|
||||
HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<MultiValueMap<String, String>>(map, headers);
|
||||
try {
|
||||
ResponseEntity<EmailRecord> response = restTemplate.exchange(getRestServerUrl(), HttpMethod.POST, request, EmailRecord.class);
|
||||
System.out.println(response.getStatusCode());
|
||||
ResponseEntity<EmailRecord> response = restTemplate.exchange(getRestServerUrl(), HttpMethod.POST, entity, EmailRecord.class);
|
||||
if (response.getStatusCode() == HttpStatus.OK) {
|
||||
EmailRecord er = response.getBody();
|
||||
id.setText(String.valueOf(er.getId()));
|
||||
EmailRecord emailRecord = response.getBody();
|
||||
id.setText(String.valueOf(emailRecord.getId()));
|
||||
}
|
||||
} catch (HttpStatusCodeException ex) {
|
||||
try {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
ErrorResponse err = objectMapper.readValue(ex.getResponseBodyAsString(), ErrorResponse.class);
|
||||
Utils.showAlert("error", err.getMessage());
|
||||
} catch (IOException e) {
|
||||
Utils.showAlert("error", e.getMessage());
|
||||
}
|
||||
} catch (RestClientException ex) {
|
||||
Utils.showAlert("error", ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void readAction() {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
headers.set("id", id.getText());
|
||||
headers.set("name", name.getText());
|
||||
headers.set("email", email.getText());
|
||||
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
HttpEntity<EmailRecord> entity = new HttpEntity<>(headers);
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(getRestServerUrl());
|
||||
if (!id.getText().isEmpty()) {
|
||||
builder.queryParam("id", id.getText());
|
||||
} else if (!email.getText().isEmpty()) {
|
||||
builder.queryParam("email", email.getText());
|
||||
} else {
|
||||
builder.queryParam("name", name.getText());
|
||||
}
|
||||
|
||||
HttpEntity<?> entity = new HttpEntity<>(headers);
|
||||
try {
|
||||
ResponseEntity<EmailRecord> result = restTemplate.exchange(getRestServerUrl(), HttpMethod.GET, entity, EmailRecord.class);
|
||||
if (result.getStatusCode() == HttpStatus.OK) {
|
||||
EmailRecord emailRecord = result.getBody();
|
||||
ResponseEntity<EmailRecord> response = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.GET, entity, EmailRecord.class);
|
||||
if (response.getStatusCode() == HttpStatus.OK) {
|
||||
EmailRecord emailRecord = response.getBody();
|
||||
id.setText(String.valueOf(emailRecord.getId()));
|
||||
name.setText(emailRecord.getName());
|
||||
email.setText(emailRecord.getEmail());
|
||||
}
|
||||
} catch (RestClientException ex) {
|
||||
Utils.showAlert("error", ex.getMessage());
|
||||
} catch (HttpStatusCodeException ex) {
|
||||
try {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
ErrorResponse err = objectMapper.readValue(ex.getResponseBodyAsString(), ErrorResponse.class);
|
||||
Utils.showAlert("error", err.getMessage());
|
||||
} catch (IOException e) {
|
||||
Utils.showAlert("error", e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,12 +0,0 @@
|
||||
package ru.cft.task.restClient;
|
||||
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.web.client.DefaultResponseErrorHandler;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class RestErrorHandler extends DefaultResponseErrorHandler {
|
||||
public void handleError(ClientHttpResponse response) throws IOException {
|
||||
Utils.showAlert("error", response.getStatusText());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user