Content type blank is not supported
I finally configured it and it is working. Here is the correct configuration for MappingJackson2HttpMessageConverter
@Configuration(proxyBeanMethods = false)
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(jacksonMessageConverter());
WebMvcConfigurer.super.configureMessageConverters(converters);
}
@Bean
public MappingJackson2HttpMessageConverter jacksonMessageConverter() {
MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
List<MediaType> supportedMediaTypes=new ArrayList<>();
supportedMediaTypes.addAll(messageConverter.getSupportedMediaTypes());
messageConverter.setSupportedMediaTypes(supportedMediaTypes);
supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
messageConverter.setSupportedMediaTypes(supportedMediaTypes);
messageConverter.setPrettyPrint(true);
return messageConverter;
}
Aso add the APPLICATION_OCTET_STREAM_VALUE } in the controller method you want to support the octet-stream.
consumes = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_OCTET_STREAM_VALUE
}
"Content type 'application/json;charset=UTF-8' not supported" in Spring Rest application
I was able to solve it by removing @JsonManagedReference.
Spring REST Controller content/type not supported for all content types
Because of this
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(jacksonMessageConverter());
super.configureMessageConverters(converters);
}
Spring MVC skips all the default converters it would have otherwise registered. (If you're curious, this is done in WebMvcConfigurationSupport#getMessageConverters(..)
.)
Your only HttpMessageConverter
, MappingJackson2HttpMessageConverter
, can only read MediaType.APPLICATION_JSON
content, ie. application/json
. Therefore, every other request content type will be rejected.
You can register all the regular defaults yourself in your configureMessageConverters
override (or just the ones you need to read HTML forms, XML, plain text, etc.).
Or, you could instead override extendMessageConverters
to find the default MappingJackson2HttpMessageConverter
instance and configure it to use your custom ObjectMapper
. For example,
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
ObjectMapper mapper = new ObjectMapper();
// ...initialize...
for (HttpMessageConverter<?> converter : converters) {
if (converter instanceof MappingJackson2HttpMessageConverter) {
MappingJackson2HttpMessageConverter m = (MappingJackson2HttpMessageConverter) converter;
m.setObjectMapper(mapper);
}
}
}
And maybe drop a comment about relying on the default list of converters.
Content type 'application/json' not supported in Spring MVC and jackson
Here is my working example:
@SpringBootApplication
@Controller
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
@RequestMapping(value = "/testing", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
private void testing(@RequestBody Request body, @RequestHeader HttpHeaders headers, HttpServletRequest httpRequest) {
System.out.println(body.getTest());
}
}
This project has only 1 dependency:
org.springframework.boot:spring-boot-starter-web
When I call the url like this:
curl -XPOST -v -d '{ "test": "abc123" }' -H "Content-type: application/json" http://localhost:8080/testing
I see the correct abc123
in the logs. If I remove Content-type
header I get the exception
org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content type 'application/x-www-form-urlencoded' not supported
Spring Boot Content type 'text/plain' not supported in POST request in terminal
Please modify a couple of things:
- In case you expect a
Map
in the request body, you need to haveconsumes
content type something other thantext/plain
likeapplication/json
. Thetext/plain
content won't be converted toMap
by any converter. Otherwise, take request body asString
and internally convert it toMap
in your code. - In curl request add
-X POST
. Also, make the payload structure JSON key value pairs.
You are getting 405 error code due to text content in payload and expected request as Map
data type. To confirm this, just remove the request body Map
and see whether your API is hit. And then follow the steps above.
Related Topics
Enter Array Without Knowing Its Size
How to Check Whether a Field Exists or Not in Mongodb
Correct Way to Focus an Element in Selenium Webdriver Using Java
Tomcat Server Error - Port 8080 Already in Use
How to Configure Hikaricp in My Spring Boot App in My Application.Properties Files
Spring Data JPA and Exists Query
How to Remove Line Breaks from a File in Java
How to Query Using an Enum Parameter Mapped as Ordinal Using JPA and Hibernate
How to Print the List of Employees Who Have Joined on a Particular Month from an Employee List
Using Streams to Convert a List of Objects into a String Obtained from the Tostring Method
How to Process a Multipart Request Consisting of a File and a Json Object in Spring Restful Service
Nb: Java_Home Should Point to a Jdk Not a Jre
Createprocess Error=2, the System Cannot Find the File Specified
How to Skip @Postconstruct When Unit Testing
Re-Prompt User After Invalid Input in Java
How to Open a .Dat File in Java Program
Get Requestbody and Responsebody At Handlerinterceptor
Java Stream:Create New List from 2 Lists Keeping Only Matching Values