Issue
I have a spring boot project with thymeleaft rest + soap.
I have a page that does:
Show the front end
+ Rest Requests
+ Soap requests.
The problem originates from "Soap" when I have to create the endpoint.
When I add this endpoint to the configuration:
@Bean
public ServletRegistrationBean<CXFServlet> dispatcherServlet() {
return new ServletRegistrationBean<CXFServlet>(new CXFServlet(), "/soap-api/*");
}
@Bean
@Primary
public DispatcherServletPath dispatcherServletPathProvider() {
return () -> "";
}
I am using a soap with apache cfx. The cfx configuration I use for my project is here
I have found a slightly unexpected error.
If I run this config inside my config I get all these problems:
- config inside my config
I get an error of the type:
Error creating bean with name 'resourceHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set
I tried with @EnableWebMvc but I was getting a problem from:
Error creating bean with name 'resourceHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]
At this point I have tried: add the path for the thymeleaft
ClassLoaderTemplateResolver secondaryTemplateResolver = new ClassLoaderTemplateResolver();
But it does not work. I have also tried with basic mapping:
UrlBasedViewResolver resolve = new UrlBasedViewResolver(); resolve.setPrefix("templates/"); resolve.setSuffix(".html");
But it doesn't work either.
- config outside my config
The strange thing is that if I set that configuration aside in another java file, no problem arises. But when I access the page I get an error:
ERROR 10788 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[...] : Custom error page [/error] could not be dispatched correctly
So I thought mapping the traditional way like spring, in another java configuration does:
@Bean public UrlBasedViewResolver viewResolver() { UrlBasedViewResolver resolver = new UrlBasedViewResolver(); //resolver.setPrefix("/WEB-INF/view/"); //resolver.setSuffix(".jsp"); // THYMELEAFT resolver.setPrefix("templates/"); resolver.setSuffix(".html"); resolver.setViewClass(JstlView.class); return resolver; } // RESOURCES @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**") .addResourceLocations("/", "/resources/") .setCachePeriod(3600) .resourceChain(true) .addResolver(new PathResourceResolver()); } @Bean public ResourceBundleThemeSource themeSource() { ResourceBundleThemeSource themeSource = new ResourceBundleThemeSource(); themeSource.setDefaultEncoding("UTF-8"); themeSource.setBasenamePrefix("themes."); return themeSource; }
It doesn't work either. I am currently at this point. Is there any solution for problem 2?
At this point I can successfully load the SOAP, but the other pages in the browser show up as 404. and in the console I get: Custom error page [/error] could not be dispatched correctly
From what I understand it makes a mess with the mapped routes and I understand is that when I register a servlet "the previous or current" is lost and it is not mapped with the ServletRegistrationBean..
Is there any way to fix this? Do I have to map all the routes by hand with the ClassLoaderTemplateResolver or the UrlBasedViewResolver? I've tried it, but it throws the same error. In case of changing while advancing "a different error comes out every time". That's why I'm looking for a way to directly solve the problem.
Note: I am using the apache.cxf plugin
Solution
I have found a way to make it work by declaring it in another way. I think every day I'm understanding less how Spring/Spring Boot works. Is it time to return to JEE?
1 - It seems that if you leave the dispatcher empty (obviously it will fail with an empty arrow function) but Spring requires you to create a dispatcher when you declare ServletRegistrationBean. Thinking that: "that dispatcher "could" get the route automatically with an empty arrow function". But it doesn't.
// @Bean
// public ServletRegistrationBean<CXFServlet> dispatcherServlet() {
// return new ServletRegistrationBean<CXFServlet>(new CXFServlet(), "/soap-api/*");
// }
// @Bean
// @Primary
// public DispatcherServletPath dispatcherServletPathProvider() {
// return () -> "";
// }
2 - If I declare it a little differently, it is able to correctly detect the mappings
@Bean
public ServletRegistrationBean servletRegistrationBean() {
CXFServlet cxfServlet = new CXFServlet();
ServletRegistrationBean servletRegistration =
//new ServletRegistrationBean(cxfServlet, ServiceConstants.URL_PATH_SERVICES + "/*");
new ServletRegistrationBean(cxfServlet, "/soap-api/*");
return servletRegistration;
}
Problem source: here
Answered By - Naslo Flaquez
Answer Checked By - Willingham (JavaFixing Volunteer)