Home > Net >  Spring Boot "redirect:/path" doesn't work - actually returning the String "redir
Spring Boot "redirect:/path" doesn't work - actually returning the String "redir

Time:09-28

I'm trying to redirect from one my controllers to another controller. It should work with the keyword "redirect", but instead of redirecting to he other controller, I'm getting this response:

response

Here is my Controller:

@RestController
@Api(value = ConsentConstants.GET_HEALTH_V1, tags = {ConsentConstants.SWAGGER_API})
@Slf4j
@FieldDefaults(level = AccessLevel.PRIVATE)
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class GetHealthController implements GetHealthApi {

  final Tracer tracer;

  @Override
  public Object getHealthCheck() {
    return "redirect:/actuator/health";
  }
}

The reason I'm trying the redirect is because the healthcheck (spring boot actuator) is not working when I deploy my service. Locally it works fine, but after deployment I receive a 401 unauthorized for the /actuator/health endpoint (the other endpoints work fine). So I wanted to try to use one of the working endpoints (e.g. status/health and then redirect to /actuator/health)

I'm not using Spring MVC, is that's making any difference?

CodePudding user response:

This way of redirecting only works for regular controllers, not a rest service. There is no way to resolve a 'view' named /actuator/health.

This only works when there is a view with a filename of registration to resolve:

return "redirect:registration";

To redirect in a restcontroller, you can do something like this:

@GetMapping("/healthcheck")
public void getHealthCheck(HttpServletResponse response) throws IOException {
  response.sendRedirect("/actuator/health");
}

However, if you just want to change the health endpoint of Spring actuator it is explained here.

In application.properties:

management.endpoints.web.base-path=/whatever
management.endpoints.web.path-mapping.health=healthcheck
  • Related