4

I try to write custom HttpMessageConverter for RESTful API but the converter is not being called at all.

Here is my controller code:

@RequestMapping(value = "/devices/statuses/{serial}", method=RequestMethod.GET,produces = "application/json;charset=UTF-8")
public ResponseEntity<List<DeviceStatus>> getDeviceStatuses(@PathVariable(value="serial") String serial,
                                @RequestParam("from") long from,
                                @RequestParam("to") long to) {

    List<DeviceStatus> statuses = systemFacade.getDeviceStatuses(serial, new DateTime(from), new DateTime(to));
    return new ResponseEntity<List<DeviceStatus>>(statuses,HttpStatus.OK);
}

Custom HttpMessageConverter (the methods are empty to show only how it is configured):

public class HosHttpMessageConverter extends AbstractHttpMessageConverter<Object>{

public HosHttpMessageConverter() {
    super(MediaType.APPLICATION_JSON_UTF8);
}

@Override
protected boolean supports(Class<?> arg0) {
    return true;
}

@Override
protected Object readInternal(Class<? extends Object> clazz, HttpInputMessage message)
        throws IOException, HttpMessageNotReadableException {
    // TODO Auto-generated method stub
    return null;
}

@Override
protected void writeInternal(Object object, HttpOutputMessage message)
        throws IOException, HttpMessageNotWritableException {
    // TODO Auto-generated method stub
}
}

Converter registrations:

@EnableWebMvc
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {


    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new HosHttpMessageConverter());
    }

}

Any suggestion what can be the reason why my custom converter not working? Thanks for any suggestions!

lukassko
  • 135
  • 10
  • how did you made sure that your converter isn't calld?You have nothing in your converter – pvpkiran Feb 10 '18 at 18:52
  • It's only empty method paste here to show how it is configured. In my app I put some log to console to check if something was called during tests. – lukassko Feb 10 '18 at 20:43
  • very weird! how about trying this directly inside of the constructor? this.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON)); – Steve Park Jan 05 '19 at 19:25

0 Answers0