Reading Content-Type application/json in Spring












1















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"}









share|improve this question




















  • 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















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"}









share|improve this question




















  • 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








1


1






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"}









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












1 Answer
1






active

oldest

votes


















0














From Spring REST guide




The Accept and Content-Type HTTP headers can be used to describe the content being sent or requested within an HTTP request. The client may set Accept to application/json if it is requesting a response in JSON. Conversely, when sending data, setting the Content-Type to application/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 and Accept 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 the consumes and produces 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





share|improve this answer


























  • 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











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
});


}
});














draft saved

draft discarded


















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









0














From Spring REST guide




The Accept and Content-Type HTTP headers can be used to describe the content being sent or requested within an HTTP request. The client may set Accept to application/json if it is requesting a response in JSON. Conversely, when sending data, setting the Content-Type to application/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 and Accept 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 the consumes and produces 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





share|improve this answer


























  • 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
















0














From Spring REST guide




The Accept and Content-Type HTTP headers can be used to describe the content being sent or requested within an HTTP request. The client may set Accept to application/json if it is requesting a response in JSON. Conversely, when sending data, setting the Content-Type to application/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 and Accept 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 the consumes and produces 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





share|improve this answer


























  • 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














0












0








0







From Spring REST guide




The Accept and Content-Type HTTP headers can be used to describe the content being sent or requested within an HTTP request. The client may set Accept to application/json if it is requesting a response in JSON. Conversely, when sending data, setting the Content-Type to application/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 and Accept 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 the consumes and produces 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





share|improve this answer















From Spring REST guide




The Accept and Content-Type HTTP headers can be used to describe the content being sent or requested within an HTTP request. The client may set Accept to application/json if it is requesting a response in JSON. Conversely, when sending data, setting the Content-Type to application/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 and Accept 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 the consumes and produces 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






share|improve this answer














share|improve this answer



share|improve this answer








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



















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Homophylophilia

Updating UILabel text programmatically using a function

Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage