Use ResponseBodyAdvice to remove double quotation marks from the response body encryption result

example ResponseBodyAdvice :

@Order(1)
@ControllerAdvice
public class EncryptBodyAdvice implements ResponseBodyAdvice {

    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        return returnType.getMethod().isAnnotationPresent(EncryptBody.class);
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
                                  Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        if(body==null) return null;
        EncryptBodyMethod encryptBodyMethod;
        if(returnType.getMethod().isAnnotationPresent(EncryptBody.class)){
            EncryptBody encryptBody = returnType.getMethodAnnotation(EncryptBody.class);
            encryptBodyMethod = encryptBody.value();
        }
        response.getHeaders().setContentType(MediaType.TEXT_PLAIN);
        String s = JSON.toJSONString(body);
        return "3232312";
    }
}

in addition, it is useless to configure ConverterConfig :

@Configuration
public class HttpConverterConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setSupportedMediaTypes(new LinkedList<MediaType>(){{
            add(MediaType.TEXT_HTML);
            add(MediaType.APPLICATION_JSON_UTF8);
        }});
        converters.add(new StringHttpMessageConverter());
        converters.add(converter);
    }
}

in any case, the result returned is

"3232312"

with double quotation marks like above, it will not be available unless the return value of the controller is changed to String. Is there any way to solve it?


has been resolved. Override the writeInternal method of MappingJackson2HttpMessageConverter . The complete configuration is as follows:

@Configuration
public class HttpConverterConfig extends WebMvcConfigurerAdapter {

    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(){
        return new MappingJackson2HttpMessageConverter(){
            @Override
            protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
                if(object instanceof String){
                    Charset charset = this.getDefaultCharset();
                    StreamUtils.copy((String)object, charset, outputMessage.getBody());
                }else{
                    super.writeInternal(object, type, outputMessage);
                }
            }
        };
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter converter = mappingJackson2HttpMessageConverter();
        converter.setSupportedMediaTypes(new LinkedList<MediaType>(){{
            add(MediaType.TEXT_HTML);
            add(MediaType.APPLICATION_JSON_UTF8);
        }});
        converters.add(new StringHttpMessageConverter());
        converters.add(converter);
    }
}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1c1fba3-32c18.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1c1fba3-32c18.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?