In my Maven project with Spring Boot I have a following folder with a static content(Html, CSS, JavaScript files):
/src/main/webapp/docs/
this is my application properties:
server.port: 8080
server.contextPath: /admin-api
When a user access a following url: http://localhost:8080/admin-api/docs I want to show him a static content from /src/main/webapp/docs/ folder.
I'm trying to configure ResourceHandler in my WebMvcConfigurerAdapter but it doesn't work:
#EnableWebMvc
#Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
String[] staticResourceMappingPath = { "/docs/", "classpath:/docs/" };
registry.addResourceHandler("/docs").addResourceLocations(staticResourceMappingPath);
}
...
}
http://localhost:8080/admin-api/docs returns 404 Not Found.
What am I doing wrong and how to properly configure it ?
Your addResourceHandlers method should be like below.
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/docs/**").addResourceLocations("/docs/");
}
Now if you have a static page say index.html in docs folder then you can access it using http://localhost:8080/admin-api/docs/index.html
Related
I'm usign spring boot.
When I deploy my app under a AWS balancer, the app is calling some resources from myDir/myAppName. But the style resources are called from root, like myDir/.
E.g. myDir/myAppName/index.html is accesible for loadBalancer. But myDir/myStilesheet.css isn't accesible.
How can I set that resources (css, etc) under myDir/myAppName?
Thanks in advance!
You can implement the WebMvcConfigurer interface and override the void addResourceHandlers(final ResourceHandlerRegistry registry) method.
#Configuration
public class MvcConfig implements WebMvcConfigurer {
#Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("myDir/myAppName/myStilesheet.css")
.addResourceLocations("classpath:/resources/static/css/myStilesheet.css");
}}
I'm using Spring Boot 2.5.2. I configured all things (not security, it is auto configured and need to convert all configuration from XML to Java. Authentication is working via LDAP) and working correctly except one thing i.e. mapping to / -> redirect:/main.
Note: This is a migration from Spring to Spring Boot with .jsp as view.
Below is the mapping in XML:
<mvc:view-controller path="/" view-name="redirect:/main" />
Above is working correctly in Spring but when converted to Spring Boot using Java code as below then this specific case is not working:
#Configuration
public class WebMvcConfig implements WebMvcConfigurer {
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("redirect:/main");
}
}
Note: If I set addViewController something else like /documentation then it is redirecting successfully but not with /.
you can use addRedirectViewController
#Configuration
public class WebConfiguration implements WebMvcConfigurer {
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/", "redirect:/main");
}
}
After trying many things, finally got the solution. I need to setOrder() to highest precedence to work. But I still don't know why I need to do this for this specific case. Below is the code:
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("redirect:/main");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
When using addRedirectViewController() one have to set url name properly:
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/", "/yourURL");
}
but NOT "redirect:/yourURL"
I need to serve static index.html page from "/" GET request in spring-web.
My module is included into bigger one, packaged as WAR and deployed to tomcat.
I've tried
#Configuration
#EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
System.out.println("WebMvcConfigurer - addResourceHandlers() function get loaded...");
registry.addResourceHandler("/WEB-INF/classes/index.html")
.addResourceLocations("/WEB-INF/classes/index.html");
}
}
and placed index.html in resources folder. Still 404. Can anybody help me out with understanding what have I done wrong?
The pattern of the ResourceHandler should reflect the query path, in your case /index.html
Resources inside a jar/war /WEB-INF/classes/ can be accessed via classpath:, in your case classpath:/index.html
So your config should have been like this
#Configuration
#EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/index.html")
.addResourceLocations("classpath:/index.html");
}
}
You can simplify if you have more resources with
registry.addResourceHandler("/*.html")
.addResourceLocations("classpath:/");
I have the following in configuration for my Spring Boot project that serves static files from the local filesystem:
#Configuration
public class StaticResourceConfiguration extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{
System.out.println("adding resource handler");
registry.addResourceHandler("/myfiles/**").addResourceLocations("file:///C:/Users/Pepria/Downloads/static_files/");
}
}
Above config works fine but I want to change the resource location dynamically at runtime. As I understand, above code runs before any of my logic executes. How can I go about doing this ?
You can add a ResourceHandler with your desired path like this:
registry.addResourceHandler("/myfiles/**").addResourceLocations("file:" + Strings.filePath);
You can set Strings.filePath in you application at any time.
public class Strings {
public static String filePath;
//or maybe setters getters
}
Hi I am trying to include a front-end in the /resources folder of my application.
My directory structure is as follows:
src
main
java // backend located here
webapp
resource //my html/css/js
I am using oauth for security of the backend:
MethodSecurityConfig.java
#Configuration
#EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
#Override
protected MethodSecurityExpressionHandler createExpressionHandler() {
return new OAuth2MethodSecurityExpressionHandler();
}
}
ResourceServerConfigurerAdapter.java
#Configuration
#EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
#Override
public void configure(final HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/resources/**").permitAll().and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
.and().authorizeRequests().anyRequest().authenticated();
}
}
ResourceWebConfig.java
#Configuration
#EnableWebMvc
#ComponentScan({"eu.emif.resource.web"})
public class ResourceWebConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/resources/**")
.addResourceLocations("(/resources/");
}
}
Whenever I try to go to http://localhost:8082/style.css
Or http://localhost:8082/resources/style.css
I am getting
<oauth>
<error_description>
Full authentication is required to access this resource
</error_description>
<error>unauthorized</error>
</oauth>
Help would be much appreciated.
Change you directory structure to as follows:
src
main
java // backend located here
resources(By default these files are not added in deployment assembly and mostly used for loading java resources like xml file,properies file etc.)
webapp
resource(create new folder here and put your frontend(html/js/css) files here)