Reputation: 1879
im trying to parse a simple object in a POST method but I'm always getting this error:
2017-02-23 03:52:45 DEBUG AnnotationMethodHandlerExceptionResolver:133 - Resolving exception from handler [com.dlo.food.controller.Lists@40079d7e]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported
2017-02-23 03:52:45 DEBUG ResponseStatusExceptionResolver:133 - Resolving exception from handler [com.dlo.food.controller.Lists@40079d7e]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported
2017-02-23 03:52:45 DEBUG DefaultHandlerExceptionResolver:133 - Resolving exception from handler [com.dlo.food.controller.Lists@40079d7e]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported
In my pom.xml I've included the jackson library files:
...
<properties>
<jackson.version>2.6.3</jackson.version>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
...
And I've declared the controller this way:
/**
*
* @param request
* @return
* @return
*/
@ResponseBody
@RequestMapping(value = "/uiupdatepost", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> UIUpdatePost(@RequestBody Category category, @RequestHeader HttpServletRequest request) {
try {
return ResponseEntity.ok("");
} catch (Throwable t) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(new UIError(t).toHTML());
}
}
The class category is very simple:
public class Category implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public Category() {
//
}
private String id;
private String name;
private String priority;
private String categoryId;
private String color;
/**
ALL GETTER AND SETTERS
**/
}
If I run CURL:
curl -H "Content-Type: application/json" -X POST -d '{"id":"1","name":"PIZZA","priority":"1","color":"","categoryId":""}' http://localhost:8080/food/uiupdatepost.html
I get this error:
HTTP: 415
I've searched all the possible solutions included:
Also tested the manual deserialization without problems:
Category userFromJSON = mapper.readValue("{\"id\":\"1\",\"name\":\"PIZZA\",\"priority\":\"1\",\"color\":\"\",\"categoryId\":\"\"}", Category.class);
Upvotes: 1
Views: 6602
Reputation: 493
Sounds, that Spring wasn't able to create an appropriate HTTPMessageConverter able to parse JSON for some reason. If jackson is on the classpath - as it is as contained in your pom-xml - the converter should be registered in method org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.addDefaultHttpMessageConverters
I'd suggest to check, why this isn't happening. Alternatively you can configure appropriate HttpMessageConverter
by hand. If you've a configuration class derived from org.springframework.web.servlet.config.annotation.WebMvcConfigurer
you would have something like this:
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
converters.add(new MappingJackson2XmlHttpMessageConverter());
converters.add(new MappingJackson2HttpMessageConverter());
}
Upvotes: 2
Reputation: 1879
Welp.. I want to cry... after hours and hours, changing :
MediaType.APPLICATION_JSON_VALUE
for
MediaType.APPLICATION_JSON_UTF8_VALUE
worker for me.
Upvotes: 1