Reading Content-Type application/json in Spring
Like in trying-to-use-spring-boot-rest-to-read-json-string-from-post I want to read a json payload from a POST request in a Spring RestController. Using the content type "text/plain" there is no problem, but with "application/json" the deserialization fails and I get a MessageNotReadable exception. But actually the content couldn't be simpler, it is just an empty json object "{}". Could it be that a required converter is missing?
I use Spring Root version 1.2.3.RELEASE.
Coding Example
@RequestMapping(value = "/deepdefinitions", method = POST, headers = "Accept=application/json")
@ResponseBody
public Definitions createOrUpdateDefinitions(HttpEntity<String> httpEntity) throws IOException { ... }
Curl Call
curl -H "Content-type: application/json" -X POST -d '{}' http://localhost:8080/deepdefinitions
Error
{"timestamp":1434397457853,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Could not read JSON: Can not deserialize instance of java.lang.String out of START_OBJECT tokenn at [Source: org.apache.catalina.connector.CoyoteInputStream@122f9ce3; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT tokenn at [Source: org.apache.catalina.connector.CoyoteInputStream@122f9ce3; line: 1, column: 1]","path":"/deepdefinitions"}
java json spring spring-mvc spring-boot
add a comment |
Like in trying-to-use-spring-boot-rest-to-read-json-string-from-post I want to read a json payload from a POST request in a Spring RestController. Using the content type "text/plain" there is no problem, but with "application/json" the deserialization fails and I get a MessageNotReadable exception. But actually the content couldn't be simpler, it is just an empty json object "{}". Could it be that a required converter is missing?
I use Spring Root version 1.2.3.RELEASE.
Coding Example
@RequestMapping(value = "/deepdefinitions", method = POST, headers = "Accept=application/json")
@ResponseBody
public Definitions createOrUpdateDefinitions(HttpEntity<String> httpEntity) throws IOException { ... }
Curl Call
curl -H "Content-type: application/json" -X POST -d '{}' http://localhost:8080/deepdefinitions
Error
{"timestamp":1434397457853,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Could not read JSON: Can not deserialize instance of java.lang.String out of START_OBJECT tokenn at [Source: org.apache.catalina.connector.CoyoteInputStream@122f9ce3; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT tokenn at [Source: org.apache.catalina.connector.CoyoteInputStream@122f9ce3; line: 1, column: 1]","path":"/deepdefinitions"}
java json spring spring-mvc spring-boot
1
You should use headers = "Accept=application/json" in '@RequestMapping' and pass the serialized object in '@RequestBody' to your controller method
– Darshan
Jun 15 '15 at 12:00
Show the exact exception/stack trace please.
– ci_
Jun 15 '15 at 13:03
@Darshan, the "headers" in RequestMapping does not change the behaviour. I update the question with a coding example.
– Gregor
Jun 15 '15 at 19:46
add a comment |
Like in trying-to-use-spring-boot-rest-to-read-json-string-from-post I want to read a json payload from a POST request in a Spring RestController. Using the content type "text/plain" there is no problem, but with "application/json" the deserialization fails and I get a MessageNotReadable exception. But actually the content couldn't be simpler, it is just an empty json object "{}". Could it be that a required converter is missing?
I use Spring Root version 1.2.3.RELEASE.
Coding Example
@RequestMapping(value = "/deepdefinitions", method = POST, headers = "Accept=application/json")
@ResponseBody
public Definitions createOrUpdateDefinitions(HttpEntity<String> httpEntity) throws IOException { ... }
Curl Call
curl -H "Content-type: application/json" -X POST -d '{}' http://localhost:8080/deepdefinitions
Error
{"timestamp":1434397457853,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Could not read JSON: Can not deserialize instance of java.lang.String out of START_OBJECT tokenn at [Source: org.apache.catalina.connector.CoyoteInputStream@122f9ce3; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT tokenn at [Source: org.apache.catalina.connector.CoyoteInputStream@122f9ce3; line: 1, column: 1]","path":"/deepdefinitions"}
java json spring spring-mvc spring-boot
Like in trying-to-use-spring-boot-rest-to-read-json-string-from-post I want to read a json payload from a POST request in a Spring RestController. Using the content type "text/plain" there is no problem, but with "application/json" the deserialization fails and I get a MessageNotReadable exception. But actually the content couldn't be simpler, it is just an empty json object "{}". Could it be that a required converter is missing?
I use Spring Root version 1.2.3.RELEASE.
Coding Example
@RequestMapping(value = "/deepdefinitions", method = POST, headers = "Accept=application/json")
@ResponseBody
public Definitions createOrUpdateDefinitions(HttpEntity<String> httpEntity) throws IOException { ... }
Curl Call
curl -H "Content-type: application/json" -X POST -d '{}' http://localhost:8080/deepdefinitions
Error
{"timestamp":1434397457853,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Could not read JSON: Can not deserialize instance of java.lang.String out of START_OBJECT tokenn at [Source: org.apache.catalina.connector.CoyoteInputStream@122f9ce3; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT tokenn at [Source: org.apache.catalina.connector.CoyoteInputStream@122f9ce3; line: 1, column: 1]","path":"/deepdefinitions"}
java json spring spring-mvc spring-boot
java json spring spring-mvc spring-boot
edited May 23 '17 at 12:10
Community♦
11
11
asked Jun 15 '15 at 11:38
GregorGregor
1,15921435
1,15921435
1
You should use headers = "Accept=application/json" in '@RequestMapping' and pass the serialized object in '@RequestBody' to your controller method
– Darshan
Jun 15 '15 at 12:00
Show the exact exception/stack trace please.
– ci_
Jun 15 '15 at 13:03
@Darshan, the "headers" in RequestMapping does not change the behaviour. I update the question with a coding example.
– Gregor
Jun 15 '15 at 19:46
add a comment |
1
You should use headers = "Accept=application/json" in '@RequestMapping' and pass the serialized object in '@RequestBody' to your controller method
– Darshan
Jun 15 '15 at 12:00
Show the exact exception/stack trace please.
– ci_
Jun 15 '15 at 13:03
@Darshan, the "headers" in RequestMapping does not change the behaviour. I update the question with a coding example.
– Gregor
Jun 15 '15 at 19:46
1
1
You should use headers = "Accept=application/json" in '@RequestMapping' and pass the serialized object in '@RequestBody' to your controller method
– Darshan
Jun 15 '15 at 12:00
You should use headers = "Accept=application/json" in '@RequestMapping' and pass the serialized object in '@RequestBody' to your controller method
– Darshan
Jun 15 '15 at 12:00
Show the exact exception/stack trace please.
– ci_
Jun 15 '15 at 13:03
Show the exact exception/stack trace please.
– ci_
Jun 15 '15 at 13:03
@Darshan, the "headers" in RequestMapping does not change the behaviour. I update the question with a coding example.
– Gregor
Jun 15 '15 at 19:46
@Darshan, the "headers" in RequestMapping does not change the behaviour. I update the question with a coding example.
– Gregor
Jun 15 '15 at 19:46
add a comment |
1 Answer
1
active
oldest
votes
From Spring REST guide
The
Accept
andContent-Type
HTTP headers can be used to describe the content being sent or requested within an HTTP request. The client may setAccept
toapplication/json
if it is requesting a response in JSON. Conversely, when sending data, setting theContent-Type
toapplication/xml
tells the client that the data being sent in the request is XML.
It appears your Controller is processing only Accept header:
@RequestMapping(value = "/deepdefinitions", method = POST, headers = "Accept=application/json")
You need to change it to:
@RequestMapping(value = "/deepdefinitions", method = POST, headers = "Accept=application/json,Content-type=application/json")
There are also Consumes and Produces annotations.
The documentation recommends:
Although you can match to
Content-Type
andAccept
header values using media type wild cards (for example "content-type=text/*" will match to "text/plain" and "text/html"), it is recommended to use theconsumes
andproduces
conditions respectively instead. They are intended specifically for that purpose.
Going by your related post, you should change your method signature to:
public @ResponseBody Definitions createOrUpdateDefinitions(@RequestBody String value, HttpEntity httpEntity) throws IOException
I think you should also change you curl command as below.This is because {}
(Javascript Object literal) would map to a object and to map to a String you should use a empty string ''
literal.
curl -H "Content-type: application/json" -X POST -d '' http://localhost:8080/deepdefinitions
I had used the consumes condition before. It was Darsham who recommended to use the accept header. But the consumes leads to the same problem.
– Gregor
Jun 16 '15 at 3:50
add a comment |
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',
autoActivateHeartbeat: false,
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%2f30844228%2freading-content-type-application-json-in-spring%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
From Spring REST guide
The
Accept
andContent-Type
HTTP headers can be used to describe the content being sent or requested within an HTTP request. The client may setAccept
toapplication/json
if it is requesting a response in JSON. Conversely, when sending data, setting theContent-Type
toapplication/xml
tells the client that the data being sent in the request is XML.
It appears your Controller is processing only Accept header:
@RequestMapping(value = "/deepdefinitions", method = POST, headers = "Accept=application/json")
You need to change it to:
@RequestMapping(value = "/deepdefinitions", method = POST, headers = "Accept=application/json,Content-type=application/json")
There are also Consumes and Produces annotations.
The documentation recommends:
Although you can match to
Content-Type
andAccept
header values using media type wild cards (for example "content-type=text/*" will match to "text/plain" and "text/html"), it is recommended to use theconsumes
andproduces
conditions respectively instead. They are intended specifically for that purpose.
Going by your related post, you should change your method signature to:
public @ResponseBody Definitions createOrUpdateDefinitions(@RequestBody String value, HttpEntity httpEntity) throws IOException
I think you should also change you curl command as below.This is because {}
(Javascript Object literal) would map to a object and to map to a String you should use a empty string ''
literal.
curl -H "Content-type: application/json" -X POST -d '' http://localhost:8080/deepdefinitions
I had used the consumes condition before. It was Darsham who recommended to use the accept header. But the consumes leads to the same problem.
– Gregor
Jun 16 '15 at 3:50
add a comment |
From Spring REST guide
The
Accept
andContent-Type
HTTP headers can be used to describe the content being sent or requested within an HTTP request. The client may setAccept
toapplication/json
if it is requesting a response in JSON. Conversely, when sending data, setting theContent-Type
toapplication/xml
tells the client that the data being sent in the request is XML.
It appears your Controller is processing only Accept header:
@RequestMapping(value = "/deepdefinitions", method = POST, headers = "Accept=application/json")
You need to change it to:
@RequestMapping(value = "/deepdefinitions", method = POST, headers = "Accept=application/json,Content-type=application/json")
There are also Consumes and Produces annotations.
The documentation recommends:
Although you can match to
Content-Type
andAccept
header values using media type wild cards (for example "content-type=text/*" will match to "text/plain" and "text/html"), it is recommended to use theconsumes
andproduces
conditions respectively instead. They are intended specifically for that purpose.
Going by your related post, you should change your method signature to:
public @ResponseBody Definitions createOrUpdateDefinitions(@RequestBody String value, HttpEntity httpEntity) throws IOException
I think you should also change you curl command as below.This is because {}
(Javascript Object literal) would map to a object and to map to a String you should use a empty string ''
literal.
curl -H "Content-type: application/json" -X POST -d '' http://localhost:8080/deepdefinitions
I had used the consumes condition before. It was Darsham who recommended to use the accept header. But the consumes leads to the same problem.
– Gregor
Jun 16 '15 at 3:50
add a comment |
From Spring REST guide
The
Accept
andContent-Type
HTTP headers can be used to describe the content being sent or requested within an HTTP request. The client may setAccept
toapplication/json
if it is requesting a response in JSON. Conversely, when sending data, setting theContent-Type
toapplication/xml
tells the client that the data being sent in the request is XML.
It appears your Controller is processing only Accept header:
@RequestMapping(value = "/deepdefinitions", method = POST, headers = "Accept=application/json")
You need to change it to:
@RequestMapping(value = "/deepdefinitions", method = POST, headers = "Accept=application/json,Content-type=application/json")
There are also Consumes and Produces annotations.
The documentation recommends:
Although you can match to
Content-Type
andAccept
header values using media type wild cards (for example "content-type=text/*" will match to "text/plain" and "text/html"), it is recommended to use theconsumes
andproduces
conditions respectively instead. They are intended specifically for that purpose.
Going by your related post, you should change your method signature to:
public @ResponseBody Definitions createOrUpdateDefinitions(@RequestBody String value, HttpEntity httpEntity) throws IOException
I think you should also change you curl command as below.This is because {}
(Javascript Object literal) would map to a object and to map to a String you should use a empty string ''
literal.
curl -H "Content-type: application/json" -X POST -d '' http://localhost:8080/deepdefinitions
From Spring REST guide
The
Accept
andContent-Type
HTTP headers can be used to describe the content being sent or requested within an HTTP request. The client may setAccept
toapplication/json
if it is requesting a response in JSON. Conversely, when sending data, setting theContent-Type
toapplication/xml
tells the client that the data being sent in the request is XML.
It appears your Controller is processing only Accept header:
@RequestMapping(value = "/deepdefinitions", method = POST, headers = "Accept=application/json")
You need to change it to:
@RequestMapping(value = "/deepdefinitions", method = POST, headers = "Accept=application/json,Content-type=application/json")
There are also Consumes and Produces annotations.
The documentation recommends:
Although you can match to
Content-Type
andAccept
header values using media type wild cards (for example "content-type=text/*" will match to "text/plain" and "text/html"), it is recommended to use theconsumes
andproduces
conditions respectively instead. They are intended specifically for that purpose.
Going by your related post, you should change your method signature to:
public @ResponseBody Definitions createOrUpdateDefinitions(@RequestBody String value, HttpEntity httpEntity) throws IOException
I think you should also change you curl command as below.This is because {}
(Javascript Object literal) would map to a object and to map to a String you should use a empty string ''
literal.
curl -H "Content-type: application/json" -X POST -d '' http://localhost:8080/deepdefinitions
edited Jun 16 '15 at 4:15
answered Jun 16 '15 at 1:04
randominstanceOfLivingThingrandominstanceOfLivingThing
5,57352852
5,57352852
I had used the consumes condition before. It was Darsham who recommended to use the accept header. But the consumes leads to the same problem.
– Gregor
Jun 16 '15 at 3:50
add a comment |
I had used the consumes condition before. It was Darsham who recommended to use the accept header. But the consumes leads to the same problem.
– Gregor
Jun 16 '15 at 3:50
I had used the consumes condition before. It was Darsham who recommended to use the accept header. But the consumes leads to the same problem.
– Gregor
Jun 16 '15 at 3:50
I had used the consumes condition before. It was Darsham who recommended to use the accept header. But the consumes leads to the same problem.
– Gregor
Jun 16 '15 at 3:50
add a comment |
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.
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%2f30844228%2freading-content-type-application-json-in-spring%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
1
You should use headers = "Accept=application/json" in '@RequestMapping' and pass the serialized object in '@RequestBody' to your controller method
– Darshan
Jun 15 '15 at 12:00
Show the exact exception/stack trace please.
– ci_
Jun 15 '15 at 13:03
@Darshan, the "headers" in RequestMapping does not change the behaviour. I update the question with a coding example.
– Gregor
Jun 15 '15 at 19:46