You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
@RequestMapping Content-Type error when a @RequestBody is used but Content-Type header is not given #3313
We are trying to upgrade to 1.3.0.M1 from 1.2.1.
When using the 1.2.1 version, we did not have to set the Content-Type: application/json header when sending a request body to a method web service endpoint like:
The Content-Type would be With version 1.3.0.M1, the call will fail when passing a @RequestBody if the Content-Type header is not set, with the error:
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/octet-stream' not supported at
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:216) at
org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:148) at
org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:126) at
org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77) at
org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162) at
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:129) at
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:111) at
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:799) at
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:728) at
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) at
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
... 67 more
Could you give more details about this issue please? Especially, HTTP headers for request and response. In any case, what you're describing here looks normal - application/octet-stream is a sensible default for "Content-Type" if none is sent by the client. What HTTP client are you using?
I've just reproduced this exact behavior in Spring Boot 1.2.1, with the following:
I totally agree that application/octet-stream is an acceptable default if the Content-Type header is not set. That makes total sense. I was just thrown off as to why simply changing the version in my gradle.build from 1.2.1 to 1.3.0M1 altered the behavior I was seeing.
Digging a little deeper, I found that the issue is specifically only when making a POST request where I don't actually set anything on the request body. It was a little clouded by testing in PostMan and not curl. In PostMan, when I don't have the Content-Type set, use a POST, and don't add data to the request, it's successful in 1.2.1. It fails in 1.3.0M1. I can get it to work in Curl, but I have to remove the --data flag all together to match the behavior. If i have an empty --data '' in curl, the error is given.
So narrowing in on the findings: 1.3.0M1 fails when you don't add data to a POST request, but 1.2.1 does not.
I'm OK with the requirement to set the Content-Type, that's sensible. However, I think it worthwhile to note that the behavior is slightly different in 1.3.0M1 compared to earlier versions for this one use case.
Given that the method declares a required @RequestBody if the body is empty, it makes sense to have the exception. Can you describe exactly what was the before and after behavior? If possible provide a snippet of code how to reproduce the behavior.
Even if you don't manually set a Content-Type, Postman will set one for you anyway.
Here is an example (look at the bottom of the screen capture, Content-Type: text/plain.
Too bad Postman only shows headers received, and not the actual headers sent.
Now I did try to reproduce your use case.
Spring Boot 1.2.1, same Controller, POST request with empty body sent by Postman:
In that case, it's complaining because the request body is missing and our controller is expecting one.
Same thing with Spring Boot 1.3.0.M1, :
{
"timestamp": 1435441392535,
"status": 415,
"error": "Unsupported Media Type",
"exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
"message": "Content type 'text/plain;charset=UTF-8' not supported",
"path": "/test"
}
It's saying our controller does not support this Content-Type, which is true.
So there is a change of behavior here, probably coming from Spring Framework itself.
I suspect this comes from RequestResponseBodyMethodProcessor but I have yet to confirm that.
In any case, I couldn't reproduce the "working" state.
I created these in a separate sample project to remove any inadvertent configuration or code that would affect the outcome. If you are interested, here is my build.gradle file (using 3.0.0M1):
Using the sample project and only cURL instead of PostMan, you ca see the results and URL's used at the bottom of the post.
Summary
The results are pretty clear, there is an obvious change in behavior, which I think has degraded.
Version 1.2.1
GET requests don't accept a response body, so the Content-Type is never required.
POST requests - ONLY if data is set on the request body is the Content-Type needed.
Version 3.0.0M1
GET requests where the controller method has an optional @RequestBody requires the Content-Type to be set, whether or not the data is set on the request body. Note that in the example Controller method, the method supports both GET and POST requests, with the @RequestBody's required attribute set false.
POST requests - The Content-Type is always required whether the request body is set or not.
I believe the way version 1.2.1 functions is the correct behavior. Because the @RequestBody can be an optional parameter, the Content-Type should not be required if the request body is not set, regardless of the request type.
Just for fyi... we have the filter endpoint to support both GET and POST requests because we allow the filter to be called without criteria as a GET to retrieve all results possible.
upload completely sent off: 24 out of 24 bytes
< HTTP/1.1 415 Unsupported Media Type
< Server: Apache-Coyote/1.1
< X-Application-Context: application:8080
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Mon, 29 Jun 2015 20:13:27 GMT
<
{"timestamp":"2015-06-29T20:13:27.388+0000","status":415,"error":"Unsupported Media Type","exception
":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content type 'application/
x-www-form-urlencoded;charset=UTF-8' not supported","path":"/TestApp/sample/filter"}* Connection #0
to host localhost left intact
upload completely sent off: 24 out of 24 bytes
< HTTP/1.1 415 Unsupported Media Type
< Server: Apache-Coyote/1.1
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Mon, 29 Jun 2015 20:25:35 GMT
<
{"timestamp":"2015-06-29T20:25:35.601+0000","status":415,"error":"Unsupported Media Type","exception
":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content type 'application/
x-www-form-urlencoded;charset=UTF-8' not supported","path":"/TestApp/sample/filter"}* Connection #0
to host localhost left intact
Apparently, it is the fact that you are using a simple String as the filter parameter type and not a complex Object. There is a code snippet above of the data object I used. When I use your method, it works, mine doesn't:
Thanks a lot @lewisdavidcole for all your work.
This is indeed a subtle issue to reproduce.
We've tracked it down to the right class and we've created an issue: SPR-13176. The fix will be probably included in the next Boot milestone.
In the meantime, just an opinion: using required=false to use a controller handler for GET and POST requests is a bit of a workaround. Something like this makes it easier to understand your application, appears as separate handlers in the list of mapped handlers with the Boot actuator feature, and is less expensive since args processing does not happen anymore for GET requests:
Hello, for me the problem persists for Spring boot 1.4.1 and Spring MVC 4.3.3.
I am trying to consume AWS SNS messages (which are interestingly sent with Content-Type: text/plain; charset=UTF-8 headers). My controller is like the following:
@PostMapping(value = Routes.NOTIFICATION_HANDLER_ROUTE)
public String notificationHandler(@RequestBody(required = false) SNSMessage snsMessage) {
// Process SNSMessage object
return "test";
}
When I send request with Content-Type: application/json header manually and with JSON String, SNSMessage is serialized successfully by Jackson. When I change the header to text/plain, I get org.springframework.web.HttpMediaTypeNotSupportedException exception.
From HTTP standarts point of view it is true, the response should be 415 because I send JSON instead of plain text but as I am not able to change the Content-Type header of AWS, so there should be an option to disable the Content-Type header check.
@cagataygurturk That's something that would have to be addressed in Spring MVC rather than in Spring Boot. Without any changes in Spring MVC, one thing that you could do is to register an HTTP message converter that treats text/plain as JSON.
It's not a Content-Type header check but rather a failure to find a suitable HttpMessageConverter. The StringHttpMessageConverter can deal with "text/plain" but it only converts to String. The JacksonHttpMessageConverter can convert to SNSMessage but it only gets involved for application/json. You could configure the JacksonHttpMessageConverter to also convert text/plain but that could cause you issues in other endpoints. I do not see what we could do in Spring MVC to make this better. We are in a worse position to make assumptions.
Probably the simplest reliable thing to do is to insert the request body as a String and invoke the Jackson converter manually. This is just a weird situation to begin with. Another option might be to use a custom ContentNegotiationStrategy that resolves the content type for very specific URLs. Then you can keep your method signature as it is.
@wilkinsona For me it is also an Spring MVC issue but as I saw the problem is being discussed here, I decided to post here.
@rstoyanchev You are right. A generic HttpMessageConverter could cause issues in other controllers because I want to do only one exception for one single controller. Well, for now I am deserialising it manually.
Very simply, it was an oversight on our part. It should have been 'application/json' all along but we missed it. Now we have to find a way to change it to what it should be without breaking anyone's integration with SNS.
@ztgoto This issue has been closed for a while. If you think you've found a new bug please open a fresh issue and provide a sample application that shows it.
@user20161119 This issue has been closed for a while. If you think you've found a new bug please open a fresh issue and provide a sample application that shows it.
Activity
bclozel commentedon Jun 24, 2015
Hi @lewisdavidcole
Could you give more details about this issue please? Especially, HTTP headers for request and response. In any case, what you're describing here looks normal -
application/octet-streamis a sensible default for"Content-Type"if none is sent by the client. What HTTP client are you using?I've just reproduced this exact behavior in Spring Boot 1.2.1, with the following:
Maybe do you have a custom
ContentNegotiationStrategythat sets thedefaultContentType? I don't think Boot is doing anything like this.lewisdavidcole commentedon Jun 26, 2015
Hi Phil, thanks for responding.
I totally agree that application/octet-stream is an acceptable default if the Content-Type header is not set. That makes total sense. I was just thrown off as to why simply changing the version in my gradle.build from 1.2.1 to 1.3.0M1 altered the behavior I was seeing.
Digging a little deeper, I found that the issue is specifically only when making a POST request where I don't actually set anything on the request body. It was a little clouded by testing in PostMan and not curl. In PostMan, when I don't have the Content-Type set, use a POST, and don't add data to the request, it's successful in 1.2.1. It fails in 1.3.0M1. I can get it to work in Curl, but I have to remove the --data flag all together to match the behavior. If i have an empty --data '' in curl, the error is given.
So narrowing in on the findings: 1.3.0M1 fails when you don't add data to a POST request, but 1.2.1 does not.
I'm OK with the requirement to set the Content-Type, that's sensible. However, I think it worthwhile to note that the behavior is slightly different in 1.3.0M1 compared to earlier versions for this one use case.
rstoyanchev commentedon Jun 27, 2015
We did make some changes in this area spring-projects/spring-framework@36ed4df#diff-1b0f5771c87599a9c49da7e88ddb788b related to the introduction of RequestBodyAdvice as well as https://jira.spring.io/browse/SPR-12778.
Given that the method declares a required @RequestBody if the body is empty, it makes sense to have the exception. Can you describe exactly what was the before and after behavior? If possible provide a snippet of code how to reproduce the behavior.
bclozel commentedon Jun 27, 2015
Even if you don't manually set a
Content-Type, Postman will set one for you anyway.Here is an example (look at the bottom of the screen capture,
Content-Type: text/plain.Too bad Postman only shows headers received, and not the actual headers sent.
Now I did try to reproduce your use case.
Spring Boot 1.2.1, same Controller, POST request with empty body sent by Postman:
{ "timestamp": 1435441055428, "status": 400, "error": "Bad Request", "exception": "org.springframework.http.converter.HttpMessageNotReadableException", "message": "Required request body content is missing: org.springframework.web.method.HandlerMethod$HandlerMethodParameter@ca37b95", "path": "/test" }In that case, it's complaining because the request body is missing and our controller is expecting one.
Same thing with Spring Boot 1.3.0.M1, :
{ "timestamp": 1435441392535, "status": 415, "error": "Unsupported Media Type", "exception": "org.springframework.web.HttpMediaTypeNotSupportedException", "message": "Content type 'text/plain;charset=UTF-8' not supported", "path": "/test" }It's saying our controller does not support this
Content-Type, which is true.So there is a change of behavior here, probably coming from Spring Framework itself.
I suspect this comes from
RequestResponseBodyMethodProcessorbut I have yet to confirm that.In any case, I couldn't reproduce the "working" state.
lewisdavidcole commentedon Jun 29, 2015
Thanks for looking deeper into the behavior. I think there is one aspect missing: The input to the controller method is marked as optional:
There are 2 important parts to the method signature:
There is nothing special about the SampleData object, but note that all my code is in Groovy:
I created these in a separate sample project to remove any inadvertent configuration or code that would affect the outcome. If you are interested, here is my build.gradle file (using 3.0.0M1):
Using the sample project and only cURL instead of PostMan, you ca see the results and URL's used at the bottom of the post.
Summary
The results are pretty clear, there is an obvious change in behavior, which I think has degraded.
Version 1.2.1
GET requests don't accept a response body, so the Content-Type is never required.
POST requests - ONLY if data is set on the request body is the Content-Type needed.
Version 3.0.0M1
GET requests where the controller method has an optional @RequestBody requires the Content-Type to be set, whether or not the data is set on the request body. Note that in the example Controller method, the method supports both GET and POST requests, with the @RequestBody's required attribute set false.
POST requests - The Content-Type is always required whether the request body is set or not.
I believe the way version 1.2.1 functions is the correct behavior. Because the @RequestBody can be an optional parameter, the Content-Type should not be required if the request body is not set, regardless of the request type.
Just for fyi... we have the filter endpoint to support both GET and POST requests because we allow the filter to be called without criteria as a GET to retrieve all results possible.
cURL Results
Spring Boot 1.3.0.M1
GET request with Content-Type set
curl -X GET -H "Content-Type: application/json" http://localhost:8080/TestApp/sample/filter
GET request with no Content-Type set
curl -v -X GET http://localhost:8080/TestApp/sample/filter
POST with no Content-Type and no request body
curl -v -X POST http://localhost:8080/TestApp/sample/filter
POST with no Content-Type and a request body
curl --data "{"sampleString1":"test"}" -v -X POST "http://localhost:8080/TestApp/sample/filter
POST with Content-Type set and no request body
curl -v -X POST -H "Content-Type: application/json" http://localhost:8080/TestApp/sample/filter
POST with Content-Type set and a request body
curl --data "{"sampleString1":"test"}" -v -X POST -H "Content-Type: application/json" "http://localhost:8080/TestApp/sample/filter"
Spring Boot 1.2.1
GET request with Content-Type set
curl -v -X GET -H "Content-Type: application/json" http://localhost:8080/TestApp/sample/filter
GET request with no Content-Type set
curl -v -X GET http://localhost:8080/TestApp/sample/filter
POST with no Content-Type and no request body
curl -v -X POST http://localhost:8080/TestApp/sample/filter
POST with no Content-Type and a request body
curl --data "{"sampleString1":"test"}" -v -X POST "http://localhost:8080/TestApp/sample/filter"
POST with Content-Type set and no request body
curl -v -X POST -H "Content-Type: application/json" http://localhost:8080/TestApp/sample/filter
POST with Content-Type set and a request body
curl --data "{"sampleString1":"test"}" -v -X POST -H "Content-Type: application/json" "http://localhost:8080/TestApp/sample/filter"
rstoyanchev commentedon Jun 29, 2015
Hm, I'm unable to reproduce this behavior. I used start.spring.io, check web. Then modified DemoApplication:
Modified the pom.xml to use Boot 1.3.0.M1 + added milestone repo. What am I missing?
lewisdavidcole commentedon Jun 29, 2015
Apparently, it is the fact that you are using a simple String as the filter parameter type and not a complex Object. There is a code snippet above of the data object I used. When I use your method, it works, mine doesn't:
curl -v -X POST http://localhost:8080/TestApp/sample-controller/filter
curl -v -X POST http://localhost:8080/TestApp/sample-controller/filter-string
rstoyanchev commentedon Jun 30, 2015
Okay, thanks I can reproduce it now.
bclozel commentedon Jun 30, 2015
Thanks a lot @lewisdavidcole for all your work.
This is indeed a subtle issue to reproduce.
We've tracked it down to the right class and we've created an issue: SPR-13176. The fix will be probably included in the next Boot milestone.
In the meantime, just an opinion: using
required=falseto use a controller handler for GET and POST requests is a bit of a workaround. Something like this makes it easier to understand your application, appears as separate handlers in the list of mapped handlers with the Boot actuator feature, and is less expensive since args processing does not happen anymore for GET requests:Fix @RequestBody argument processing for null Content-Type
lewisdavidcole commentedon Jun 30, 2015
@bclozel I totally agree with your comment of having a single method supporting two request types.. accidental complexity (or intentional :) ).
Thanks for working through the issue to reproduce and identify a solution.
Spring Boot rocks and has a great dev team!
Hope to see you at Uber Conf in Denver this July!
sbrannen commentedon Jul 2, 2015
I'm quite aware that I'm being pedantic here, but....
Can we please start using the
pathattribute alias forvaluein our code and examples?😏
marveshy commentedon Mar 19, 2016
for my case the headers must be :
Content-Type: application/json
and i solve the problem good luck
cagataygurturk commentedon Sep 25, 2016
Hello, for me the problem persists for Spring boot 1.4.1 and Spring MVC 4.3.3.
I am trying to consume AWS SNS messages (which are interestingly sent with Content-Type: text/plain; charset=UTF-8 headers). My controller is like the following:
When I send request with
Content-Type: application/jsonheader manually and with JSON String, SNSMessage is serialized successfully by Jackson. When I change the header to text/plain, I getorg.springframework.web.HttpMediaTypeNotSupportedExceptionexception.From HTTP standarts point of view it is true, the response should be 415 because I send JSON instead of plain text but as I am not able to change the Content-Type header of AWS, so there should be an option to disable the Content-Type header check.
wilkinsona commentedon Sep 26, 2016
@cagataygurturk That's something that would have to be addressed in Spring MVC rather than in Spring Boot. Without any changes in Spring MVC, one thing that you could do is to register an HTTP message converter that treats
text/plainas JSON.rstoyanchev commentedon Sep 26, 2016
It's not a Content-Type header check but rather a failure to find a suitable
HttpMessageConverter. TheStringHttpMessageConvertercan deal with "text/plain" but it only converts to String. TheJacksonHttpMessageConvertercan convert toSNSMessagebut it only gets involved forapplication/json. You could configure theJacksonHttpMessageConverterto also converttext/plainbut that could cause you issues in other endpoints. I do not see what we could do in Spring MVC to make this better. We are in a worse position to make assumptions.Probably the simplest reliable thing to do is to insert the request body as a String and invoke the Jackson converter manually. This is just a weird situation to begin with. Another option might be to use a custom
ContentNegotiationStrategythat resolves the content type for very specific URLs. Then you can keep your method signature as it is.cagataygurturk commentedon Sep 26, 2016
@wilkinsona For me it is also an Spring MVC issue but as I saw the problem is being discussed here, I decided to post here.
@rstoyanchev You are right. A generic HttpMessageConverter could cause issues in other controllers because I want to do only one exception for one single controller. Well, for now I am deserialising it manually.
If you are interested about why AWS is sending text/plain Content-Type you can see this thread:
https://forums.aws.amazon.com/thread.jspa?threadID=69413
Very interesting response from an AWS employee:
ztgoto commentedon Dec 21, 2016
spring boot version: 1.4.1.RELEASE
response
philwebb commentedon Dec 21, 2016
@ztgoto This issue has been closed for a while. If you think you've found a new bug please open a fresh issue and provide a sample application that shows it.
user20161119 commentedon Sep 18, 2017
I got same issue,I use resttemplate request spring boot client as follow:
and the http client log show the Content-Type header is correct:
and response the exception
I am not sure why the Content-Type header change to
application/json;charset=UTF-8wilkinsona commentedon Sep 18, 2017
@user20161119 This issue has been closed for a while. If you think you've found a new bug please open a fresh issue and provide a sample application that shows it.
shihuncl commentedon Apr 29, 2018
I met the same problem. Did you solve it? How did you solve it?
kiranshiva commentedon Nov 12, 2018
specify
@JsonPropertyin entity class constructor like this.fernandongana commentedon Jan 15, 2019
ElbekEshkuvvatov commentedon Feb 15, 2022