Why do we need to customize MessageConverter when we want to have some data returned in our custom format, rather than plain JSON or XML format data. And the underlying one is ultimately the one that traverses MessageConverter and then transforms the data.

First you need to create your own HttpMessageConverter as follows

public class MyMessageConverter implements HttpMessageConverter<Pet> {

    @Override
    public boolean canRead(Class
        clazz, MediaType mediaType) {
        return false;
    }
    //Pet is just a POJO class for testing
    @Override
    public boolean canWrite(Class
        clazz, MediaType mediaType) {
        return clazz.isAssignableFrom(Pet.class);
    }
   // Set the media type that can be converted
    @Override
    public List<MediaType> getSupportedMediaTypes(a) {
        return MediaType.parseMediaTypes("application/mydata");
    }

    @Override
    public List<MediaType> getSupportedMediaTypes(Class
        clazz) {
        return HttpMessageConverter.super.getSupportedMediaTypes(clazz);
    }

    @Override
    public Pet read(Class<? extends Pet> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
        return null;
    }

    @Override
    public void write(Pet pet, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
        OutputStream body = outputMessage.getBody();
        String myout="Test data"+pet.getName()+pet.getAge(); body.write(myout.getBytes()); }}Copy the code

We then throw our custom HttpMessageConverter into our SpringMVC container, which is typically added via WebMvcConfigurer, as shown below

@Configuration
public class MyMessageConverterConfig {

    @Bean
    public WebMvcConfigurer webMvcConfigurer(a){
        return new WebMvcConfigurer() {
            @Override
            public void extendMessageConverters(List
       
        > converters)
       > {
                converters.add(newMyMessageConverter()); }}; }}Copy the code

Note that this only works if the request header Accept is set to a custom media type (application/ myData).

If you want to change the return type of the data by adding the request parameter format, you can do the following

// Customize the strategies, note that the above code is also retained, as the final processing is still the above HttpMessageConverter
@Configuration
public class MyMessageConverterConfig {

    @Bean
    public WebMvcConfigurer webMvcConfigurer(a){
        return new WebMvcConfigurer() {
            @Override
            public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
                Map<String, MediaType> mediaTypes = new HashMap<>();
                mediaTypes.put("json", MediaType.APPLICATION_JSON);
                mediaTypes.put("xml", MediaType.APPLICATION_XML);
                mediaTypes.put("mydata", MediaType.parseMediaType("application/mydata"));
                ParameterContentNegotiationStrategy strategys = new ParameterContentNegotiationStrategy(mediaTypes);
                // Without header, in the absence of format, whatever value accept is returned as JSON data
                HeaderContentNegotiationStrategy headerStrategy = new HeaderContentNegotiationStrategy();
                configurer.strategies(Arrays.asList(strategys,headerStrategy));
            }

            @Override
            public void extendMessageConverters(List
       
        > converters)
       > {
                converters.add(newMyMessageConverter()); }}; }}Copy the code

Once added, the underlying strategies are as follows

The default is as follows