@JsonProperty (JSON) and @XMLElement (JAXB) both annotations not working on Pojo class
up vote
0
down vote
favorite
I am using Jackson for my POJO serialisation/deserialisation. The dependency that I am using is :
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.9.7</version>
</dependency>
I have a POJO which looks something like this :
@JsonIgnoreProperties(ignoreUnknown = true)
class MyPOJO{
private MyPOJO(){
}
@JsonProperty("name")
String name;
@JsonProperty("id")
String id;
........
.........
// getters only (no setters)
}
I annotate my getters with @JsonProperty("...") again.
When I write a test code I am able to deserialise my POJO all fine. But when I sent a request through Postman client, I get the following error :
Unrecognized field "name" (class MyPOJO), not marked
as ignorable (0 known properties: ])
at [Source: (org.apache.cxf.transport.http.AbstractHTTPDestination$1);
line: 10, column: 14] (through reference chain: SomePOJO1["somename1"]-
>java.util.ArrayList[0]->SomePOJO2["somename2"]->java.util.ArrayList[0]-
>MyPOJO["name"])
Here is my test code which works all fine
final String json = "some json string here";
final ObjectMapper mapper = new ObjectMapper();
try {
final MyPOJO pojo= mapper.readValue(json, MyPOJO.class);
System.out.println(pojo.getId());
System.out.println(pojo.getName());
} catch (final Exception e) {
e.printStackTrace();
}
Output : "123"
"abc"
There is no codehaus jackson library in the classpath and I am importing the right classes from com.fasterxml.jackson
. MyPOJO is being used by some other POJO from a different module as a member field (as seen in the error too).
MyPOJO has a no-arg private constructor and there are no setters on the fields.
What am I missing here??
Thanks!
EDIT
@XmlElement annotations (javax.xml.bind.annotation.XmlElement
) are also not working on this class along with @JsonProperty annotations. XML also showing same error :
@JsonIgnoreProperties(ignoreUnknown = true)
@XmlType(name = "MyPOJOType", propOrder = {"id", "name"})
class MyPOJO{
private MyPOJO(){
}
@XmlElement(name = "name", required = true)
@JsonProperty("name")
String name;
@XmlElement(name = "id", required = true)
@JsonProperty("id")
String id;
........
.........
// getters only (no setters)
}
On giving XML as input :
Output : JAXBException occurred : cvc-complex-type.2.4.a: Invalid
content was found starting with element 'name'
java json serialization jackson
add a comment |
up vote
0
down vote
favorite
I am using Jackson for my POJO serialisation/deserialisation. The dependency that I am using is :
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.9.7</version>
</dependency>
I have a POJO which looks something like this :
@JsonIgnoreProperties(ignoreUnknown = true)
class MyPOJO{
private MyPOJO(){
}
@JsonProperty("name")
String name;
@JsonProperty("id")
String id;
........
.........
// getters only (no setters)
}
I annotate my getters with @JsonProperty("...") again.
When I write a test code I am able to deserialise my POJO all fine. But when I sent a request through Postman client, I get the following error :
Unrecognized field "name" (class MyPOJO), not marked
as ignorable (0 known properties: ])
at [Source: (org.apache.cxf.transport.http.AbstractHTTPDestination$1);
line: 10, column: 14] (through reference chain: SomePOJO1["somename1"]-
>java.util.ArrayList[0]->SomePOJO2["somename2"]->java.util.ArrayList[0]-
>MyPOJO["name"])
Here is my test code which works all fine
final String json = "some json string here";
final ObjectMapper mapper = new ObjectMapper();
try {
final MyPOJO pojo= mapper.readValue(json, MyPOJO.class);
System.out.println(pojo.getId());
System.out.println(pojo.getName());
} catch (final Exception e) {
e.printStackTrace();
}
Output : "123"
"abc"
There is no codehaus jackson library in the classpath and I am importing the right classes from com.fasterxml.jackson
. MyPOJO is being used by some other POJO from a different module as a member field (as seen in the error too).
MyPOJO has a no-arg private constructor and there are no setters on the fields.
What am I missing here??
Thanks!
EDIT
@XmlElement annotations (javax.xml.bind.annotation.XmlElement
) are also not working on this class along with @JsonProperty annotations. XML also showing same error :
@JsonIgnoreProperties(ignoreUnknown = true)
@XmlType(name = "MyPOJOType", propOrder = {"id", "name"})
class MyPOJO{
private MyPOJO(){
}
@XmlElement(name = "name", required = true)
@JsonProperty("name")
String name;
@XmlElement(name = "id", required = true)
@JsonProperty("id")
String id;
........
.........
// getters only (no setters)
}
On giving XML as input :
Output : JAXBException occurred : cvc-complex-type.2.4.a: Invalid
content was found starting with element 'name'
java json serialization jackson
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am using Jackson for my POJO serialisation/deserialisation. The dependency that I am using is :
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.9.7</version>
</dependency>
I have a POJO which looks something like this :
@JsonIgnoreProperties(ignoreUnknown = true)
class MyPOJO{
private MyPOJO(){
}
@JsonProperty("name")
String name;
@JsonProperty("id")
String id;
........
.........
// getters only (no setters)
}
I annotate my getters with @JsonProperty("...") again.
When I write a test code I am able to deserialise my POJO all fine. But when I sent a request through Postman client, I get the following error :
Unrecognized field "name" (class MyPOJO), not marked
as ignorable (0 known properties: ])
at [Source: (org.apache.cxf.transport.http.AbstractHTTPDestination$1);
line: 10, column: 14] (through reference chain: SomePOJO1["somename1"]-
>java.util.ArrayList[0]->SomePOJO2["somename2"]->java.util.ArrayList[0]-
>MyPOJO["name"])
Here is my test code which works all fine
final String json = "some json string here";
final ObjectMapper mapper = new ObjectMapper();
try {
final MyPOJO pojo= mapper.readValue(json, MyPOJO.class);
System.out.println(pojo.getId());
System.out.println(pojo.getName());
} catch (final Exception e) {
e.printStackTrace();
}
Output : "123"
"abc"
There is no codehaus jackson library in the classpath and I am importing the right classes from com.fasterxml.jackson
. MyPOJO is being used by some other POJO from a different module as a member field (as seen in the error too).
MyPOJO has a no-arg private constructor and there are no setters on the fields.
What am I missing here??
Thanks!
EDIT
@XmlElement annotations (javax.xml.bind.annotation.XmlElement
) are also not working on this class along with @JsonProperty annotations. XML also showing same error :
@JsonIgnoreProperties(ignoreUnknown = true)
@XmlType(name = "MyPOJOType", propOrder = {"id", "name"})
class MyPOJO{
private MyPOJO(){
}
@XmlElement(name = "name", required = true)
@JsonProperty("name")
String name;
@XmlElement(name = "id", required = true)
@JsonProperty("id")
String id;
........
.........
// getters only (no setters)
}
On giving XML as input :
Output : JAXBException occurred : cvc-complex-type.2.4.a: Invalid
content was found starting with element 'name'
java json serialization jackson
I am using Jackson for my POJO serialisation/deserialisation. The dependency that I am using is :
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.9.7</version>
</dependency>
I have a POJO which looks something like this :
@JsonIgnoreProperties(ignoreUnknown = true)
class MyPOJO{
private MyPOJO(){
}
@JsonProperty("name")
String name;
@JsonProperty("id")
String id;
........
.........
// getters only (no setters)
}
I annotate my getters with @JsonProperty("...") again.
When I write a test code I am able to deserialise my POJO all fine. But when I sent a request through Postman client, I get the following error :
Unrecognized field "name" (class MyPOJO), not marked
as ignorable (0 known properties: ])
at [Source: (org.apache.cxf.transport.http.AbstractHTTPDestination$1);
line: 10, column: 14] (through reference chain: SomePOJO1["somename1"]-
>java.util.ArrayList[0]->SomePOJO2["somename2"]->java.util.ArrayList[0]-
>MyPOJO["name"])
Here is my test code which works all fine
final String json = "some json string here";
final ObjectMapper mapper = new ObjectMapper();
try {
final MyPOJO pojo= mapper.readValue(json, MyPOJO.class);
System.out.println(pojo.getId());
System.out.println(pojo.getName());
} catch (final Exception e) {
e.printStackTrace();
}
Output : "123"
"abc"
There is no codehaus jackson library in the classpath and I am importing the right classes from com.fasterxml.jackson
. MyPOJO is being used by some other POJO from a different module as a member field (as seen in the error too).
MyPOJO has a no-arg private constructor and there are no setters on the fields.
What am I missing here??
Thanks!
EDIT
@XmlElement annotations (javax.xml.bind.annotation.XmlElement
) are also not working on this class along with @JsonProperty annotations. XML also showing same error :
@JsonIgnoreProperties(ignoreUnknown = true)
@XmlType(name = "MyPOJOType", propOrder = {"id", "name"})
class MyPOJO{
private MyPOJO(){
}
@XmlElement(name = "name", required = true)
@JsonProperty("name")
String name;
@XmlElement(name = "id", required = true)
@JsonProperty("id")
String id;
........
.........
// getters only (no setters)
}
On giving XML as input :
Output : JAXBException occurred : cvc-complex-type.2.4.a: Invalid
content was found starting with element 'name'
java json serialization jackson
java json serialization jackson
edited Nov 21 at 8:50
asked Nov 20 at 13:57
raghav
10013
10013
add a comment |
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394629%2fjsonproperty-json-and-xmlelement-jaxb-both-annotations-not-working-on-pojo%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394629%2fjsonproperty-json-and-xmlelement-jaxb-both-annotations-not-working-on-pojo%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown