Untitled

 avatar
unknown
plain_text
2 months ago
4.1 kB
5
Indexable
// Response wrapper class
@JsonIgnoreProperties(ignoreUnknown = true)
public class ApiResponse {
    private String[][] data;
    private String message;

    // Default constructor needed for deserialization
    public ApiResponse() {}

    // Getters and setters
    public String[][] getData() {
        return data;
    }

    public void setData(String[][] data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public boolean isError() {
        return message != null;
    }
}

// Custom decoder to handle both response types
public class CustomResponseDecoder implements Decoder {
    private final ObjectMapper mapper = new ObjectMapper();

    @Override
    public Object decode(Response response, Type type) throws IOException {
        String responseBody = Util.toString(response.body().asReader(Charset.defaultCharset()));
        
        try {
            // First try to parse as 2D array
            String[][] data = mapper.readValue(responseBody, String[][].class);
            ApiResponse apiResponse = new ApiResponse();
            apiResponse.setData(data);
            return apiResponse;
        } catch (JsonProcessingException e) {
            // If that fails, try to parse as error message
            try {
                JsonNode node = mapper.readTree(responseBody);
                if (node.has("Message")) {
                    ApiResponse apiResponse = new ApiResponse();
                    apiResponse.setMessage(node.get("Message").asText());
                    return apiResponse;
                }
            } catch (JsonProcessingException ex) {
                throw new DecodeException(response.status(), "Failed to decode response", response.request());
            }
        }
        throw new DecodeException(response.status(), "Unexpected response format", response.request());
    }
}

// Feign client interface
@FeignClient(name = "myApi", url = "${api.base.url}", configuration = CustomFeignConfig.class)
public interface MyApiClient {
    @GetMapping("/your-endpoint")
    ApiResponse fetchData();
}

// Feign configuration class
@Configuration
public class CustomFeignConfig {
    @Bean
    public Decoder feignDecoder() {
        return new CustomResponseDecoder();
    }
}

// Example service using the Feign client
@Service
public class MyApiService {
    private final MyApiClient apiClient;

    public MyApiService(MyApiClient apiClient) {
        this.apiClient = apiClient;
    }

    public void processData() {
        try {
            ApiResponse response = apiClient.fetchData();
            
            if (!response.isError()) {
                String[][] data = response.getData();
                // Process your 2D array data
                for (String[] row : data) {
                    // Process each row
                }
            } else {
                // Handle error message
                System.err.println("Error: " + response.getMessage());
            }
        } catch (FeignException e) {
            // Handle Feign specific exceptions
            System.err.println("API call failed: " + e.getMessage());
        }
    }
}

// Example usage in controller
@RestController
@RequestMapping("/api")
public class MyController {
    private final MyApiService apiService;

    public MyController(MyApiService apiService) {
        this.apiService = apiService;
    }

    @GetMapping("/process")
    public ResponseEntity<?> processApiData() {
        try {
            ApiResponse response = apiService.processData();
            if (!response.isError()) {
                return ResponseEntity.ok(response.getData());
            } else {
                return ResponseEntity
                    .status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(response.getMessage());
            }
        } catch (Exception e) {
            return ResponseEntity
                .status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body("Failed to process data: " + e.getMessage());
        }
    }
}
Editor is loading...
Leave a Comment