Save Custom Spring HTTP Message Converter Boot 1.2.3

I want to create a custom HttpMessageConverter using Rest, Json, Spring Boot 1.2.3 and Spring 4, but my custom HTTPMessageConverter never called it.

I followed these steps:

1: Created a class that extends 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: I am creating a config class to register 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: resting class method

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

      

My custom HTTPMessageCoverter that it created but never gets called? Is there a configuration or step I am missing? any input or advice is appreciated.


After overriding the (AbstractHttpMessageConverter) class methods, I discovered two annotations to achieve polymorphism @JsonTypeInfo and @JsonSubTypes. For those looking to achieve polymorphism, these two annotations can be used.

+3


source to share


1 answer


I believe you want to customize these message converters using the configureMessageConverters method in the configuration class that extends WebMvcConfigurerAdapter. I did it myself with a converter for CSV content. I have included this code below. This link shows an example. This link might also be helpful. It looks like it's not always clear when configuring Spring what is the best to configure. :) Let me know if it helps.

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

      



You will also need to change the maintainer () method to return true for the classes supported by the converter. See Spring doc for AbstractHttpMessageConverter supports method .

+3


source







All Articles