[ad_1]
I have the following route on a Spring Boot application:
@SuppressWarnings("unused")
@GetMapping("/status")
public Object status() {
String statusMessage = "disconnected";
String error = null;
try {
Db db = new Db();
if (db.isConnected()) {
statusMessage = "ok";
}
} catch (Exception e) {
error = e.getMessage();
}
return new Object() {
public String project = applicationName;
public String version = buildVersion;
public String connection = statusMessage;
public String message = error;
};
}
However the lines public String connection = statusMessage;
and public String message = error;
have the following error:
Local variable statusMessage defined in an enclosing scope must be final or effectively final
How can I fix this besides creating a new final variable that receives whatever error
or statusMessage
has before using it in the new Object?
Note: The reason why I am returning a new Object is because its the simplest way I could find to return a POJO for spring to turn it into JSON without having to create a class for every route return.
[ad_2]