Spring Boot in Cloud Foundry (PCF) disable Whitelabel Error Page - spring

Related question:
Spring Boot Remove Whitelabel Error Page
For my case,
I disabled whitelabel by setting whitelabel.enabled = false, and I also exclude ErrorMvcAutoConfiguration. It worked in regular spring boot service. But I deployed the same service on PCF cloud foundry, then spring still want to redirect error to /error page.
Any help and suggestion is welcome.
Edit:
I added exclude annotation on Application, then it works on PCF.
Previously I added exclude configuration in application.yml, then it didn't work on PCF

You need to create a separate endpoint /error and then handle it in the method. I would suggest you to maintain a separate controller infact. My code would look something like this
#RestController
#RequestMapping(path = "/error")
public class ErrorController {
#ApiOperation(value = "Error Page", notes = "Error Page")
#GetMapping
public String error() {
return "Some Error Occurred and I will Graciously show the Error"
}
}

It turns out circuit breaker service set exclusion property first, than local application.yml didn't take effect. If I add exclusion property in repo, then it take preference.
I feel this is kind of spring bug, since exclusion is list, it should include all items, instead of taking the first configuration only.

Related

Why are details not being returned for spring health indicator?

I am currently using spring boot 2.1.4 and jackson 2.9.8
When hitting the health endpoint the details are not displayed.
{
status: "UP"
}
I have added the following property to my configuration.
management.endpoint.health.show-details=always
When I look at the environment management.endpoint.health.show-details it is set.
When I monitor org.springframework.boot.actuate.health.HealthWebEndpointResponseMapper.HealthWebEndpointResponseMapper(HealthStatusHttpMapper, ShowDetails, Set<String>) it is showing that ALWAYS is in fact being used, and details is not being stripped out.
However, when it gets to com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector it doesn't add details, because the method getDetails has not been annotated in org.springframework.boot.actuate.health.Health like it has with getStatus (being #JsonWrapped)
How do I get details to be serialized by Jackson, so they can be returned via the health endpoint?
EDIT: This appears to be getting caused by a custom ObjectMapper that is being injected into the spring environment at startup which sets both AUTO_DETECT_GETTERS and AUTO_DETECT_IS_GETTERS to disabled. While this may of worked in spring 4, it no longer appears to work in spring 5.

What is exactly server.error.path property?

In Spring Boot, what is the purpose of server.error.path property in application.properties file?
The documentation just says:
Path of the error controller
But I want a clear description of this property with an example.
server.error.path - used as part of url for error pages.
site.getBaseUrl() + "/error"
For example some error happen on server side and you decide redirect user to error page like this:
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/images/custom-error-page-aws-404-example.png
Code example of error controller you can find here:
https://www.logicbig.com/tutorials/spring-framework/spring-boot/implementing-error-controller.html
You can use this property in #RequestMapping("/error"). But instead of "/error" you can use "${server.error.path}"
UPDATE:
Also, Spring Boot BasicErrorController use server.error.path property
Property server.error.path in spring boot application used to define an error path while dealing with custom error handler. In Spring we create custom error handler using functional interface ErrorController, ths interface has a String type method getErrorPath which helps us to return the error page path(our error page as view).
But from Spring 2.3.0 this getErrorPath() method has been deprecated and replaced with server.error.path to manage the error path.
e.g. server.error.path=/error
For more detail about interface ErrorController, please refer Spring doc for ErrorController

Spring Boot Actuator paths not enabled by default?

