Refactor PrestashopProxyController to enhance proxy response handling and ensure consistent JSON output

This commit is contained in:
Vincent Guillet
2025-11-29 10:05:21 +01:00
parent 4c16e356a3
commit 44764a5f14

View File

@@ -3,9 +3,12 @@ package fr.gameovergne.api.controller;
import fr.gameovergne.api.service.PrestashopClient;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.HandlerMapping;
@RestController
@@ -20,7 +23,7 @@ public class PrestashopProxyController {
@GetMapping("/**")
public ResponseEntity<String> proxyGet(HttpServletRequest request) {
// Ex: fullPath = /api/ps/categories
String fullPath = (String) request.getAttribute(
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
String bestMatchPattern = (String) request.getAttribute(
@@ -31,8 +34,15 @@ public class PrestashopProxyController {
String path = relativePath.isEmpty() ? "/" : "/" + relativePath;
String rawQuery = request.getQueryString(); // déjà encodée
// Query string brute, déjà encodée (display=%5Bid,name,active%5D&output_format=JSON)
String rawQuery = request.getQueryString();
return prestashopClient.getWithRawQuery(path, rawQuery);
var prestaResponse = prestashopClient.getWithRawQuery(path, rawQuery);
// On renvoie EXACTEMENT le même status + body, en forçant JSON
return ResponseEntity
.status(prestaResponse.getStatusCode())
.contentType(MediaType.APPLICATION_JSON)
.body(prestaResponse.getBody());
}
}