I'm doing a GUI in Angular which is calling a REST-api written in SparkJava (JavaSE). Everything will be running on the same machine.
The GUI was originally written in swing but I'm rewriting it in Angular to get a more modern touch.
Problem:
When the application starts everything seems to be loaded as expected. When I open the web page in a browser I get an INFO-message in the Java-console stating spark.http.matching.MatcherFilter - The requested route [/sv/assets/images/image1.png] has not been mapped in Spark for Accept: [image/webp,*/*], among other similar messages.
The Angular files are located in src/main/resources/public. There are a few subfolders with content, see screen shot.
The backend is served as it should, but the frontend files can't be loaded properly in the browser. HTML/JS/CSS seems to be loaded correct, but images and other files in assets folder can't be loaded.
Can this be solved? Or do I have to put all files in the root directory? I found nowhere all static files in SparkJava have to be located in the same folder (meaning, no subfolders).
My setup:
Spark.port(4567);
Spark.staticFiles.location("/public");
// simple rest API to serve some data
Spark.get("/labels/getall", "application/json", new Route() {
#Override
public Object handle(Request rqst, spark.Response rspns) throws Exception {
Labels l = LabelReader.getLabels();
Gson gson = new Gson();
return gson.toJson(l);
}
});
File tree:
Java console messages
Browser output
Related
I work on spring boot application. I'm trying to serve static content with spring.
want to serve a resource stored in the /c:/frontend/files/ directory whenever a request comes in for the URL matching the pattern: /file/**:
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/file/**")
.addResourceLocations("file:///C:/frontend/files/" );
}
but when i try to access to this resource using this url: http://localhost:9999/file/app.min.js
I have this problem
There was an unexpected error (type=Not Acceptable, status=406).
Could not find acceptable representation
I resolved the problem. it's related to "spring-cloud-config-server". I just delete this config: org.springframework.cloud spring-cloud-config-server
It sounds like your project's folder structure is wrong.
Code should go under src/main/java and resources (like your javascript) should go under src/main/resources. You have a few different options where you can actually serve the files from. This post on the spring.io blog has the following to say:
Spring Boot will automatically add static web resources located within any of the following directories:
/META-INF/resources/
/resources/
/static/
/public/
Another option you also have is using webjars.
Personally, I've found it easiest to put those kind of files under src/main/resources/public. It always works without any issues for me. The interesting thing is you can put a folder named /public anywhere in your project and spring-boot will serve files out of it. You have to be really careful that it's under src/main/resources/public though if you're using a build tool like maven, as when you come to build your .jar the files won't be in the right place otherwise.
I have a Dropwizard/AngularJS website. I have my assets served from an AssetsBundle on the root directory:
public void initialize(Bootstrap<WebsiteConfiguration> bootstrap) {
bootstrap.addBundle(new AssetsBundle("/assets/", "/", "index.html"));
...
}
And I want to serve my REST endpoints on /services:
public void run(WebsiteConfiguration configuration, Environment environment) throws Exception {
environment.jersey().setUrlPattern("/service");
...
}
Based on documentation and answers I've seen elsewhere, this seems like it should work. However, I just tried it, and everything on /service is returning a 404. When I dug into the app through a debugger, it appears that it's because the requests are going through the AssetServlet, not through Jersey.
Am I missing something? Is there a good way to serve my front-end on the root directory and the REST services on a sub-path?
I also struggled with this issue some time ago. What helped me was adding these lines to the app.yml config file:
server:
type: simple
rootPath: '/rest/*'
applicationContextPath: /
I have a Dropwizard webserver with a rest api which also serves some static content like html, css, javascript and jpg images. Unfortunately, when I change the html or add another image, the server always needs to be restarted to turn changes into effect.
As I thought that it might be a problem of caching, I explored bazaarvoice's Configurable Assets Bundle.
That's what I added to the configuration class:
#Valid
#NotNull
#JsonProperty
private final AssetsConfiguration assets = new AssetsConfiguration();
And in the main class
#Override
public void initialize(Bootstrap<MyConfiguration> bootstrap) {
// ...
CacheBuilderSpec cacheBuilderSpec = CacheBuilderSpec.disableCaching();
bootstrap.addBundle(new ConfiguredAssetsBundle("/html", cacheBuilderSpec, "/", "index.html", "Static assets"));
}
#Override
public void run(MyConfiguration config, Environment env) {
env.jersey().setUrlPattern("/api/*");
// ...
}
No changes in the yaml configuration.
The static files are located in src/main/resources/html.
How can caching be disabled such that Dropwizard shows changes instantly?
And second question, how can I make Dropwizard follow symbolic links from the assets directory?
Update
I found this in the ConfiguredAssetsBundle source:
// Let the cache spec from the configuration override the one specified in the code
CacheBuilderSpec spec = (config.getCacheSpec() != null)
? CacheBuilderSpec.parse(config.getCacheSpec())
: cacheBuilderSpec;
This certainly overrides the cache builder spec which was set in the code with the configuration from the yaml file. After append
assets:
cacheSpec: maximumSize=0
to the configuration, the debugger shows that maximum size is now 0. However, behaviour did not change.
Static content doesn't change not because you need to restart, but because the running server actually serves files under the target directory. Changing the files in this directory only confuses things (so it's not really a solution), but change a few lines and wait a second to verify that the server now serves the modified file with no need to restart.
As a solution, I prefer opening the dropwizard project as a maven project in eclipse and run the project with mvn exec:java on a terminal, using the exec-maven-plugin. Eclipse updates the target directory as files change, but it takes a few seconds, depending on the size of the project.
I'm working on a Spring WebMVC project that generates HTML output by using FreeMarker templates. A site controller returns a view name and a data model which is then fed into the view template.
Sample controller method (in /src/main/java/com/mycompany/controller.java):
#RequestMapping("/example")
public ModelAndView handleRequest() {
ModelAndView mv = new ModelAndView("my_tempate");
mv.addObject("my_data", "my_value");
return mv;
}
Sample template for this (which resides in /src/main/webapp/WEB-INF/views/my_template.ftl):
<title>Sample Template</title>
<p>I'm a sample template, I output: ${my_data}</p>
I package a .war file and deploy it to tomcat with mvn tomcat7:redeploy. This process takes about 4-5 seconds for my very small code base. For changes to the java code I'm fine with this compilation time. However for developing HTML templates it is a very unsatisfying workflow having to redeploy my .war file with every change.
Is it possible to set up Spring to not read template files from the .war file package but rather from the local file system while in development mode? So I could point in the source to an absolute path like /home/user/my_project/my_template.ftl that is parsed on every site request.
I'm developing an applet application which is compiled on Java 1.5 (compatible with 1.5+).
It contains some resource property files that are bundled together in the same jar, which lies parallel to the Java package.
Whenever I access that resource file through applet it makes a request to server from where the applet is been downloaded. After that it reads the files from the jar and works as it used to be but I don't want it to make server request for those files.
This is how my java code access the resource file.
ResourceBundle messages = ResourceBundle.getBundle("resources/properties/Messages", locale);
I tried access in both ways:
ResourceBundle messages = ResourceBundle.getBundle("resources.properties.Messages", locale);
Both it had the same behaviour.
Note: Those resources are not available as loose resources in my web app.
I got these details from server logs.. I was analyzing my server log for 404 and 500 responses ..
The 404 (not found) & 500 (server error) messages can be expected because the plug-in is trying to check if the cached resources are up to date. To do that, it needs to check the time last updated on the server version of the resource.
The complicating factor is that the resource can be expected to be in a Jar mentioned in the archive attribute of the applet or it can be a 'loose file' on the same path as the codebase specified. So if a resource is in the following path of a Jar:
/resources/properties/Messages_en_US.properties
The JVM will also check
${codebase}/resources/properties/Messages_en_US.properties
as well as each Jar.
To fix them, see the codebase_lookup parameter. This use-case needs:
<param name='codebase_lookup' value='false' >
That tells the JVM that there are no resources stored as loose files on the class-path, and only Jars are to be searched. It should stop the 404/500 messages being reported as often (for newer JREs that understand that parameter).
I don't know much about the inner details of the Java Plug-in's caching of applets, but if your applet is using a .jnlp descriptor, I would try adding download="eager" to the descriptor's <jar> element.
You can also try defining your ResourceBundles as classes rather than .properties files. For instance:
package resources.properties;
import java.util.ListResourceBundle;
public class Messages
extends ListResourceBundle {
protected Object[][] getContents() {
return new Object[][] {
{"entry1", "Some message text"},
{"entry2", "A different message"},
// etc.
};
}
}
Just like properties files, you can define them for as many locales as you wish:
package resources.properties;
import java.util.ListResourceBundle;
public class Messages_es
extends ListResourceBundle {
protected Object[][] getContents() {
return new Object[][] {
{"entry1", "Some message text in Spanish"},
{"entry2", "A different message in Spanish"},
// etc.
};
}
}
If you define ResourceBundle subclasses, it's a good idea to remove the corresponding .properties files.