Spring not able to find specific web pages from Vite Build? - java

ResourceHttpRequestHandler from Spring Boot is unable to find specific web pages in sub-directories from a "Vite Build" dist/ package of my frontend.
Here's the stack trace, scrubbed for anonymity:
GET "/path/to-my/webPage", parameters={}
DEBUG 1 --- [nio-8443-exec-5] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
DEBUG 1 --- [nio-8443-exec-5] o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
DEBUG 1 --- [nio-8443-exec-5] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND
Navigating to this page works fine when navigating directly from home, but returns a 404 on page refresh, and it cannot be navigated to directly. Any help?
I've already tried numerous things, detailed in another post that isn't getting any traction: Spring Boot not serving Vite dist/prod build assets, only index.html
I figured I'd post the stack trace here. Getting desperate.

Related

How to reference a set of common properties shared across many Java (Spring/Boot) batches and is this even a good idea?

In our Spring Boot based Java project, I am attempting to find the best way to solve this problem.
Right now, we have a directory full of Java based batch applications that follow the conventional Maven directory structure of batch-name/src/main/java/batch.java along with other subfolders like a src/resources that houses the Spring application.properties as well as the spring-config.xml. Each batch also has its own deploy folder with different environments, allowing us to place other properties files within them as needed by the program. For how it looks at the highest level, each batch is laid out something like this with a common folder in the same hierarchy:
bapps
--- batch1
--- --- deploy
--- --- src
--- --- resources
--- --- --- application.properties
--- --- --- spring-config.xml
--- batch2
--- --- deploy
--- --- src
--- --- resources
--- --- --- application.properties
--- --- --- spring-config.xml
--- batch3
--- --- deploy
--- --- src
--- --- resources
--- --- --- application.properties
--- --- --- spring-config.xml
--- common
--- --- deploy
--- --- --- common.properties
--- --- src
--- --- resources
--- --- --- common-b-config.xml
What we want is for each of these individual batches to pull in their own properties from within their personal application.properties files, what is found in their deploy directories, AND what is found within the common/deploy/common.properties file. As of now, it only pulls from its application.properties and deploy folders. We want to include common/deploy/common.properties into this flow.
The currently injected properties are within the classpath, so this is where my limited knowledge begins to fade. How do I get access to this common.properties file within other batches?
Currently we use a slightly modified PropertyPlaceholderConfigurer in each batch to point to classpath:application.properties. Everything cascades from there and as far as I know, we can't get away with just adding another route into that prop loader since any other path will override the other.
I am still a bit of a novice at Spring and while the documentation is pretty robust, I am having a hard time following it since it tends to keep itself rather project structure neutral.

Why is the JSP not found in Spring ? File exists

