I'm building a web application in spring boot.
The development environment is Gradle and I am using visual studio code.
I'm using visual studio code's debug function, spring boot dashboard, and when I start the application, the page is displayed correctly, but when I make the web application a jar file and run it with java -jar
, it doesn't work with some page transitions.
Please tell me what I should do to make it work and what is happening. (I think that spring boot can't find the some path, but I don't know what is and how to fix it.)
Here's the page I was on when I accessed 「localhost:8080/gallery」
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Sep 01 23:09:16 JST 2020
There was an unexpected error (type=Internal Server Error, status=500).
The following is the error I get when I use java -jar to access a specific gallery page.
2020-09-01 23:09:14.129 INFO 15464 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-09-01 23:09:14.143 INFO 15464 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 14 ms
Check image file path
2020-09-01 23:09:16.656 ERROR 15464 --- [nio-8080-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
at com.example.sweepea.GetImage.getImage(GetImage.java:23) ~[classes!/:na]
at com.example.sweepea.swepeaController.getGallery(swepeaController.java:21) ~[classes!/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
The methods in the controller class are as follows. 「index」and「about」is work.
@GetMapping("/index")
public String getIndex(){
return "index";
}
@GetMapping("/gallery")
public String getGallery(Model model){
List<String> imagePath = new GetImage().getImage(new File("src/main/resources/static/images"));
model.addAttribute("imagePath", imagePath);
return "gallery";
}
@GetMapping("/about")
public String getAbout(){
return "about";
}
}
GetImage class are as follw. It is get .jpg
images , add List and return.
@Service
public class GetImage {
String month = "202009";
List<String> pathname = new ArrayList<String>();
List<String> getImage(File file){
File[] filelist = file.listFiles();
if(filelist == null){
System.out.println("Check image file path");
}
for (File tmpFile : filelist){
if(tmpFile.isDirectory()){
month = tmpFile.getName();
//System.out.println(month);
//pathname.add(month);
getImage(tmpFile);
}else{
String imagePath = "images/" + month + "/" + tmpFile.getName();
if(imagePath.substring(imagePath.length() - 3).equals("jpg")){
pathname.add(imagePath);
//System.out.println(imagePath);
}
}
}
return pathname;
}
}
gallery.html
<body>
<div id="content">
<header class="page_header wrapper" >
<h1><a th:href="@{'/index'}"><img class="logo" th:src="@{images/logo.svg}" alt=""></a></h1>
<nav class="main_nav">
<ul>
<ii><a th:href="@{'/index'}">TOP</a></ii>
<ii><a th:href="@{'/gallery'}">GALLERY</a></ii>
<ii><a th:href="@{'/about'}">ABOUT</a></ii>
</ul>
</nav>
</header>
<main>
<div class="edition">
<p>sub title</p>
</div>
<h2 class="mounth">Gallery</h2>
<div class="grid item">
<a th:each="image : ${imagePath}" th:href="@{${image}}"><img th:src="@{${image}}" alt=""></a>
</div>
</main>
</body>
source tree is like that
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── example
│ │ └── sweepea
│ │ ├── DemoApplication.java
│ │ ├── GetImage.java
│ │ ├── Test.java
│ │ └── swepeaController.java
│ └── resources
│ ├── application.properties
│ ├── static
│ │ ├── logo.svg
│ │ ├── images
│ │ │ ├── 201910
│ │ │ │ ├── 1.jpg
│ │ │ ├── 201911
│ │ │ ├── 201912
│ │ │ │ ├── 1.jpg
│ │ │ ├── 202001
│ │ │ ├── 202002
│ │ │ │ ├── 1.jpg
│ │ │ ├── 202003
│ │ │ │ ├── 1.jpg
│ │ │ ├── 202004
│ │ │ │ ├── 1.jpg
│ │ │ ├── 202005
│ │ │ │ ├── 1.jpg
│ │ │ ├── 202006
│ │ │ ├── 202007
│ │ │ ├── 202008
│ │ │ │ ├── 1.jpg
│ │ │ ├── 202009
│ │ │ ├── logo.svg
│ │ │ └── logo_favicon.png
│ │ └── style.css
│ └── templates
│ ├── about.html
│ ├── gallery.html
│ ├── index.html
│ └── video.html
└── test
└── java
└── com
└── example
└── sweepea
└── DemoApplicationTests.java
If you package you application in a jar, it's no longer dependent on the source code.
If you start your application using java -jar ...
in another folder or on another server, then new File("src/main/resources/static/images")
will no longer work.
I think it will work when you execute java -jar ...
in the root folder of your project, but that's probably not a long-term solution.
Maven considers the src/main/resources
folder as an input folder for files that need to be included in the jar, so after packaging, those files will be available from java as getClass().getResource("/static/images/202008/1.jpg")
, and that's how they will be served in Spring Boot (using the ResourceHttpRequestHandler, without the static/
prefix).
If you want to add images later without having to rebuild and repackage your application, it's better to use an absolute server path like "C:\gallery\images"
or "/opt/gallery/images"
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments