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:/");
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 trying to implement handling requests for static resourses in Spring + Angular project. Angular app is running on http://localhost:4200 when Spring app is running on http://localhost:8080.
So when I need to get an image, angular sends a request which looks like this http://localhost:4200/screenShots/image-name.jpg. To handle these kind of requests I've created WebConfig class:
#Configuration
public class WebConfig implements WebMvcConfigurer {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
String path = "file:///" + *path to folder with images on my PC*
registry.addResourceHandler("/screenShots/**")
.addResourceLocations(path);
}
}
And it doesn't work, I get 404 (Not Found) error. I've experimented with addind and removing #EnableWebMvc and #CrossOrigin annotations, tried to add addCorsMappings() method in my class like that, nothing helped.
#Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/screenShots/**")
.allowedOrigins("http://localhost:4200");
}
However when I put http://localhost:8080/screenShots/image-name.jpg (changed :4200 to :8080) string in Google address bar, it returns me my image as intended, so I wonder is there a way to add some kind of #CrossOrigin annotation alternative for resourse handlers cuz I haven't found any solution in the Internet yet. Thanks!
Github repo for those, who are curious (only gameImagesRelease branch contains current piece of code): https://github.com/Vladyslav-Bahlai/OktenGames/tree/gameImagesRelease
For example, I will show my MVC-configs. I had the same problem. Spring did not see HTML files. I draw your attention to the templates folder.
#Configuration
public class MvcConfig implements WebMvcConfigurer {
/**
* Making our pages visible
*
* #param registry
*/
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("login");
registry.addViewController("/coffees").setViewName("coffees");
registry.addViewController("/teas").setViewName("teas");
registry.addViewController("/contacts").setViewName("contacts");
registry.addViewController("/home").setViewName("home");
registry.addViewController("/about").setViewName("about");
registry.addViewController("/registration").setViewName("registration");
registry.addViewController("/card").setViewName("card");
}
/**
* This method helps Spring discover file paths
*
* #param registry
*/
#Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/templates/");
}
}
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
}
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
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)