In a very basic app, I have the sayHelloController.java as following
#Controller
public class SayHelloController {
#RequestMapping("say-hello-jsp")
public String sayHelloJsp() {
return "sayHello";
}
}
with the following application.properties:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
logging.level.org.springframwork=debug
and the sayHello.jsp:
<html>
<head>
My first JSP file!
</head>
<body>
<h1>Hello</h1>
<p>
This is the text we want to show <br/>
<p>
</body>
</html>
When I go to http://localhost:8080/say-hello-jsp, I get a 404 and the logs give me :
: GET "/say-hello-jsp", parameters={}
: Mapped to com.in28minutes.springboot.myfirstwebapp.hello.SayHelloController#sayHelloJsp()
: Selected 'text/html' given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.9, */*;q=0.8]
: View name 'sayHello', model {}
: Forwarding to [/WEB-INF/jsp/sayHello.jsp]
: "FORWARD" dispatch for GET "/WEB-INF/jsp/sayHello.jsp", parameters={}
: Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
: "Path with "WEB-INF" or "META-INF": [WEB-INF/jsp/sayHello.jsp]"
: Resource not found
Which does not make sense, because the file exists, proof :
macbook-air:jsp gerald$ pwd
/Users/gerald/IdeaProjects/myfirstwebapp/src/main/resources/META-INF/resources/WEB-INF/jsp
macbook-air:jsp gerald$ ls
sayHello.jsp
Also, I've added jasper as a dependency in my pom.xml
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
Why is the file not retrieved ?
Found the answer here : https://youtrack.jetbrains.com/issue/IDEA-158125/Spring-boot-run-configuration-does-not-find-resources-in-META-INF , thanks Andy Wilkinson:
The problem is that Jasper is marked as provided in the pom so it's
not on the classpath. I believe there's already an IDEA issue open for
that problem. It causes Spring Boot to go down a code path where
various JSP-related bits and pieces are not enabled. You can avoid it
by removing provided from your pom as it's only
necessary when building a war file that will be deployed to a servlet
container.
I've removed the provided , ran a mvn clean (it does not work to just re-run the project) and then it worked.
Weird that this basic issue is still present in IntelliJ after 6 years (link is from 2016)

can root directory of a classpath in java or spring have a package name?

this seems a simple question, but im not able to get it to work
I have a simple spring boot java project. java.srcDirs is set to src. I'm assuming that classpath also has src now.
Can I put code files directly into src directory ? or must I create a subdirectory?
what is happening is that if i create a subdirectory demo, then i can set package demo; in code files sitting in demo.
but i cannot set any package name to files in src directory. If i leave it blank or "", i get errors as
declared package "src" does not match the expected package ""
if I leave out package name entirely, then spring boot errors out saying
** WARNING ** : Your ApplicationContext is unlikely to start due to a #ComponentScan of the default package.
2022-10-01 13:17:51.010 WARN 7999 --- [ main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:/home/user/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-autoconfigure/2.7.4/1848e5e8e474cc880a7708f03f750736665d4157/spring-boot-autoconfigure-2.7.4.jar!/org/springframework/boot/autoconfigure/r2dbc/ConnectionFactoryConfigurations$PoolConfiguration.class]; nested exception is java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryConfigurations$PoolConfiguration due to io/r2dbc/spi/ValidationDepth not found. Make sure your own configuration does not rely on that class. This can also happen if you are #ComponentScanning a springframework package (e.g. if you put a #ComponentScan in the default package by mistake)
2022-10-01 13:17:51.021 INFO 7999 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-10-01 13:17:51.036 ERROR 7999 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:/home/user/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-autoconfigure/2.7.4/1848e5e8e474cc880a7708f03f750736665d4157/spring-boot-autoconfigure-2.7.4.jar!/org/springframework/boot/autoconfigure/r2dbc/ConnectionFactoryConfigurations$PoolConfiguration.class]; nested exception is java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryConfigurations$PoolConfiguration due to io/r2dbc/spi/ValidationDepth not found. Make sure your own configuration does not rely on that class. This can also happen if you are #ComponentScanning a springframework package (e.g. if you put a #ComponentScan in the default package by mistake)
is it part of java standard that packages should be in subdirectories of srcdir/classpath ? quite confused here.
You can put the following class in the source root directory.
package demo;
public class MyClass {
}
When you compile the code, the class MyClass will be put into the output directory in the directory demo.
As this works it's not recommended. Package structure and directory structure should match.

java spring images in resources folder not available (404 error)

I am working on a Web Application using the Spring Framework in version 4.1.5
I have a resources folder configured and set up correctly and am already loading css, js and image files out of that.
<mvc:resources mapping="/images/**" location="/resources/" />
However, I am also using the resources folder for uploading images into it and displaying them in the frontend. And there's a point where I'm currently stuck and am not proceeding my understanding. I am getting 404 Error (resource not available), while others are working perfectly fine. The paths are all correct and the files are successfully uploaded, I am sure of that.
I have also seen the reverse effect - renaming or even removing resources out of the resources folder does not impact their accessibility over their web url. I am not copying any files to another folder during building the application, so I am working with those files in the resources folder. I also have looked for the filename on my filesystem and its not there anymore, so I assume there must be some cache in place.
Does Spring build an index or cache over the existing resource files at startup? Do I have to refresh any ApplicationContext after uploading files to notify the framework of changes in the file system and if so, how do I do that?
Hopefully someone can help me here :)
Thank you,
Tom
EDIT:
When calling an URL like: localhost:8080/EstelV2/images/testimage.jpg
I run into a 404 resource is not available, even though the testimage.jpg is in the correct folder structure on the file system and get the log output:
2015-04-30 22:13:05,035 - [DEBUG] - [http-nio-8080-exec-4] (DispatcherServlet.java:845) - DispatcherServlet with name 'spring-dispatcher' processing GET request for [/EstelV2/images/testimage.jpg]
2015-04-30 22:13:05,035 - [DEBUG] - [http-nio-8080-exec-4] (AbstractHandlerMethodMapping.java:297) - Looking up handler method for path /images/testimage.jpg
2015-04-30 22:13:05,041 - [DEBUG] - [http-nio-8080-exec-4] (AbstractHandlerMethodMapping.java:305) - Did not find handler method for [/images/testimage.jpg]
2015-04-30 22:13:05,041 - [DEBUG] - [http-nio-8080-exec-4] (AbstractUrlHandlerMapping.java:168) - Matching patterns for request [/images/testimage.jpg] are [/images/**]
2015-04-30 22:13:05,042 - [DEBUG] - [http-nio-8080-exec-4] (AbstractUrlHandlerMapping.java:193) - URI Template variables for request [/images/testimage.jpg] are {}
2015-04-30 22:13:05,042 - [DEBUG] - [http-nio-8080-exec-4] (AbstractUrlHandlerMapping.java:123) - Mapping [/images/testimage.jpg] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/resources/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver#280f257d]]] and 1 interceptor
2015-04-30 22:13:05,042 - [DEBUG] - [http-nio-8080-exec-4] (DispatcherServlet.java:931) - Last-Modified value for [/EstelV2/images/testimage.jpg] is: -1
2015-04-30 22:13:05,042 - [DEBUG] - [http-nio-8080-exec-4] (DispatcherServlet.java:1018) - Null ModelAndView returned to DispatcherServlet with name 'spring-dispatcher': assuming HandlerAdapter completed request handling
2015-04-30 22:13:05,042 - [DEBUG] - [http-nio-8080-exec-4] (FrameworkServlet.java:996) - Successfully completed request
Soo after hours of investigations I came to a conclusion:
My problem was not related to spring, it was a problem about my eclipse project. It seemed, as long as I didn't refresh my project in eclipse, the newly uploaded files were not accessible by the tomcat server - and the old ones which were removed from the file system but still were in the eclipse project kept being accessible.
I turned on both auto-refresh options at Preferences -> General -> Workspace -> "Refresh using native hooks and polling" and "Refresh on access", which solved my problem for now.
Thank you for your help!
servlet specification not mention that WEB_INF is Document root
A special directory exists within the application hierarchy named WEB-INF. This directory contains all things related to the application that aren’t in the document root of the application. The WEB-INF node is not part of the public document tree of the application. No file contained in the WEB-INF directory may be served directly to a client by the container. However, the contents of the WEB-INF directory are visible to servlet code using the getResource and getResourceAsStream method calls on the ServletContext, and may be exposed using the RequestDispatcher calls. so use it for secure your content only but for images put it in other folder in your web path

Spring / Glassfish 3.1.2 stale files

I'm currently developping an application using Spring Web MVC & Spring Security.
I can deploy it on Glassfish 3.1.2 but when I want to redeploy after some bug fixing, I get this error :
WARNING: Exception while locating sub archive: XXX.war
WARNING: DPL8031: Ignoring XXX_war/ because the containing archive C:\dev\apps\glassfish3\glassfish\domains\domain1\applications\XXX recorded it as a pre-existing stale file
.glassfishStaleFiles contains :
XXX_war/
XXX_war/WEB-INF/
XXX_war/WEB-INF/lib/
XXX_war/WEB-INF/lib/spring-web-3.2.2.RELEASE.jar
And indeed, spring-web-3.2.2.RELEASE.jar still exists and when I try to remove it, I get a message that says It's still in use by JVM.
I found this link :
https://www.java.net//forum/topic/glassfish/glassfish/stale-files-when-undeploying-web-app-glassfish-3
My question is : does someone have a workaround for this issue ?
It's really annoying while developping...
To correct this problem, I have an easier solution :
Assuming your EAR file is correct:
stop the server
goto your EAR directory :
C:\java_apps\glassfish3\glassfish\domains\domain1\applications\XXX_EAR
delete the file called .glassfishStaleFiles
restart server and re-deploy EAR file
It did the trick for me.
Ok, after some days of research...
I finally found how to get around.
My project hierarchy was :
XXX.ear
--- lib/
------ XXX_Entities.jar
--- XXX_Ejb.jar
--- XXX_EjbClient.jar
--- XXX_Web.war
------ WEB-INF/lib/
--------- *libraries*
Now :
XXX.ear
--- lib/
------ XXX_Entities.jar
------ *libraries*
--- XXX_Ejb.jar
--- XXX_EjbClient.jar
--- XXX_Web.war
Then add lib folder to EAR deployment assembly and it works. Glassfish unload correctly when undeploying.

Categories