What is exactly server.error.path property? - spring

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

Related

Spring Boot 2.4.2 and Thymeleaf 3.0.12 - access static methods

Since I switched to Spring Boot 2.4.2 my Thymeleaf templates are broken. When I want to access a static member in Spring Controller I get the following error:
Exception processing template "template_name": Instantiation of new
objects and access to static classes is forbidden in this context.
The code looks like:
th:text="${T(com.test).testMethod("1234")}"
Do you have any recommendation to fix this?
This change is the part of Thymeleaf 3.0.12. They improve restricted expression evaluation mode security by restriction of the access to static code (#identifier# in OGNL, T(identifier) in SpringEL). What they have done by themselves? ... "Avoided instantiation of new objects and calls to static classes" as stated in release notes. You may move the JAVA calls into your controller and put the result into the view model. After just access this variable from Thymeleaf template.
Another quick fix is the use of th:with
th:text="${testText}"
th:with="testText=${T(com.test).testMethod("1234")}"
Source/Kudos: https://github.com/thymeleaf/thymeleaf/issues/816#issuecomment-791921248 and https://github.com/thymeleaf/thymeleaf/issues/816#issuecomment-826401631
There is a workaround to use method from beans registered at the Spring Application Context with the #beanName syntax. Like this:
<div th:text="${#testService.testMethod('123')}">...</div>
http://www.thymeleaf.org/doc/articles/springmvcaccessdata.html

passing a Spring Exception to a Thymeleaf page

Having added the error.html Thymeleaf template to replace the default error page on Spring how can I get access to the exception on that page?
So I have custom exception thrown on some controller. How to access that exception on the Thymeleaf page?
I want this to work globally, so any controller thrown exception should be accessible.
Case 1: For customized error pages with spring default error info
There are some predefined objects in Thymeleaf to show error information, e.g. ${error}, ${exception} and so on. These objects can be used in your customized error page.
This article will help you more. Custom Error Page with Thymeleaf
Case 2: For customized error pages with customized error info
There are #ControllerAdvice and #ExceptionHandler used to handle exceptions in controllers globally. A certain exception can be add to Modal's attribute and therefore accessed in thymeleaf templates. However, please be noted that the solution does not work in Webflux.
The blog shows details about the solution. Exception Handling in Spring MVC

org.springframework.web.servlet.DispatcherServlet.noHandlerFound

If i want to map my source address only i.e. #RequestMapping(value="/"), then apache tomcat gives following error:
org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI [/application-name/]
Any other mapping works totally fine.
1)Make sure that you annotated your class containing handler method (the one annotated with #RequestMapping) with #Controller
2) If you don't use spring boot, you may need this annotation: #ComponentScan(/path to package with components/) in your configuration class.
This will tell where to look for spring components (controllers are one type of them)
It would be great if you show us your configuration and controller files.

Using Vaadin Error View instead of Spring Boot's Whitelabel error page

I'm using Spring Boot with Vaadin and. By using the #Autowired SpringNavigator, I have also set the error view:
navigator.setErrorView(ErrorView.class);
But when I type wrong URL like http://localhost:8080/asdf .
I always get the Spring Boot's Whitelabel error page. I know that I can set custom HTML page for the error like
/resources/public/error/404.html
but this is HTML way, I would like to use Vaadin with components,
the best solution would be the mentioned error view.

Spring messaging websockets - how to call setMessageCodec

I am trying to use Spring websockets with Genson instead of Jackson. When I try and connect from a client I get the following:
java.lang.IllegalStateException: A SockJsMessageCodec is required but not available: Add Jackson 2 to the classpath, or configure a custom SockJsMessageCodec.
It would appear I need to set a custom message codec.
I can see that a .setMessageCodec method appears on the TransportHandlingSockJsService but I can't see anywhere in the configuration options where I can actually set it.
I think the the .setMessageCodec method should be present on the SockJsServiceRegistration class so it can be set from configuration...but it isn't...any ideas?
EDIT: I believe this is a bug so have raised: https://jira.spring.io/browse/SPR-12091
Have a look at this issue https://jira.spring.io/browse/SPR-11184.
It looks like that you could implement it by overriding the configureMessageConverters method in WebSocketMessageBrokerConfigurer.

Resources