Spring Rest Post Json Requestbody Content Type Not Supported

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 have consumes content type something other than text/plain like application/json. The text/plain content won't be converted to Map by any converter. Otherwise, take request body as String and internally convert it to Map 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



Leave a reply



Submit