【问题标题】:Rest Custom HTTP Message Converter Spring Boot 1.2.3Rest 自定义 HTTP 消息转换器 Spring Boot 1.2.3
【发布时间】:2015-08-25 19:15:11
【问题描述】:

我想使用 Rest、Json、Spring Boot 1.2.3 和 Spring 4 创建一个自定义的 HttpMessageConverter,但是我的自定义 HTTPMessageConverter 从未调用过。

我已经执行了以下步骤:

1:创建了一个扩展 AbstractHttpMessageConverter 的类

@Component
public class ProductConverter extends AbstractHttpMessageConverter<Employee>   {

public ProductConverter() {
     super(new MediaType("application", "json", Charset.forName("UTF-8")));     
     System.out.println("Created ");
}

@Override
protected boolean supports(Class<?> clazz) {
    return false;
}

@Override
protected Employee readInternal(Class<? extends Employee> clazz,
        HttpInputMessage inputMessage) throws IOException,
        HttpMessageNotReadableException {
    InputStream inputStream =  inputMessage.getBody();
    System.out.println("Test******");
    return null;
}

@Override
protected void writeInternal(Employee t,
        HttpOutputMessage outputMessage) throws IOException,
        HttpMessageNotWritableException {
    // TODO Auto-generated method stu   
}

}

2:我创建一个配置类来注册HTTPMessageConverters

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter{  
   @Override
   public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
     System.out.println("Configure Message Converters");
     converters.add(new ProductConverter());
     super.configureMessageConverters(converters);
     //super.extendMessageConverters(converters);
    }
 }

3:其余类方法

@RequestMapping(value="/{categoryId}" ,method=RequestMethod.POST, consumes="application/json")
@PreAuthorize("permitAll")
public ResponseEntity<ProductEntity>  saveProduct(@RequestBody Employee  employee , @PathVariable Long categoryId) {

    logger.log(Level.INFO, "Category Id: {0}" , categoryId);

    ResponseEntity<ProductEntity> responseEntity =
            new ResponseEntity<ProductEntity>(HttpStatus.OK);
    return responseEntity;
}

我的自定义 HTTPMessageCoverter 它已创建但从未被调用?是否有我缺少的配置或步骤?任何意见或建议表示赞赏。


重写(AbstractHttpMessageConverter)类方法后,我发现有两个注解用于实现多态@JsonTypeInfo 和@JsonSubTypes。对于任何想要实现多态性的人都可以使用这两个注释。

【问题讨论】:

    标签: spring spring-boot spring-4 spring-restcontroller spring-json


    【解决方案1】:

    我相信您想在扩展 WebMvcConfigurerAdapter 的配置类中使用 configureMessageConverters 方法来配置这些消息转换器。我自己使用 CSV 内容的转换器完成了这项工作。我在下面包含了该代码。 This link 也显示了一个示例。 This link 也可能会有所帮助。似乎对于 Spring 配置,配置事物的最佳位置并不总是很清楚。 :) 让我知道这是否有帮助。

    @Configuration
    public class ApplicationWebConfiguration extends WebMvcConfigurerAdapter {
         @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            super.configureMessageConverters(converters);
            converters.add(new CsvMessageConverter());
        }
    }
    

    您还需要修改您的supports() 方法以对转换器支持的类返回true。见the Spring doc for AbstractHttpMessageConverter supports method

    【讨论】:

    • 谢谢@Rob。我已经进行了更改,但我的自定义转换器仍然从未调用过。从日志中我可以看到创建了自定义封面并调用了 configureMessageConverters 方法。
    • 在更仔细地查看您的代码时,我发现您的 supports() 方法总是返回 false。对于要转换的类,它需要返回 true。另一件事是,假设您的类路径中有 Jackson,您不需要为 JSON 创建自定义转换器,因为 Jackson 转换器包含在每个 docs.spring.io/spring/docs/current/spring-framework-reference/… 的类路径中。您可能还想在 REST 上查看此 Spring 指南 spring.io/guides/tutorials/bookmarks
    • 感谢@Rob,这是支持方法。我知道我不应该创建自定义转换器,但我想试验一下,看看我是否可以返回属于类别的子产品类..
    • @SpringBootApplication public class FooApplication extends WebMvcConfigurerAdapter { public static void main(String[] args) { SpringApplication.run(FooApplication.class, args); } @Override public void configureMessageConverters(List&lt;HttpMessageConverter&lt;?&gt;&gt; converters) { super.configureMessageConverters(converters); converters.add(new FastJsonHttpMessageConverter()); } } 为什么这段代码不起作用
    • @zhguuowei 如果没有更多信息,几乎不可能说出为什么您的方法不起作用,您还需要定义“不起作用”的含义。我建议创建一个新问题,其中包含我们可以查看的更详细信息。请务必包含您正在使用的版本,您看到的行为等。
    猜你喜欢
    • 2016-07-11
    • 1970-01-01
    • 2020-02-04
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多