Reputation: 734
I'm getting the following error and no resolution i found did the trick for me:
Unrecognized field "GaugeDeviceId" (Class GaugeDevice), not marked as ignorable
The problem seems, that the service returns the property names with a leading upper letter, while the class properties begin with a lower letter.
I tried:
@JsonProperty("SerialNo")
to the property instantiation - same error@JsonProperty("SerialNo")
to the corresponding getters - same error@JsonProperty("SerialNo")
to the corresponding setters - same error@JsonProperty("SerialNo")
to all of them (just for fun) - same error(note: @JsonProperty("SerialNo")
is just an example)
The strange thing is, that annotation: @JsonIgnoreProperties(ignoreUnknown = true)
should suppress exactly that error, but it is still triggering...
here the Class: (note: not complete)
@JsonIgnoreProperties(ignoreUnknown = true)
public class GaugeDevice
{
private int gaugeDeviceId;
private Date utcInstallation;
private String manufacturer;
private float valueOffset;
private String serialNo;
private String comment;
private int digitCount;
private int decimalPlaces;
@JsonProperty("SerialNo")
public String getSerialNo() {
return serialNo;
}
public void setSerialNo(String serialNo) {
this.serialNo = serialNo;
}
@JsonProperty("Comment")
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
Where is the way out here? Please help.
edit:
Here is the Client Class: (just a simple test client)
import ccc.android.meterdata.*;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import org.glassfish.jersey.jackson.JacksonFeature;
public class RestClient
{
private String connectionUrl;
private javax.ws.rs.client.Client client;
public RestClient(String baseUrl) {
client = ClientBuilder.newClient();;
connectionUrl = baseUrl;
client.register(JacksonFeature.class);
}
public GaugeDevice GetGaugeDevice(int id){
String uri = connectionUrl + "/GetGaugeDevice/" + id;
Invocation.Builder bldr = client.target(uri).request("application/json");
return bldr.get(GaugeDevice.class);
}
}
I hope the error has its root here?
Upvotes: 21
Views: 95818
Reputation: 7636
Make all your private variables and members to public. Jackson works on public members variables.
public int gaugeDeviceId;
public Date utcInstallation;
....
or add public Getters to private fields.
Upvotes: 2
Reputation: 1785
The solution that worked for me is the following
Add the import
import org.codehaus.jackson.map.DeserializationConfig;
Configure the ObjectMapper
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,false);
The complete solution
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String jsonInString = objectMapper.writeValueAsString(eje);
Eje newElement = objectMapper.readValue(jsonInString, Eje.class);
this.eje = newElement;
Upvotes: 1
Reputation: 157
I had the same issue and solved it by changing the annotation import from
com.fasterxml.jackson.annotation.JsonProperty
to
org.codehaus.jackson.annotate.JsonProperty
Upvotes: 11
Reputation: 8308
You can ignore the unknown properties while deserializing by using an annotation at class level.
For example :
@JsonIgnoreProperties(ignoreUnknown=true)
class Foo{
...
}
The above snippet will ignore any unknown properties. ( Annotation import : org.codehaus.jackson.annotate.JsonIgnoreProperties )
Upvotes: 6
Reputation: 934
I had the same issue and I resolved it by changing the annotation import from:
com.fasterxml.jackson.annotation.JsonIgnoreProperties
to
org.codehaus.jackson.annotate.JsonIgnoreProperties
Didn't have to define any NamingStrategy or ObjectMapper.
Upvotes: 18
Reputation: 116630
Another thing to check out is PropertyNamingStrategy
, which would allow Jackson to use "Pascal naming" and match JSON properties with POJO properties. See f.ex here: http://www.javacodegeeks.com/2013/04/how-to-use-propertynamingstrategy-in-jackson.html
Upvotes: 9
Reputation: 2766
Given the following is your error:
Unrecognized field "GaugeDeviceId" (Class GaugeDevice), not marked as ignorable
I'm pretty sure you need to do the same thing for the GaugeDeviceId
property as you've done for the SerialNo
property.
@JsonProperty("SerialNo")
public String getSerialNo() {
return this.serialNo;
}
@JsonProperty("GaugeDeviceId")
public int getGaugeDeviceId() {
return this.gaugeDeviceId;
}
Here I have a quick test class that is not throwing errors.
import org.codehaus.jackson.map.ObjectMapper;
public class JsonDeserialization {
public static void main(final String[] args) {
final String json = "{ \"SerialNo\":\"123\", \"GaugeDeviceId\":\"456\"}";
final ObjectMapper mapper = new ObjectMapper();
try {
final GaugeDevice readValue = mapper.readValue(json, GaugeDevice.class);
System.out.println(readValue.getSerialNo());
System.out.println(readValue.getGaugeDeviceId());
} catch (final Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
And it outputs:
123
456
EDIT: Version information
Not that it matters, as I believe the above is all using some pretty standard stuff from jackson, I'm using version 1.9.13 of the core-asl and the mapper-asl libraries
EDIT: Client Provided
I wonder if this is related to this issue? I believe the resolution is the configuration of dependencies that you're using.
I'm not sure, but I feel like I'm close with the following dependency setup (note I'm using maven)
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-processing</artifactId>
<version>2.0</version>
</dependency>
These links have provided the configuration information: Link 1, Link 2
Upvotes: 8