java - Spring Boot Content Negotiating ViewResolver -
i'm working on spring boot project lists users in table, can create .pdf itext.
when try hit unknown endpoint such as:
http://localhost:8080/blablabla , browser tries download blank page(blablabla) instead of showing whitelabel error page(404).
http://localhost:8080/admin/protected_path , browser tries download blank page (protected_path) instead of showing whitelabel error page(403).
http://localhost:8080/login (mapped loginpage.jsp) opens correctly(without trying download blank page)
http://localhost:8080/downloadpdf opens correctly, downloads desired pdf our local machine.
how should configure webconfig display white label error pages correctly(without trying download endpoint) ?
import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.http.mediatype; import org.springframework.web.accept.contentnegotiationmanager; import org.springframework.web.servlet.viewresolver; import org.springframework.web.servlet.config.annotation.contentnegotiationconfigurer; import org.springframework.web.servlet.config.annotation.defaultservlethandlerconfigurer; import org.springframework.web.servlet.config.annotation.webmvcconfigureradapter; import org.springframework.web.servlet.view.contentnegotiatingviewresolver; import org.springframework.web.servlet.view.internalresourceviewresolver; import org.springframework.web.servlet.view.jstlview; import java.util.arraylist; import java.util.list; /** * @author skylifee7 on 25/07/2017. */ @configuration public class webconfig extends webmvcconfigureradapter { @override public void configurecontentnegotiation(contentnegotiationconfigurer configurer) { configurer .favorpathextention(true) .ignoreacceptheader(true) .defaultcontenttype(mediatype.text_html); //does not work. } @override public void configuredefaultservlethandling( defaultservlethandlerconfigurer configurer) { configurer.enable(); } /* * configure contentnegotiatingviewresolver */ @bean public viewresolver contentnegotiatingviewresolver(contentnegotiationmanager manager) { contentnegotiatingviewresolver resolver = new contentnegotiatingviewresolver(); resolver.setcontentnegotiationmanager(manager); // define possible view resolvers list<viewresolver> resolvers = new arraylist<>(); resolvers.add(pdfviewresolver()); resolvers.add(internalresourceviewresolver()); resolver.setviewresolvers(resolvers); return resolver; } /* * configure view resolver provide pdf output using itext library * generate pdf output object content */ @bean public viewresolver pdfviewresolver() { return new pdfviewresolver(); } @bean public viewresolver internalresourceviewresolver() { internalresourceviewresolver bean = new internalresourceviewresolver(); bean.setviewclass(jstlview.class); bean.setprefix("/"); bean.setsuffix(".jsp"); return bean; } }
update: have done trick workaround weird issue. have created custom error pages .jsp (actually pure html) files , wrote extension spring's errorviewresolver class.
@controlleradvice public class myerrorviewresolver implements errorviewresolver { @override public modelandview resolveerrorview(httpservletrequest request, httpstatus status, map<string, object> model) { // use request or status optionally return modelandview modelandview modelandview = new modelandview(); if (status.is4xxclienterror()) { if (status.value() == 404) { modelandview.setviewname("error404"); } } return modelandview; } }
Comments
Post a Comment