Guest User

Untitled

a guest
Oct 17th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. @RestController
  2. public class LoginController {
  3.  
  4. @RequestMapping(value = "/login", method = POST, produces = "application/json")
  5. ResponseEntity<LoginResponse> login(@RequestBody LoginRequest loginRequest) {
  6. // ...
  7. }
  8. }
  9.  
  10. {
  11. "timestamp": 1423844498998,
  12. "status": 415,
  13. "error": "Unsupported Media Type",
  14. "exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
  15. "message": "Content type 'text/plain;charset=UTF-8' not supported",
  16. "path": "/login/"
  17. }
  18.  
  19. public class CustomerJsonHttpMessageConverter extends AbstractHttpMessageConverter<Object> {
  20.  
  21. private ObjectMapper mapper = new ObjectMapper();
  22. private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
  23.  
  24. public CustomerJsonHttpMessageConverter() {
  25. super(new MediaType("application", "json", DEFAULT_CHARSET));
  26. }
  27.  
  28. @Override
  29. protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException,
  30. HttpMessageNotReadableException {
  31. return mapper.readValue(inputMessage.getBody(), clazz);
  32. }
  33.  
  34. @Override
  35. protected boolean supports(Class<?> clazz) {
  36. return true;
  37. }
  38.  
  39. @Override
  40. protected void writeInternal(Object value, HttpOutputMessage outputMessage) throws IOException,
  41. HttpMessageNotWritableException {
  42. String json = mapper.writeValueAsString(value);
  43. outputMessage.getBody().write(json.getBytes());
  44. }
  45.  
  46. MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
  47. converter.setSupportedMediaTypes(
  48. Arrays.asList(
  49. new MediaType("text", "plain"),
  50. new MediaType("text", "html")
  51. ));
  52.  
  53. @Bean
  54. public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
  55. MappingJackson2HttpMessageConverter converter =
  56. new MappingJackson2HttpMessageConverter(new CustomObjectMapper());
  57. converter.setSupportedMediaTypes(Arrays.asList(MediaType.ALL));
  58. return converter;
  59. }
Add Comment
Please, Sign In to add comment