gpt4 book ai didi

java - 为什么我的应用程序VSCode不能在CLI中正确运行?它是一个 Spring 启动应用程序

转载 作者:行者123 更新时间:2023-12-03 04:47:55 25 4
gpt4 key购买 nike

我在Spring Boot中构建一个Web应用程序。
开发环境是Gradle,我正在使用Visual Studio代码。
我正在使用Visual Studio代码的调试功能,Spring Boot仪表板,并且在启动应用程序时,页面可以正确显示,但是当我将Web应用程序设为jar文件并使用java -jar运行时,它不适用于某些页面过渡。
请告诉我我应该做些什么才能使其正常工作以及正在发生的事情。
(我认为spring boot无法找到某些路径,但是我不知道这是什么以及如何修复它。)
这是我访问“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).
以下是我使用java -jar访问特定画廊页面时遇到的错误。
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]
Controller 类中的方法如下。 “索引”和“关于”是有效的。
    @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类如下。
它是获取 .jpg图像,添加List并返回。
@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>
源树就是这样

└── 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

最佳答案

如果将应用程序打包在jar中,则它不再依赖于源代码。
如果使用另一个文件夹或其他服务器上的java -jar ...启动应用程序,则new File("src/main/resources/static/images")将不再起作用。
我认为当您在项目的根文件夹中执行java -jar ...时它将起作用,但这可能不是一个长期解决方案。
Maven认为src/main/resources文件夹是需要包含在jar中的文件的输入文件夹,因此打包后,这些文件将以getClass().getResource("/static/images/202008/1.jpg")的形式从java中提供,这就是它们在Spring Boot中的服务方式(使用ResourceHttpRequestHandler,而无需static/前缀)。
如果要稍后添加图像而不必重建和重新包装应用程序,则最好使用绝对服务器路径,例如"C:\gallery\images""/opt/gallery/images"

关于java - 为什么我的应用程序VSCode不能在CLI中正确运行?它是一个 Spring 启动应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63690133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com