How to redirect springboot error page to vaadin error UI? - spring-boot

I have an error page in vaadin UI. But sometimes we can write a wrong url and we see the springboot error page.
I want to show vaadin UI error page in this case. By the way, I have already have a rendered 404 page for springboot. But I don't want to show it.
This is my vaadin error UI. But this works into the application. (http://localhost:7001/MyApplication/#!error)
sometimes I write an invalid url like this: http://localhost:7001/MyApplication/blablablabla
in this case I want to redirect vaadin error page (http://localhost:7001/MyApplication/#!error) But the springboot redirects me to rendered 404 page.
It is possible?
#UIScope
#SpringView(name = ErrorView.VIEW_NAME)
public class ErrorView extends VerticalLayout implements View {
public static final String VIEW_NAME = "error";
private Label explanation;
public ErrorView() {
Label header = new Label("The view could not be found");
header.addStyleName(MaterialTheme.LABEL_H1);
addComponent(header);
addComponent(explanation = new Label());
}
#Override
public void enter(ViewChangeListener.ViewChangeEvent event) {
explanation.setValue(String.format("You tried to navigate to a view ('%s') that does not exist.", event.getViewName()));
getUI().getNavigator().navigateTo(ErrorView.VIEW_NAME);
}
}

you can put 404/500 page under resources/error/ folder, Spring boot will redirect those page automatic when have error

I think SpringNavigator can solve your problem. While defining your Navigator, you can also define error View. See the example below
#SpringUI(path = "ui")
public class DemoUI extends com.vaadin.ui.UI {
#Override
protected void init(VaadinRequest vaadinRequest) {
SpringNavigator navigator = new SpringNavigator();
navigator.setErrorView(new ErrorView());
setNavigator(navigator);
}
}

Related

Unexpected Vaadin Session Expired and parallel spring boot applications

I am using Vaadin 8.9.4 and Spring boot 2.2.4.RELEASE. I have 2 spring boot applications, FirstApplication (server.port=8083) and SecondApplication (server.port=8084). Both the application have #SpringUI annotated class extending UI Class, as given below. The session for FirstApplication is expired as soon as I click on the button in the SecondApplication and vice versa. This happens only when I use two chorme tabs in parallel. If I use two different browsers, everything is working as expected.
Is this some kind of a bug as I expect no relation between the sessions of the two applications simply because they are running independently on different port.
Note: I am new to the spring boot and I am trying to build 2 micorservices communicating with each other via Rest API.
#SpringUI
#Theme("valo")
public class FirstApplicationUI extends UI {
private static final long serialVersionUID = 9197592298697220144L;
#Override
protected void init(final VaadinRequest request) {
final VerticalLayout layout = new VerticalLayout();
final Label label = new Label("First Application");
final Button button = new Button("click");
button.addClickListener(e -> Notification.show("First Application"));
layout.addComponents(label, button);
setContent(layout);
}
}
#SpringUI
#Theme("valo")
public class SecondApplicationUI extends UI {
private static final long serialVersionUID = 9059755286859361908L;
#Override
protected void init(final VaadinRequest request) {
final VerticalLayout layout = new VerticalLayout();
final Label label = new Label("Second Application");
final Button button = new Button("click");
button.addClickListener(e -> Notification.show("Second Application"));
layout.addComponents(label, button);
setContent(layout);
}
}
This is your two applications battling over the same cookie; both use the same name and your browser happily sends both backends the same cookie as the port is not considered.
Change the name in at least one of your applications; see server.servlet.session.cookie.name in https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html

Loading React Pages from Spring Controller

I have this code in my Spring Boot Application
#RestController
public class SampleController {
public SampleController() {
File file = new File("restresources");
System.out.println("File -> "+ file.getAbsolutePath());
}
#GetMapping(value = "/sample")
public String sampleText(HttpServletRequest request){
System.out.println("Sample Text");
return "Sample Text";
}
#RequestMapping()
public String acceptAny(){
return "ASDasdasdasdasd";
}
}
The acceptAny Method accepts any url hit from browser? But I have react files in my public folder too. So, when I hit localhost:8090/, instead of loading react index.html, it loads acceptAny() method.
How can I load react index.html instead of invoking this acceptAny() method or is there any method for loading React index.html from Controller itself?
Any help is highly appreciated!
first of all you have remove method acceptAny is hasn't any useful duties.
Than you should build your reactjs project and copy build files in resources/public or resources/static, than run project and open in browser localhost:8090/

Spring Boot displays images in some pages but not others

I'm facing issues with images in my Spring Boot + Thymeleaf application.
After reading a lot of fixes I'm able to show images in some of my app's pages but in other pages the images doesn't show.
I think that the number of paths in the request made is involved. It seems that requests to /myaction render pages showing images while requests to /myaction/other render pages not showing images.
In the fomer the successful request to obtain the images is:
http://localhost:8080/myapp/images/logo.png
In the latter the failed request to obtain the images is:
http://localhost:8080/myapp/myaction/images/logo.png
I append my configuration:
In my implementation of WebMvcConfigurerAdapter:
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/"
};
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
registry.addResourceHandler("/**")
.addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
}
Controller class:
#Controller
#RequestMapping(path="/myaction")
public class PagosController {
#GetMapping(path="")
public String show(Model model) {
//...
}
#GetMapping(path="/other")
public String show2(Model model) {
//...
}
}
In my html template I load the image this way:
<img th:src="#{images/logo.png}" />
logo.png
The file logo.png is located in src/main/resources/static/images
I have no clue why this is happening. Any idea on why images are getting requested at http://localhost:8080/myapp/myaction/images/logo.png? Thanks in advance.
According to your configuration, the images are available from the root /.
So you should be able to use <img th:src="#{/images/logo.png}" /> in any page.

