Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @RestController
- public class LoginController {
- @RequestMapping(value = "/login", method = POST, produces = "application/json")
- ResponseEntity<LoginResponse> login(@RequestBody LoginRequest loginRequest) {
- // ...
- }
- }
- {
- "timestamp": 1423844498998,
- "status": 415,
- "error": "Unsupported Media Type",
- "exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
- "message": "Content type 'text/plain;charset=UTF-8' not supported",
- "path": "/login/"
- }
- public class CustomerJsonHttpMessageConverter extends AbstractHttpMessageConverter<Object> {
- private ObjectMapper mapper = new ObjectMapper();
- private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
- public CustomerJsonHttpMessageConverter() {
- super(new MediaType("application", "json", DEFAULT_CHARSET));
- }
- @Override
- protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException,
- HttpMessageNotReadableException {
- return mapper.readValue(inputMessage.getBody(), clazz);
- }
- @Override
- protected boolean supports(Class<?> clazz) {
- return true;
- }
- @Override
- protected void writeInternal(Object value, HttpOutputMessage outputMessage) throws IOException,
- HttpMessageNotWritableException {
- String json = mapper.writeValueAsString(value);
- outputMessage.getBody().write(json.getBytes());
- }
- MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
- converter.setSupportedMediaTypes(
- Arrays.asList(
- new MediaType("text", "plain"),
- new MediaType("text", "html")
- ));
- @Bean
- public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
- MappingJackson2HttpMessageConverter converter =
- new MappingJackson2HttpMessageConverter(new CustomObjectMapper());
- converter.setSupportedMediaTypes(Arrays.asList(MediaType.ALL));
- return converter;
- }
Add Comment
Please, Sign In to add comment