TLDR; Есть ли способ написать ResponseBodyAdvice для ответов, поступающих от шаблонизатора Thymeleaf?
Для «нормальной» конечной точки, аннотированной с помощью @RequestMapping и @ResponseBody, я могу написать ResponseBodyAdvice для изменения / обработки ответа перед его записью в выходной поток. Теперь, есть ли способ сделать то же самое с конечной точкой, возвращающей имя представления шаблона (в данном случае Thymeleaf)?
Пример, который работает:
@Controller
public class MyController
{
@GetMapping("/path")
@ResponseBody
public String dispatch(final HttpServletRequest request,
final HttpServletResponse response)
throws Exception
{
return "my output string...";
}
}
@ControllerAdvice
public class MyControllerAdvice implements ResponseBodyAdvice<String>
{
@Override
public boolean supports(final MethodParameter returnType,
final Class<? extends HttpMessageConverter<?>> converterType)
{
// This is called
return true;
}
@Override
public String beforeBodyWrite(final String body,
final MethodParameter returnType,
final MediaType selectedContentType,
final Class<? extends HttpMessageConverter<?>> selectedConverterType,
final ServerHttpRequest request,
final ServerHttpResponse response)
{
// This is called
// Perform the response logic...
return body;
}
}
Пример, который НЕ работает:
@Controller
public class MyController
{
@GetMapping("/path")
public String dispatch(final HttpServletRequest request,
final HttpServletResponse response)
throws Exception
{
return "viewName"; // will be resolved to a view by the templating engine (Thymeleaf) - typically viewName.html - and evaluated
}
}
@ControllerAdvice
public class MyControllerAdvice implements ResponseBodyAdvice<Object>
{
@Override
public boolean supports(final MethodParameter returnType,
final Class<? extends HttpMessageConverter<?>> converterType)
{
// This is never called
return true;
}
@Override
public Object beforeBodyWrite(final Object body,
final MethodParameter returnType,
final MediaType selectedContentType,
final Class<? extends HttpMessageConverter<?>> selectedConverterType,
final ServerHttpRequest request,
final ServerHttpResponse response)
{
// This is never called
// Perform the response logic...
return body;
}
}
Трудно сказать, просто не совпал ли приведенный выше совет контроллера с выходным сигналом механизма шаблонов или есть совет контроллера для более конкретного типа, выбранного на его месте ...
С уважением,
Blasphemic