How to edit Whitelabel Error Page in Spring

I want only to edit Spring White Label Page. I see a lot of tutorials for deleting this page, but I want only to change some text here, for example Error 404 - Go back! Any tutorial?
Thanks!
If you are using Spring Boot version 1.4+, custom error pages may be named according to their appropriate error code (e.g. 404.html) and placed in the /src/main/resources/public/error directory for static files or the /src/main/resources/templates/error directory if using a template engine. See Spring Boot and custom 404 error page for more details. You may alternatively follow the steps below to implement custom error pages.
Set the server.error.whitelabel.enabled property to false in your application.properties file. This will disable the error page and show an error page originating from the underlying application container.
server.error.whitelabel.enabled=false
Create custom error pages and save them in resources/templates directory. These pages may be created and named for different HTTP status codes, e.g.: error-404, error-500, etc.
Create a new Controller class that implements the ErrorController interface and override the getErrorPath method. Create a mapping for the path returned by the getErrorPath method. The method that handles this mapping can read the error code and return the appropriate custom error page.
#Controller
public class MyErrorController implements ErrorController {
private static final String ERROR_PATH = "/error";
#RequestMapping("/error")
public String handleError(HttpServletRequest request) {
Object status =
request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
if (status != null) {
Integer statusCode = Integer.valueOf(status.toString());
if(statusCode == HttpStatus.NOT_FOUND.value()) {
return "error-404";
}
else if(statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()){
return "error-500";
}
}
return "error";
}
#Override
public String getErrorPath() {
return ERROR_PATH ;
}
}

How to redirect to another page while keeping the original url?

In my Wicket 1.5 web application, I want to redirect to another bookmarkable page, whilst the URL of the originating page should remain.
#MountPath(value="page1")
public class WebPage1 extends WebPage {
public WebPage1() {
...
if (!isDisplayable()) {
setResponsePage(WebPage2.class);
// throw new RestartResponseException(Error404WebPage.class);
// throw new RestartResponseAtInterceptPageException(Error404WebPage.class);
}
}
private boolean isDisplayable() {
boolean flag = ...
...
return flag;
}
}
#MountPath(value="page2")
public class WebPage2 extends WebPage {
public WebPage2() {
}
public WebPage2(PageParameters params) {
}
}
Neither of the approaches with setResponsePage(..), throw new RestartResponseException(..) or throw new RestartResponseAtInterceptPageException(..) leaves the URL unchanged.
All three methods redirect to Page2 and change the displayed URL in the browser's address bar.
You should supply the RestartResponseException with RedirectPolicy.NEVER_REDIRECT. I.e.
throw new RestartResponseException(new PageProvider(Error404Page.class), RedirectPolicy.NEVER_REDIRECT);

Resources