简体   繁体   中英

Spring Boot application can't be reached from browser nor Postman

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());
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM