自定义HttpMessageConverter未被调用

时间:2018-02-10 18:47:59

标签: java spring rest spring-mvc

我尝试为RESTful API编写自定义HttpMessageConverter,但根本没有调用转换器。

这是我的控制器代码:

@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);
}

自定义HttpMessageConverter(方法为空以仅显示其配置方式):

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
}
}

转换器注册:

@EnableWebMvc
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {


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

}

任何建议可能是我的自定义转换器无法正常工作的原因?谢谢你的任何建议!

0 个答案:

没有答案