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?