Como filtrar campos de um retorno JSON

Boa Tarde Pessoal,

Tenho um JSON com muitos, mas muitos campos mesmo, sendo que necessito apenas de 2 campos dentre os demais, são os campos: customfield_10313 e customfield_16600.

Criei uma classe chamada IssueDates com esses respectivos campos, conforme vocês podem ver abaixo.

A requisição GET é executada com sucesso, com status 200 OK, mas quando vou imprimir os valores dos campos acima o resultado é null.

Fiz uma requisição GET, conforme abaixo:

String url = new StringBuilder().append("https://...").toString();

ResponseEntity<IssueDates> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, IssueDates.class);

System.out.println(response.getBody().getCustomfield_10313());
System.out.println(response.getBody().getCustomfield_16600());

O conteúdo da classe IssueDates é esse:

public class IssueDates {

    private String customfield_10313;
	
    private String customfield_16600;
	
	public String getCustomfield_10313() {
		return customfield_10313;
	}

	public void setCustomfield_10313(String customfield_10313) {
		this.customfield_10313 = customfield_10313;
	}

	public String getCustomfield_16600() {
		return customfield_16600;
	}

	public void setCustomfield_16600(String customfield_16600) {
		this.customfield_16600 = customfield_16600;
	}

}

Eu posso modelar uma classe apenas com os campos que desejo, sendo que essa requisição vai retornar muito mais campos?

Alguém poderia me ajudar?

Você não tem um construtor padrão com todos os atributos da classe?
Não me recordo com total certeza, mas acho que o Rest Template precisa que a classe de retorno tenha um construtor para montar o objeto de retorno corretamente.

criei esse construtor:

public IssueDates(String customfield_10313, String customfield_16600) {		
	this.customfield_10313 = customfield_10313;
	this.customfield_16600 = customfield_16600;
}

Mas obtive o seguinte erro:

Exception in thread “main” org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class br.com.oss.jira.quality.model.IssueDates]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of br.com.oss.jira.quality.model.IssueDates (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

Antes da implementação desse construtor a aplicação não dava erro, mas os campos desejados viam com valor nulo.

Tenta adicionar na classe a seguinte anotação:

@JsonIgnoreProperties(ignoreUnknown = true)

Dessa forma:

@JsonIgnoreProperties(ignoreUnknown = true)
public class IssueDates {

private String customfield_10313;

private String customfield_16600;

public String getCustomfield_10313() {
	return customfield_10313;
}

public void setCustomfield_10313(String customfield_10313) {
	this.customfield_10313 = customfield_10313;
}

public String getCustomfield_16600() {
	return customfield_16600;
}

public void setCustomfield_16600(String customfield_16600) {
	this.customfield_16600 = customfield_16600;
}

}

?

Exato, mas com o construtor na classe, ele precisa existir!

Não deu certo

O que exatamente não deu certo?

Sem compartilhar o erro obtido fica difícil tentar ajudar!

Sem o construtor a requisição GET funciona, mas os campos retornam nulo.

Qual o formato do JSON que essa rota responde?

Com o construtor ele não consegue realizar esta ação abaixo:

ResponseEntity<IssueDates> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, IssueDates.class);

Com o construtor ele dá falha na requisição GET.

