Java – Jackson @ jsonproperty is invalid if the property name is not equal to the field name
•
Java
I have the following JSON
{ "kNown-name": "Zevs","approximate-age": 320 }
And binding classes
public class GodBinding { @JsonProperty("kNown-name") public String name; @JsonProperty("approximate-age") public int age; // constructors // getters & setters }
And followng Maven dependence 2.23 2 2.5. four
<dependencies> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> <version>${jersey.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.jaxrs</groupId> <artifactId>jackson-jaxrs-json-provider</artifactId> <version>${jackson.version}</version> </dependency> </dependencies>
If I publish such a JSON, then I have a null unexpected result
GodBinding [name=null,age=0]
If I use @ jsonproperty without a name and send JSON, where the property name is equal to the field name
{ "name": "Zevs","age": 320 }
Then it works normally
GodBinding [name=Zevs,age=320]
If anyone knows, how does @ jsonproperty ("name") work on fields?
Solution
You should add it to your POM
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.4.4</version> </dependency>
You can see an example here
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码