Обновить src/main/java/org/lanit/controllers/JSONController.java

This commit is contained in:
Шимченко 2025-09-12 06:31:15 +00:00
parent aff58ed540
commit f05ea61703

View File

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