Обновить src/main/java/org/lanit/controllers/JSONController.java
This commit is contained in:
parent
aff58ed540
commit
f05ea61703
@ -1,4 +1,5 @@
|
||||
package org.lanit.controllers;
|
||||
|
||||
import org.lanit.models.*;
|
||||
import org.lanit.service.JsonProcessingService;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
@ -11,7 +12,7 @@ import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
@Controller
|
||||
public class JSONController {
|
||||
|
||||
@ -23,23 +24,19 @@ public class JSONController {
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
|
||||
// ЕДИНЫЙ метод для всех JSON ответов
|
||||
private ResponseEntity<Object> createJsonResponse(Object body, HttpStatus status) {
|
||||
return ResponseEntity.status(status)
|
||||
.header("Content-Type", "application/json")
|
||||
.header("Content-Type", "application/json") // Без charset!
|
||||
.body(body);
|
||||
}
|
||||
|
||||
|
||||
@PostMapping(value = "/json", produces = "application/json", consumes = MediaType.APPLICATION_JSON_VALUE)
|
||||
@PostMapping(value = "/json", produces = "application/json", consumes = "application/json")
|
||||
@ResponseBody
|
||||
public ResponseEntity<?> handleJsonRequest(
|
||||
@RequestParam("action") String action,
|
||||
@RequestBody String jsonBody) throws IOException {
|
||||
|
||||
System.out.println("Received JSON: " + jsonBody);
|
||||
System.out.println("Action: " + action);
|
||||
|
||||
try {
|
||||
if ("add".equals(action)) {
|
||||
AddRequest addRequest = objectMapper.readValue(jsonBody, AddRequest.class);
|
||||
@ -59,13 +56,13 @@ public class JSONController {
|
||||
response.setInfo(modifiedInfo);
|
||||
response.setUuid(deleteRequest.getUuid());
|
||||
response.setLastUpdate(Instant.now().toString());
|
||||
return createJsonResponse(response, HttpStatus.OK);
|
||||
return createJsonResponse(response, HttpStatus.OK); // Используем createJsonResponse
|
||||
|
||||
} else {
|
||||
ErrorResponse error = new ErrorResponse();
|
||||
error.setStatus("error");
|
||||
error.setMessage("Передан некорректный action - " + action);
|
||||
return createJsonResponse(error, HttpStatus.OK);
|
||||
return createJsonResponse(error, HttpStatus.BAD_REQUEST); // Используем createJsonResponse
|
||||
}
|
||||
|
||||
} catch (RuntimeException e) {
|
||||
@ -74,12 +71,13 @@ public class JSONController {
|
||||
error.setMessage(e.getMessage());
|
||||
HttpStatus status = e.getMessage().contains("тикер") || e.getMessage().contains("индекс")
|
||||
? HttpStatus.NOT_FOUND : HttpStatus.BAD_REQUEST;
|
||||
return ResponseEntity.status(status).body(error);
|
||||
return createJsonResponse(error, status); // Используем createJsonResponse
|
||||
|
||||
} catch (IOException e) {
|
||||
ErrorResponse error = new ErrorResponse();
|
||||
error.setStatus("error");
|
||||
error.setMessage("Ошибка разбора JSON");
|
||||
return createJsonResponse(error, HttpStatus.OK);
|
||||
return createJsonResponse(error, HttpStatus.BAD_REQUEST); // Исправил на BAD_REQUEST
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user