{

"expand": "renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations",

"id": "8329475",

"self": "https://jira.indra.es/rest/api/2/issue/8329475",

"key": "BLOQVRENV-4917",

"fields": {

    "customfield_19200": null,

    "customfield_25207": null,

    "customfield_26418": null,

    "customfield_26417": null,

    "parent": {

        "id": "7976217",

        "key": "BLOQVRENV-7",

        "self": "https://jira.indra.es/rest/api/2/issue/7976217",

        "fields": {

            "summary": "Evolutivo OSS 2020 - Horas improdutivas (Ociosidade) [Ruptura]",

            "status": {

                "self": "https://jira.indra.es/rest/api/2/status/10035",

                "description": "",

                "iconUrl": "https://jira.indra.es/images/icons/statuses/inprogress.png",

                "name": "Working on it",

                "id": "10035",

                "statusCategory": {

                    "self": "https://jira.indra.es/rest/api/2/statuscategory/4",

                    "id": 4,

                    "key": "indeterminate",

                    "colorName": "yellow",

                    "name": "In Progress"

                }

            },

            "priority": {

                "self": "https://jira.indra.es/rest/api/2/priority/5",

                "iconUrl": "https://jira.indra.es/images/icons/priorities/trivial.svg",

                "name": "Trivial",

                "id": "5"

            },

            "issuetype": {

                "self": "https://jira.indra.es/rest/api/2/issuetype/110",

                "id": "110",

                "description": "",

                "iconUrl": "https://jira.indra.es/images/icons/issuetypes/IndoorTask.png",

                "name": "Indoor Task",

                "subtask": false

            }

        }

    },

    "customfield_26419": null,

    "customfield_17700": "",

    "resolution": null,

    "customfield_26300": null,

    "customfield_26421": null,

    "customfield_26420": null,

    "customfield_26423": null,

    "customfield_26422": null,

    "customfield_26425": null,

    "customfield_26424": null,

    "customfield_26427": null,

    "customfield_26426": null,

    "customfield_24800": null,

    "customfield_26429": null,

    "customfield_20300": null,

    "customfield_24801": null,

    "customfield_26428": null,

    "lastViewed": "2020-07-16T11:14:14.046+0200",

    "customfield_24802": null,

    "customfield_24803": null,

    "customfield_24804": null,

    "customfield_12360": null,

    "customfield_12363": null,

    "customfield_16600": "2020-08-02T08:57:00.000+0200",

    "customfield_12362": null,

    "customfield_12002": null,

    "customfield_12001": null,

    "customfield_12364": null,

    "customfield_12004": null,

    "customfield_12367": null,

    "customfield_12003": null,

    "customfield_14302": null,

    "labels": [],

    "customfield_12005": null,

    "customfield_12359": null,

    "aggregatetimeoriginalestimate": 3600,

    "customfield_26430": null,

    "customfield_25100": null,

    "customfield_26432": null,

    "customfield_26431": null,

    "customfield_25101": null,

    "issuelinks": [],

    "customfield_26434": "",

    "customfield_25102": null,

    "customfield_26433": "",

    "assignee": {

        "self": "https://jira.indra.es/rest/api/2/user?username=jboscod",

        "name": "jboscod",

        "key": "jboscod",

        "emailAddress": "jboscod@indracompany.com",

        "avatarUrls": {

            "48x48": "https://jira.indra.es/secure/useravatar?avatarId=10122",

            "24x24": "https://jira.indra.es/secure/useravatar?size=small&avatarId=10122",

            "16x16": "https://jira.indra.es/secure/useravatar?size=xsmall&avatarId=10122",

            "32x32": "https://jira.indra.es/secure/useravatar?size=medium&avatarId=10122"

        },

        "displayName": "Bosco Delfino Junior, Joao",

        "active": true,

        "timeZone": "Europe/Madrid"

    },

    "customfield_26436": null,

    "customfield_26435": null,

    "customfield_21742": null,

    "customfield_25900": null,

    "customfield_21741": null,

    "customfield_19300": null,

    "customfield_21740": null,

    "components": [

        {

            "self": "https://jira.indra.es/rest/api/2/component/120732",

            "id": "120732",

            "name": "OSS_BR",

            "description": "OSS_BR"

        }

    ],

    "customfield_15500": null,

    "customfield_13200": null,

    "customfield_12358": null,

    "customfield_12349": null,

    "customfield_12348": null,

    "customfield_21739": null,

    "customfield_21738": null,

    "customfield_26201": null,

    "customfield_21737": null,

    "customfield_26200": null,

    "customfield_21736": null,

    "customfield_26203": null,

    "customfield_21735": null,

    "customfield_26202": null,

    "customfield_20402": null,

    "customfield_21734": null,

    "customfield_20403": null,

    "customfield_21733": null,

    "customfield_26204": null,

    "customfield_20400": null,

    "customfield_21732": null,

    "customfield_20401": null,

    "customfield_21731": null,

    "customfield_21730": null,

    "customfield_18200": null,

    "customfield_18201": null,

    "subtasks": [],

    "reporter": {

        "self": "https://jira.indra.es/rest/api/2/user?username=jboscod",

        "name": "jboscod",

        "key": "jboscod",

        "emailAddress": "jboscod@indracompany.com",

        "avatarUrls": {

            "48x48": "https://jira.indra.es/secure/useravatar?avatarId=10122",

            "24x24": "https://jira.indra.es/secure/useravatar?size=small&avatarId=10122",

            "16x16": "https://jira.indra.es/secure/useravatar?size=xsmall&avatarId=10122",

            "32x32": "https://jira.indra.es/secure/useravatar?size=medium&avatarId=10122"

        },

        "displayName": "Bosco Delfino Junior, Joao",

        "active": true,

        "timeZone": "Europe/Madrid"

    },

    "customfield_12343": null,

    "customfield_12342": null,

    "customfield_12345": null,

    "customfield_12344": null,

    "customfield_12347": null,

    "customfield_12346": null,

    "customfield_11801": "2020-07-16T08:57:00.000+0200",

    "customfield_21729": null,

    "customfield_21728": null,

    "customfield_25000": null,

    "customfield_21727": null,

    "customfield_21726": null,

    "progress": {

        "progress": 0,

        "total": 3600,

        "percent": 0

    },

    "customfield_21725": null,

    "worklog": {

        "startAt": 0,

        "maxResults": 20,

        "total": 0,

        "worklogs": []

    },

    "customfield_21724": null,

    "customfield_21723": null,

    "customfield_21602": null,

    "customfield_21722": null,

    "customfield_21601": null,

    "customfield_21721": null,

    "customfield_21600": null,

    "customfield_21720": null,

    "issuetype": {

        "self": "https://jira.indra.es/rest/api/2/issuetype/34",

        "id": "34",

        "description": "ToDo",

        "iconUrl": "https://jira.indra.es/images/icons/issuetypes/ToDoSubTask.png",

        "name": "ToDo",

        "subtask": true

    },

    "customfield_17102": null,

    "customfield_17101": null,

    "customfield_17106": null,

    "customfield_17105": null,

    "project": {

        "self": "https://jira.indra.es/rest/api/2/project/71510",

        "id": "71510",

        "key": "BLOQVRENV",

        "name": "RENOVACIÓN BLOQUE V",

        "avatarUrls": {

            "48x48": "https://jira.indra.es/secure/projectavatar?avatarId=66003",

            "24x24": "https://jira.indra.es/secure/projectavatar?size=small&avatarId=66003",

            "16x16": "https://jira.indra.es/secure/projectavatar?size=xsmall&avatarId=66003",

            "32x32": "https://jira.indra.es/secure/projectavatar?size=medium&avatarId=66003"

        }

    },

    "customfield_17104": null,

    "customfield_17103": null,

    "customfield_12333": null,

    "customfield_17108": null,

    "customfield_12214": null,

    "customfield_12206": null,

    "customfield_12208": null,

    "customfield_12207": null,

    "customfield_21719": null,

    "customfield_21718": null,

    "resolutiondate": null,

    "customfield_21717": null,

    "customfield_21716": null,

    "customfield_26102": null,

    "customfield_21715": null,

    "customfield_21714": null,

    "customfield_21713": null,

    "customfield_21712": null,

    "customfield_21711": null,

    "customfield_26105": null,

    "customfield_21710": null,

    "customfield_26900": null,

    "customfield_26108": null,

    "customfield_26109": null,

    "watches": {

        "self": "https://jira.indra.es/rest/api/2/issue/BLOQVRENV-4917/watchers",

        "watchCount": 1,

        "isWatching": true

    },

    "customfield_18300": null,

    "customfield_22000": null,

    "customfield_14500": null,

    "customfield_21709": null,

    "customfield_21708": null,

    "customfield_21707": null,

    "customfield_21706": null,

    "customfield_26110": null,

    "customfield_21705": null,

    "customfield_21704": null,

    "customfield_21703": null,

    "customfield_26115": null,

    "customfield_21702": null,

    "customfield_21700": null,

    "customfield_26116": null,

    "updated": "2020-07-16T11:14:04.000+0200",

    "customfield_25700": null,

    "customfield_26119": null,

    "customfield_25701": null,

    "customfield_25702": null,

    "customfield_25703": null,

    "customfield_19500": null,

    "timeoriginalestimate": 3600,

    "customfield_19505": "",

    "description": "Teste do Jira Quality",

    "customfield_19501": "1|ih2ofr:",

    "customfield_13400": "9223372036854775807",

    "customfield_19502": "0|j3a9jz:",

    "timetracking": {

        "originalEstimate": "1h",

        "remainingEstimate": "1h",

        "originalEstimateSeconds": 3600,

        "remainingEstimateSeconds": 3600

    },

    "customfield_20600": null,

    "customfield_26800": null,

    "summary": "Teste do Jira Quality",

    "customfield_22101": null,

    "customfield_14601": null,

    "customfield_15802": {

        "self": "https://jira.indra.es/rest/api/2/customFieldOption/30304",

        "value": "No",

        "id": "30304"

    },

    "customfield_21808": null,

    "customfield_21807": null,

    "duedate": "2020-08-02",

    "customfield_21806": null,

    "customfield_21805": null,

    "customfield_21804": null,

    "customfield_21803": null,

    "comment": {

        "comments": [],

        "maxResults": 0,

        "total": 0,

        "startAt": 0

    },

    "customfield_21802": null,

    "customfield_25600": null,

    "customfield_23303": null,

    "customfield_15000": null,

    "fixVersions": [

        {

            "self": "https://jira.indra.es/rest/api/2/version/182363",

            "id": "182363",

            "description": "",

            "name": "BFR-BLOQVRENV-20TP04-AM20",

            "archived": false,

            "released": false,

            "releaseDate": "2023-03-31"

        }

    ],

    "customfield_14711": null,

    "customfield_14712": null,

    "customfield_13502": null,

    "customfield_14710": null,

    "customfield_14708": null,

    "customfield_14709": null,

    "customfield_14707": null,

    "customfield_20700": null,

    "customfield_22200": null,

    "priority": {

        "self": "https://jira.indra.es/rest/api/2/priority/4",

        "iconUrl": "https://jira.indra.es/images/icons/priorities/minor.svg",

        "name": "Minor",

        "id": "4"

    },

    "customfield_18500": null,

    "customfield_12401": "",

    "timeestimate": 3600,

    "versions": [],

    "customfield_21901": null,

    "customfield_25500": null,

    "customfield_25501": null,

    "customfield_25502": null,

    "customfield_25503": null,

    "status": {

        "self": "https://jira.indra.es/rest/api/2/status/10033",

        "description": "",

        "iconUrl": "https://jira.indra.es/images/icons/statuses/assigned.png",

        "name": "New",

        "id": "10033",

        "statusCategory": {

            "self": "https://jira.indra.es/rest/api/2/statuscategory/2",

            "id": 2,

            "key": "new",

            "colorName": "blue-gray",

            "name": "To Do"

        }

    },

    "customfield_25504": null,

    "customfield_25505": null,

    "customfield_21101": null,

    "customfield_21100": null,

    "customfield_19700": "2020-07-16T08:57:00.000+0200",

    "customfield_13600": null,

    "aggregatetimeestimate": 3600,

    "customfield_20800": null,

    "customfield_26601": null,

    "customfield_26600": null,

    "customfield_26603": null,

    "customfield_26602": null,

    "customfield_26726": null,

    "customfield_26604": null,

    "customfield_20000": null,

    "customfield_22300": null,

    "creator": {

        "self": "https://jira.indra.es/rest/api/2/user?username=jboscod",

        "name": "jboscod",

        "key": "jboscod",

        "emailAddress": "jboscod@indracompany.com",

        "avatarUrls": {

            "48x48": "https://jira.indra.es/secure/useravatar?avatarId=10122",

            "24x24": "https://jira.indra.es/secure/useravatar?size=small&avatarId=10122",

            "16x16": "https://jira.indra.es/secure/useravatar?size=xsmall&avatarId=10122",

            "32x32": "https://jira.indra.es/secure/useravatar?size=medium&avatarId=10122"

        },

        "displayName": "Bosco Delfino Junior, Joao",

        "active": true,

        "timeZone": "Europe/Madrid"

    },

    "aggregateprogress": {

        "progress": 0,

        "total": 3600,

        "percent": 0

    },

    "customfield_14800": null,

    "customfield_18600": null,

    "customfield_10313": "2020-08-02T08:57:00.000+0200",

    "customfield_10314": null,

    "customfield_25400": null,

    "customfield_21200": null,

    "customfield_19000": "",

    "timespent": null,

    "customfield_19001": null,

    "customfield_19802": {

        "self": "https://jira.indra.es/rest/api/2/customFieldOption/53004",

        "value": "Intermediate",

        "id": "53004"

    },

    "customfield_15203": null,

    "customfield_19924": null,

    "aggregatetimespent": null,

    "customfield_19800": null,

    "customfield_19921": null,

    "customfield_19922": null,

    "customfield_19801": "2020-07-31T08:57:00.000+0200",

    "customfield_14901": null,

    "workratio": 0,

    "customfield_20900": null,

    "customfield_20101": null,

    "customfield_22400": null,

    "created": "2020-07-16T08:58:24.000+0200",

    "customfield_18702": null,

    "customfield_14100": "",

    "customfield_12600": null,

    "customfield_25300": null,

    "customfield_21300": null,

    "customfield_19104": null,

    "customfield_19901": null,

    "customfield_19902": null,

    "customfield_19900": null,

    "attachment": [],

    "customfield_26401": null,

    "customfield_26403": null,

    "customfield_26402": null,

    "customfield_26405": null,

    "customfield_26404": null,

    "customfield_20200": "Indoor Task",

    "customfield_26407": null,

    "customfield_24900": null,

    "customfield_26406": null,

    "customfield_24901": null,

    "customfield_26409": null,

    "customfield_26408": null,

    "customfield_16500": null,

    "customfield_13900": null,

    "customfield_26410": null,

    "customfield_25200": null,

    "customfield_26412": null,

    "customfield_25201": null,

    "customfield_26411": null,

    "customfield_25202": null,

    "customfield_26414": null,

    "customfield_25203": null,

    "customfield_26413": null,

    "customfield_25204": null,

    "customfield_25205": null,

    "customfield_26416": null,

    "customfield_26415": null,

    "customfield_25206": null

}

}

Percebi agora que os campos desejados estão dentro do campo fields no JSON, então talvez seja isso.

Talvez ele não tenha encontrado os campos desejados porque no JSON, eles estão no campo fields, então talvez eu deveria criar esse campo fields na classe IssueDates.

Então eu teria que criar uma classe para receber o campo fields e outra classe para receber os campos que estão dentro do fields?

Sua classe não vai conseguir recuperar devido a hierarquia do JSON, sua estrutura tem que seguir o mesmo padrão do retorno.

Como ficaria a classe IssueDates para recuperar esse campo fields, já que esse campo tem um subnível do JSON?

Você precisa de mais uma classe, para recuperar fields!

public class FieldDto {

    private IssueDates fields;

    //getters e setters

}

Na chamada ao client tu passa a classe FieldDto como tipo de retorno esperado.

Beleza, então na requisição GET eu passo a classe FielDto, isso?

Sim!

@Jonathan_Medeiros, deu certo.

Tentei te avisar ontem, mas o fórum informou que eu havia chegado ao limite de respostas por ainda ser novato no fórum e que tinha que esperar 17 horas para responder novamente.

Muito obrigado pela grande ajuda.