Spring Boot应用无法从浏览器或Postman访问。

huangapple go评论80阅读模式
英文:

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.

Spring Boot应用无法从浏览器或Postman访问。

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    
    &lt;artifactId&gt;todoey-backend&lt;/artifactId&gt;
    &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
    &lt;packaging&gt;war&lt;/packaging&gt;
    &lt;name&gt;todoey-backend&lt;/name&gt;
    &lt;description&gt;Reminder application&lt;/description&gt;

    &lt;properties&gt;
        &lt;java.version&gt;1.10&lt;/java.version&gt;
    &lt;/properties&gt;

    &lt;parent&gt;
        &lt;groupId&gt;org.reminder&lt;/groupId&gt;
        &lt;artifactId&gt;todoey-parent&lt;/artifactId&gt;
        &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
    &lt;/parent&gt;

    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
            &lt;artifactId&gt;lombok&lt;/artifactId&gt;
            &lt;optional&gt;true&lt;/optional&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;com.h2database&lt;/groupId&gt;
            &lt;artifactId&gt;h2&lt;/artifactId&gt;
            &lt;scope&gt;runtime&lt;/scope&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
            &lt;artifactId&gt;spring-data-commons&lt;/artifactId&gt;
            &lt;version&gt;2.2.6.RELEASE&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-mail&lt;/artifactId&gt;
            &lt;version&gt;2.2.5.RELEASE&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;

    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
            &lt;/plugin&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                &lt;configuration&gt;
                    &lt;source&gt;10&lt;/source&gt;
                    &lt;target&gt;10&lt;/target&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;

&lt;/project&gt;

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(&quot;reminder&quot;)
public class ReminderController {

    @Autowired
    private ReminderService reminderService;

    @GetMapping
    public ResponseEntity&lt;List&lt;Reminder&gt;&gt; getAllReminders() {
        List&lt;Reminder&gt; 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 = &quot;/email-address&quot;)
    public ResponseEntity updateEmailAddress(@RequestParam final String email) {
        reminderService.updateEmailAddress(email);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    @GetMapping(path = &quot;/email-address&quot;)
    public ResponseEntity&lt;String&gt; 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.

huangapple
  • 本文由 发表于 2020年10月27日 04:45:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64544716.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定