I've been trying to solve this issue and I can't find the answer.
I've been having to use src/main/resources/public/whatever... in order to access static resources from class files. I was under the impression that if you use /static /public /resources... that Spring Boot already has those paths configured.
I'm lost at this point and not sure how I can access these files without having to prepend src/main/resources/public.
EDIT
Based on the suggestions regarding the static content, I created a private directory to protect files etc... that I don't want publicly available. And left css, images, etc... in the /public folder.
Thank you all for the suggestions and recommendations, I appreciate it!
After more searching I ran across an example that used the ClassLoader. I've only been using Spring for a short time and not aware of all the tools that are available, there might be a better way than this; if so please tell let me know.
ClassLoader classLoader = getClass().getClassLoader()
new File(classLoader.getResource('public/file-name-or-directory').getFile())
So far it looks like it's working, I need to do some further testing and will post back if any issues.
The directories you list, /static, /public and /resources, are for static content that will be served up by a web-application. Things like javascript files and images that will always be the same no matter who requests them and when, as opposed to JSPs for example that need to be processed on the server per request.
If you need to access a configured property in one of your Java classes (it must be a Spring bean), then you can put it in your application.properties file, and have Spring inject it using the #Value annotation.
Example code from Spring docs:
import org.springframework.stereotype.*
import org.springframework.beans.factory.annotation.*
#Component
public class MyBean {
#Value("${name}")
private String name;
// ...
}
Spring Boot expect you to use for content served directly
src/main/resources/static
and to use for templating
src/main/resources/templates
Related
I try with following process to serve static content but have no luck.
I have written the below code in my application.properties:
spring.resources.static-locations=classpath:/public/
where public is the directory of my static and uploaded content, like user profile and others.
but it's giving me a 404 error when I access the file like:
http://localhost:8082/public/uploads/file/a.png
I have also tried with:
http://localhost:8082/uploads/file/a.png
but it does not working either.
Unless you don't have other configuration parameters set, spring boot should serve static resources under /
So if your static resources are under a folder called public/file you'll find that at the url localhost:8082/file/a.png
If you have spring.mvc.static-path-pattern parameter set spring boot should serve static resources at that path
In the case of spring.mvc.static-path-pattern = files (for example) it will serve them at http://localhost:8082/files/file/a.png
Here's a good article explaining all :
https://www.baeldung.com/spring-mvc-static-resources
If you have server.servlet.context-path (or server.servlet.contextPath, it's the same) set in your application.properties, you need to have this part in the URL, for example:
server.servlet.context-path=/foo
then you should visit http://localhost:8082/foo/uploads/file/a.png
I am developing a java web service for the first time. I am using Spring Framework to do it. I need to serve user images. I will get Base64 String from the client and write it as an image and add its reachable url to database than whenever a client wants to see this image I will just send the image url and by this way a client will be able to use the image with less effort.
But to do this I need to reach these images in their folder via browser.
I don't know how to reach a folder and its files on java based web services.I hope someone helps.
If there is a way you think better to do. Please feel free to write.
Spring Framework 4.1 has improved support for resource handling. It allows you to declaratively configure from where to serve resources for a certain request path, and that's what you're probably interested in.
In your code, making file system resources accessible from the web mostly boils down to overriding the addResourceHandlers() method of your WebMvcConfigurerAdapter based configuration class. The Spring Framework documentation shows the following example:
For example, to serve resource requests with a URL pattern of /resources/** from a public-resources directory within the web application root you would use:
#Configuration
#EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/public-resources/");
}
}
Substitute /public-resources/ with your filesystem path prefixed with file://, e.g. file://user-uploads/.
I suggest taking a look at the relevant blog post and the accompanying example project. The demo project sports a good example of how to configure resource handling for a complex scenario.
I need to load a file from within a directory in the root of a WAR using Spring
This is the directory structure
rootOfWar
--static-dir
---- my-file.css
--WEB-INF
---- classes
.....
It is a normal WAR.
In a Spring #RestController I need to be able to read and write to my-file.css file. What is the best way to get the File, ServletContextResource or?
More Details
- The location of the file is out of my control I cannot move the file.
- The jee container is Tomcat.
- The Spring version is current 4.1.6
- The Spring environment is not using XML only annotations with WebApplicationInitializer, WebMvcConfigurerAdapter and an annotation configuration class.
Is there another way to do this like specify a file as a resource in the configuration so that it is loaded by the frame work and accessible within the application?
Basically I have JEE knowledge but my Spring knowledge on best practices concerning read/write are lacking.
If you need to modify a file you should not make it part of the WAR. Store it somewhere outside the web package and reference it there. Everything else will lead to problems, especially when you deploy to Websphere which is often run in a restricted environment where writes are rejected.
But I consider overwriting files in the web path bad design, because you are likely to run into caching issues. Better write a servlet that generates the CSS you need. If you would be able to name the content that should overwrite your css file, you are also able to render this dynamically.
Something like this may be already sufficient:
#RequestMapping(value = "/my.css", produces = "text/css;charset=UTF-8")
public String showCss() {
return "<here goes your css>";
}
(This is written from my memory and not tested).
The good thing is that you can modify the css any time you want and even set caching information as needed.
I have followed the Spring example Serving Mobile Web Content with Spring MVC and got it working.
Now I would like to replace greeting.html with a simple static page (and replace Thymeleaf with a simpler view handler).
What is the easiest approach in this particular case?
I am new to Java, the probable solutions in web use web.xml, whereas this example does not use web.xml and it does not seem to be the recommended approach in Spring Boot documentation either. So, use #EnableAutoConfiguration with some overriding? How?
All you have to do is put the static HTML file in a place where Spring Boot will automatically look for static resources. This part of the documentation provides all the details.
So for example you can place your greeting.html under /src/main/resources/static/ and you will be able to access it at http://localhost:8080/greeting.html (that's if you have not configured a different port of the root path of the servlet context)
I am porting a web app to tomcat and I have a problem with static resources.
I have a jar that contains the web resources (css, img, js, ... files). These files are packaged in a path resembling this:
data.jar
com/bizname/application/web/css
com/bizname/application/web/img
com/bizname/application/web/js
The jar is inside the web application. I want to be able to map the resources to public acces. For example if I acces :
http://myserver:8080/mywebapp/css/style.css
I want the resource com/bizname/application/web/css/style.css to be delivered.
I cant find how to set up this in the web.xml-file of Tomcat.
In Jetty embedded, I was able to use handles to achieve this.
final URL url_css = Servidor.class.getClassLoader().getResource("com/bizname/application/web/css");
HandlerList handlers = new HandlerList();
handlers.addHandler(new WebAppContext(url_css.toExternalForm(),"/css"));
// ... adding handler to context
Maybe jawr can help you.
If not, you can implement a Servlet that does it for you (reading the file and writing it to the response). If you use something like Resteasy it will be easier because you don't need to write the IO stuff.
I am not quite sure this is right, considering I have only worked with Tomcat from Eclipse, but I'll give it a shot anyway.
I am assuming you are using Java Servlets in this app? If that is the case, then you need to specify the URL-patterns in web.xml. Here is a great example of a web.xml file from apache.org which explains the aspects of the file:
https://tomcat.apache.org/tomcat-5.5-doc/appdev/web.xml.txt
Hope this helps you!