英文:
Spring Boot application can't be reached from browser nor Postman
问题
我有一个简单的Spring Boot应用程序,在从localhost:8899访问时提供一些静态资源。
我在电脑上安装了Tomcat,并尝试部署war文件,但始终返回404响应。接下来,我想修改Spring Boot应用程序中的一些内容,但现在如果我启动应用程序,无法访问它。
我还尝试使用Postman来查看是否至少rest控制器正在工作,但我得到了“无法连接到localhost:8899”的消息。
最终,我卸载了Tomcat服务,也卸载了Tomcat,并恢复到先前的提交,这在之前是正常工作的,但结果相同。该应用程序在端口8899上启动而没有错误,但无法通过从浏览器访问静态资源或从Postman发送请求到rest控制器来获得任何响应。
我不知道要查找什么,因为除了截图中所见的内容外,我没有任何错误信息。
pom.xml
<!-- pom.xml内容略 -->
application.properties
# application.properties内容略
ReminderController.java
// ReminderController.java内容略
英文:
I have a simple Spring Boot application that serves some static resources when accessed from localhost:8899.
I installed tomcat on my pc and tried to deploy the war file, but it kept giving me a 404 response. Next, I wanted to modify some things in the spring boot app, but now if I start up the app, I can't reach it.
I also tried to use Postman to see if at least the rest controller is working, but I get the "There was an error connecting to localhost:8899" message.
I ended up uninstalling the tomcat service, I also uninstalled tomcat and reverted to a previous commit on the application, which was working before, but I get the same result. The app starts up at port 8899 without errors, but I can't get any response neither by accessing the static resources from the browser nor from sending a request from Postman to the rest controller.
I have no clue what to look for because I get no errors other than what you can see in the screenshot.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>todoey-backend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>todoey-backend</name>
<description>Reminder application</description>
<properties>
<java.version>1.10</java.version>
</properties>
<parent>
<groupId>org.reminder</groupId>
<artifactId>todoey-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>10</source>
<target>10</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties
server.port=8899
# DB
spring.datasource.url=jdbc:h2:file:./todoey-be/src/main/resources/db/todoey_db
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update
ReminderController.java
package com.reminder.todoey.controller;
import com.reminder.todoey.model.Reminder;
import com.reminder.todoey.service.ReminderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("reminder")
public class ReminderController {
@Autowired
private ReminderService reminderService;
@GetMapping
public ResponseEntity<List<Reminder>> getAllReminders() {
List<Reminder> reminderList = reminderService.getAllReminders();
return ResponseEntity.ok(reminderList);
}
@PostMapping
public ResponseEntity writeReminder(@RequestBody final Reminder reminder) {
reminderService.saveReminder(reminder);
return ResponseEntity.status(HttpStatus.OK).build();
}
@DeleteMapping
public ResponseEntity deleteReminder(@RequestParam final long id) {
reminderService.deleteReminder(id);
return ResponseEntity.status(HttpStatus.OK).build();
}
@PutMapping
public ResponseEntity updateReminder(@RequestBody final Reminder reminder) {
reminderService.updateReminder(reminder);
return ResponseEntity.status(HttpStatus.OK).build();
}
@PutMapping(path = "/email-address")
public ResponseEntity updateEmailAddress(@RequestParam final String email) {
reminderService.updateEmailAddress(email);
return ResponseEntity.status(HttpStatus.OK).build();
}
@GetMapping(path = "/email-address")
public ResponseEntity<String> getEmailAddress() {
return ResponseEntity.ok(reminderService.getEmailAddress());
}
}
答案1
得分: 0
我成功找出了为什么我无法访问我的应用程序。我有一个带有无限循环的异步方法,它在检查时间以确定何时发送提醒。这个方法被同时标注为@Async和@PostConstruct,因为我希望它在应用程序启动时被调用。删除@PostConstruct之后,一切都正常工作了。
英文:
I managed to figure out why I couldn't reach my application. I had an async method with and infinite loop that was checking the time to see when to send the reminders. This method was annotated with both @Async and @PostConstruct because I wanted it to be called as soon as the application starts. After deleting @PostConstruct, everything worked just fine.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论