当具有中间 json "data" 属性时,如何将 api 响应的 JSON 对象关联到 java 类?

How can I associate JSON object of the api response to java classes when having the intermediate json "data" attribute?

感谢您点击此处。 我有一个 JSON REST API(由 Directus CMS 提供)。所有 API 响应都包含一个 json 对象,该对象的“数据”属性包含我想要的内容。

{
    "data": {
        "id": 1,
        "status": "published",
        "sort": null,
        "user_created": "5a91c184-908d-465e-a7d5-4b648029bbe0",
        "date_created": "2022-04-26T09:43:37.000Z",
        "user_updated": "5a91c184-908d-465e-a7d5-4b648029bbe0",
        "date_updated": "2022-05-30T14:23:50.000Z",
        "Titre": "Réseaux Sociaux",
        "Description": "Retrouvez les dernières news en direct sur nos réseaux sociaux!",
        "Lien": "https://www.instagram.com/univlorraine/",
        "ImageArrierePlan": "f23ffd53-7244-4439-a8cf-41bd0fd3aa72",
        "Erreur_Bloc": null
    }
}

此数据属性可以是对象或对象列表,具体取决于请求。

我有一个 Java Spring 应用程序,其服务使用 API。我正在使用带有交换方法的 RestTemplate。

public Object callAPI(String url, HttpMethod httpMethod, Object body, MultiValueMap<String, String> headers, Class<?> classe) {
    final RestTemplate rt = new RestTemplate();
    try {
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        rt.setRequestFactory(requestFactory);
        final HttpEntity<?> request = new HttpEntity<>(body, headers);
        final ResponseEntity<?> response = rt.exchange(url, httpMethod, request, classe);
        if (response.getStatusCode().equals(HttpStatus.OK)) {
            return response.getBody();
        }
        else return response.getStatusCode();
    } catch (final Exception e) {
        System.out.println(e);
        return null;
    }
}

在交换方法中,我将现有的 class 传递给直接 link 响应数据,并提供 class。 问题是我有这个数据属性,它阻止我 linking 数据。

有人能解决这个问题吗?

----更新----

感谢 AlbiKai 的回复,我创建了一个通用的 Wrapper class :

public class Wrapper<T> {
    private T data;

    public void set(T data) {
        this.data = data;
    }

    public T get() {
        return data;
    }
}

然后我尝试将这个 Wrapper 放入交换中:

    public <classe> Object callAPI(String url, HttpMethod httpMethod, Object body, MultiValueMap<String, String> headers, Class<?> classe) {
        final RestTemplate rt = new RestTemplate();
        try {
            HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
            rt.setRequestFactory(requestFactory);
            final HttpEntity<?> request = new HttpEntity<>(body, headers);
            final ResponseEntity<?> response = rt.exchange(url, httpMethod, request, Wrapper<classe>.class);

但是我在 Wrapper 上收到错误“Cannot select from parameterized type”:/

您可以创建一个与 json 响应相匹配的包装器 class :一个对象只有一个名为“数据”的属性,类型为 desire final class(或列表)和在交换方法中使用它。

public class wrapper {
    
    YourClass data;
}

我放弃了 Wrapper 等...... 我只是传递一个字符串 class 并在我的控制器中使用它来删除这个“数据”属性 并将字符串映射到 class.

服务:

    public String callAPI(String url, HttpMethod httpMethod, Object body, MultiValueMap<String, String> headers) {
        final RestTemplate rt = new RestTemplate();
        try {
            HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
            rt.setRequestFactory(requestFactory);
            final HttpEntity<?> request = new HttpEntity<>(body, headers);
            final ResponseEntity<String> response = rt.exchange(url, httpMethod, request, String.class);
            if (response.getStatusCode().equals(HttpStatus.OK)) {
                return response.getBody();
            }
            else return response.getStatusCode().toString();
        } catch (final Exception e) {
            System.out.println(e);
            return null;
        }
    }

一个控制器:

    public List<BlocInformation> getBlocInformation() {
        String url = "http://localhost:8055/items/bloc_information/?fields=*,Erreur_Bloc.*";
        final RestAPIService blocService = new RestAPIService();
        String response = blocService.callAPI(url, HttpMethod.GET, null, null);
        if (response != null) {
            String result = response.substring(8, response.length() - 1);
            ObjectMapper mapper = new ObjectMapper();
            List<BlocInformation> blocInformationList = null;
            try {
                blocInformationList = Arrays.asList(mapper.readValue(result, BlocInformation[].class));
            } catch (IOException e) {
                e.printStackTrace();
            }
            return blocInformationList;
        }
        return null;
    }