While updating my Spring Boot application to the latest build snapshot and I am seeing that none of the actuator endpoints are enabled by default. If I specify them to be enabled in application.properties, they show up.
1) Is this behavior intended? I tried searching for an issue to explain it but couldn't find one. Could somebody link me to the issue / documentation?
2) Is there a way to enable all the actuator endpoints? I often find myself using them during development and would rather not maintain a list of them inside my properties file.
Two parts to this answer:
"Is there a way to enable all the actuator endpoints?"
Add this property endpoints.enabled=true rather than enabling them individually with endpoints.info.enabled=true, endpoints.beans.enabled=true etc
Update: for Spring Boot 2.x the relevant property is:
endpoints.default.web.enabled=true
"Is this behavior intended?"
Probably not. Sounds like you might have spotted an issue with the latest milestone. If you have a reproducible issue with a Spring Boot milestone then Spring's advice is ...
Reporting Issues
Spring Boot uses GitHub’s integrated issue tracking system to record bugs and feature requests. If you want to raise an issue, please follow the recommendations below:
Before you log a bug, please search the issue tracker to see if someone has already reported the problem.
If the issue doesn’t already exist, create a new issue.
Even if we enable all the actuator endpoints as below
management.endpoints.web.exposure.include=* (In case of YAML the star character should be surrounded by double quotes as "*" because star is one of the special characters in YAML syntax)
The httptrace actuator endpoint will still not be enabled in web by default. HttpTraceRepository interface need to be implemented to enable httptrace (See Actuator default endpoints, Actuator endpoints, Actuator httptrace).
#Component
public class CustomHttpTraceRepository implements HttpTraceRepository {
AtomicReference<HttpTrace> lastTrace = new AtomicReference<>();
#Override
public List<HttpTrace> findAll() {
return Collections.singletonList(lastTrace.get());
}
#Override
public void add(HttpTrace trace) {
if ("GET".equals(trace.getRequest().getMethod())) {
lastTrace.set(trace);
}
}
}
Now the endpoints can be accessed using the url,
http://localhost:port/actuator/respective-actuator-endpoint
(Example http://localhost:8081/actuator/httptrace)
If there is a management.servlet.context-path value present in properties file then the URL will be,
http://localhost:port/<servlet-context-path>/respective-actuator-endpoint
(Example http://localhost:8081/management-servlet-context-path-value/httptrace)
UPDATE: use this only in dev environment, not in production!
Is there a way to enable all the actuator endpoints?
Using Spring Boot 2.2.2 Release, this worked for me:
On the file src/main/resources/application.properties add this:
management.endpoints.web.exposure.include=*
To check enabled endpoints go to http://localhost:8080/actuator
Source: docs.spring.io

Spring boot exception handling with controller advice and hosting static pages

I am developing a Spring Boot REST API. It uses #ControllerAdvice and #ExceptionHandler in a class that extends ResponseEntityExceptionHandler to catch exceptions. To get this to work, we added these two fields to the config:
spring.mvc.throw-exception-if-no-handler-found: true
spring.resources.add-mapping: false
The problem is I am also trying to host static documents that I would like spring to map. I added this to my config:
spring.resources.static-locations: classpath:/static/html5/,classpath:/public/html5/
It seems I cant set mapping to false and map static resources. If I dont set add-mapping to false, I can't catch 404s.
Is there a spring way to solve this? If not, I am going to try to define an endpoint for each static resource and find the files in the jar, which seems like a hack.
Thanks in advance for any suggestions!

Mapping /error does not work in spring boot

I am developing a spring boot application, using boot v 1.2.5, using thymeleaf as the view technology
In my Mvc Configuration file
#Configuration
public class MVCConfig extends WebMvcConfigurerAdapter {
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/message").setViewName("message");
registry.addViewController("/error").setViewName("errors/error");
registry.addViewController("/login").setViewName("authentication/login");
}
The problem is that the first and last mapping is working fine, but the middle one "error" is not working at all !!
I am using the same rules as the other two templates, same prefix, same directory structure, so it is supposed to work like the other two !!
But, when i point my browser to /error, spring boot show the fallback error page saying that there is no mapping for /error
when i change it to:
registry.addViewController("/error").setViewName("errors/error");
it works fine for /errors
Spring boot documentation states about the /error handling:
"For browser clients there is a ‘whitelabel’ error view that renders the same data in HTML format (to customize it just add a View that resolves to ‘error’)."
so, it says to add a view that resolve to error, that's what i did, but it does not work !!
so, what is the problem? why /error does not work?
You've added a view controller for /error, not a view. This is then being overridden by Boot's own controller with a /error mapping. As a result, you get the default error page.
All you need to do is to provide an error view. As you're using Thymeleaf, you can do so by creating a file named src/main/resources/templates/error.html.

Resources