Serving single html page with Spring Boot - spring

I'm developing a simple Spring Boot app that uses Spring Boot Web. I placed my index.html into the templates subdirectory of the resources directory as per the Spring Boot docs. I've defined inside the application.properties file:
spring.mvc.view.prefix=classpath:/templates/
spring.mvc.view.suffix=.html
My WebMvcConfigurerAdapter looks like this:
#Configuration
#EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter{
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
super.addViewControllers(registry);
}
}
My problem is I cant seem to serve index.html at http://localhost:8080.
I get the following error:
"javax.servlet.ServletException: Could not resolve view with name 'index' in servlet with name 'dispatcherServlet'".
How can I fix this without the use of a controller?
Kind regards,
Dave

In order to serve static content from Spring Boot application you just need to place them in src/main/resources/static - no other configuration needed.
put index.html to src/main/resources/static/index.html
delete properties from application.properties
delete addViewControllers method and #EnableWebMvc annotation - you can actually delete whole class
access index by going to http://localhost:8080/

Related

Spring Boot now displaying the index.jsp page

I am a newbie with Spring boot.
I am trying to return my index page in the controller but I am not able to. Its just returning the word index in the output. Here's my code:
Controller class:
#RestController
public class IMEIController {
#Autowired
private IValidateIMEI iValidateIMEI;
#GetMapping("/")
public String home() {
return "index";
}
}
This is my structure
Application.properties file
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
Output:
The issue is with #RestController. Use #Controller annotation instead.
#RestController is use to create RESTful web services.
Here some articles if you need to know more.
The Spring #Controller and #RestController Annotations
Spring boot does not work directly with JSPs. You will have to add tomcat Jasper embedded dependency to get it working.
https://www.baeldung.com/spring-boot-jsp
Also change #RestController to #controller.
And if you want to develop Rest + JSP in the same controller then use #Controller on class level and add #ResponseBody for the rest API methods which will return response in the body instead of returning a JSP page.
Just add your jsp folder in build path of the project:-
Right click on the project > Build Path > Configure Build path > Deployment Assembly > Add(right hand side) > Folder > Add your views folder > Apply and Close

How to separate my spring boot's static resource from build

I am new in spring boot,
I have a multimodule spring boot maven project
core,
admin,
rest
I purchase an admin template which has more than 25000 static files(css and js).
my admin project is like
admin
-src
-main
-resources
-static
But my problem is to build admin it is taking too much time.
How can I separate these resource files from the build process?
You can set the property spring.resources.staticLocations in your application.propertes to configure a custom resource directory. IE spring.resources.staticLocations=file:/foo/bar/static/.
You can also configure it programmatically by extending WebMvcConfigurerAdapter
#Configuration
public class Config extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("file:/foo/bar/");
}
}

What Is the Correct Way To Use AbstractReactiveWebInitializer

I've got a Spring WebFlux application running successfully as a standalone spring boot application.
I am attempting to run the same application in a Tomcat container, and following the documentation, I've created a class that extends AbstractReactiveWebInitializer. The class requires that I implement a method getConfigClasses that would return classes normally annotated with #Configuration. If the working spring boot app started with a class called ApplicationInitializer, then the resulting implementations would look like this:
#SpringBootApplication(scanBasePackages = "my.pkg")
#EnableDiscoveryClient
#EnableCaching
public class ApplicationInitializer {
public static void main(String... args) {
SpringApplication.run(ApplicationInitializer.class, args);
}
}
and
public class ServletInitializer extends AbstractReactiveWebInitializer {
#Override
protected Class<?>[] getConfigClasses() {
return new Class[] {ApplicationInitializer.class};
}
}
When deployed, the only thing that starts is ApplicationInitializer, none of the autoconfigured Spring Boot classes (Cloud Config, DataSource, etc) ever kick off.
The documenation states this is the class I need to implement, I just expected the remainder of the spring environment to "just work".
How should I be using this class to deploy a Reactive WebFlux Spring Boot application to a Tomcat container ?
Edit:
After some additional research, I've narrowed it down to likely just Cloud Config. During bean post processing on startup, the ConfigurationPropertiesBindingPostProcessor should be enriched with additional property sources (from cloud config), but it appears to be the default Spring properties instead, with no additional sources.
The misisng properties is causing downstream beans to fail.
Spring Boot does not support WAR packaging for Spring WebFlux applications.
The documentation you're referring to is the Spring Framework doc; Spring Framework does support that use case, but without Spring Boot.
you can extend SpringBootServletInitializer, add add reactive servlet on onStartup method

Spring Boot Actuator requires #EnableWebMvc but that turns off other features

I got confused trying to integrate Actuator to a project using Spring Boot 1.5.11's static resource serving feature:
Spring Boot Actuator does not work without #EnableWebMvc, HTTP 406 is returned because the right Http media converter is not installed.
But if I add #EnableWebMvc, it turns off serving resources from the static folder and other Spring Boot features.
Is there a way to initialize Actuator without losing Boot features?
Found a bug in Spring Boot 2.0, it seems it was there in Boot 1.5 as well. Workaround: setting favorPathExtension to false turns off buggy code in ServletPathExtensionContentNegotiationStrategy and Actuator endpoints start working.
#Configuration
public class WebConfig implements WebMvcConfigurer {
#Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false);
}
}

How to register resource with javax.ws.rs.core.Applicaiton in spring-boot-starter-jersey powered REST application

In my spring boot application (powered by spring-boot-starter-jersey):
I can easily make a resource config (jersey way but not jaxrs way) like this:
#Configuration
#ApplicationPath("/sample")
public class SampleResourceConfig extends ResourceConfig {
And I just want to try with javax.ws.rs.core.Applicaiton:
#Configuration
#ApplicationPath("/sample")
public class SampleResourceConfig extends javax.ws.rs.core.Applicaiton{
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(SampleResource.class);
return classes;
}
But no lucky, it does not work.
Did I miss something?
Thanks
Leon
It won't work as the Spring Boot auto-configuration is made specifically to look for a bean of type ResourceConfig, not Application. If you want to use Application, you can't use the auto-configuration. You'll need to just create the JAX-RS servlet yourself and register it with a Spring Boot ServletRegistratiobBean, similar to what you'll see in the source code I linked to.

Resources