2016-04-29 4 views
2

Ich versuche, eine Karte von einer HTTP-Antwort zu POJO zu konvertieren. Die Antwort istConverting Map to POJO mit Jackson, Groovy beschwert sich für keine Eigenschaft

{_total=0} 

In meinem Pojo, ich möchte der Unterstrich, um loszuwerden, so kommentierte ich das Feld mit @JsonProperty. Aber Groovy beschwert sich für keine _total Eigenschaft

Stacktrace

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '{_total=0}' with class 'java.util.LinkedHashMap' to class 'io.toro.linkedin.response.CompanyAdminsResponse' due to: org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack: No such property: _total for class: io.toro.linkedin.response.CompanyAdminsResponse 
Possible solutions: total 

    at io.toro.linkedin.connector.LinkedInConnector.linkedInListCompaniesThatUserIsAdminOf(LinkedInConnector.groovy:314) 
    at LinkedInTest.list-companies-that-user-is-admin-of(LinkedInTest.groovy:115) 

Conversion Code

HttpResponseDecorator response = restClient.get(params) 
ObjectMapper mapper = new ObjectMapper() 
mapper.convertValue(response.data as Map, T) 

POJO

package io.toro.linkedin.response 

import com.fasterxml.jackson.annotation.JsonProperty 
import groovy.transform.ToString 
import io.toro.linkedin.model.Company 

/** 
* @author daniel.gomez 
*/ 
@ToString(includeNames = true) 
class CompanyAdminsResponse { 
    @JsonProperty('_count') 
    int count 
    @JsonProperty('_start') 
    int start 
    @JsonProperty('_total') 
    int total 
    List<Company> values 
} 
+0

es funktioniert Wenn Sie die toString Annotation loswerden? –

+0

nein, es funktioniert auch nicht. – danieljohngomez

Antwort

0

Ich denke, dass das Problem aus Ihrer Antwort Transformation kommt zu Map : response.data as Map was du passierst nach mapper.convertValue Methode.

Ich mache ein starkes Skript direkt die folgen mit Map[_total : 3] Ihr Problem zu testen, und es gibt keine ausgelöste Ausnahme ein das Objekt korrekt erstellt:

@Grab('com.fasterxml.jackson.core:jackson-core:2.7.3') 
@Grab('com.fasterxml.jackson.core:jackson-annotations:2.7.3') 
@Grab('com.fasterxml.jackson.core:jackson-databind:2.7.3') 
@GrabExclude('org.codehaus.groovy:groovy-all') 

import com.fasterxml.jackson.annotation.JsonProperty 
import com.fasterxml.jackson.databind.ObjectMapper 
import groovy.transform.ToString 

class Company { 
} 

@ToString(includeNames = true) 
class CompanyAdminsResponse { 
    @JsonProperty('_count') 
    int count 
    @JsonProperty('_start') 
    int start 
    @JsonProperty('_total') 
    int total 
    List<Company> values 
} 

ObjectMapper mapper = new ObjectMapper() 
def object = [_total : 3] 
println mapper.convertValue(object, CompanyAdminsResponse) 
// the above line prints CompanyAdminsResponse(count:0, start:0, total:3, values:null) 

hoffe, das hilft,

Verwandte Themen