Home > Net >  404 error in spring boot application on the server but it works on localhost
404 error in spring boot application on the server but it works on localhost

Time:01-12

I made an application and ran it via inteliji. All was well until I wated to move the application onto a server. I am getting a 404 error at the head of it, and my logs are empty. 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>spring-boot-hello</artifactId>
    <packaging>war</packaging>
    <name>Spring Boot Hello World Example</name>
    <version>1.0</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
    </parent>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <version>8.0.31</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.6.1</version>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>webjars-locator-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>5.2.3</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <finalName>nameofwar</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

demoapplication.java

package com.example;

import com.example.demo.user.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(DemoApplication.class);
    }
}

appcontroller

@RestController
public class AppController {

    @Autowired
    private final UserRepository userRepo;
    private final AssignmentRepository assignmentRepo;
    private final AssignmentService assignmentService;
    private final ConfigService configService;
    private final UserService userService;

    @Autowired
    public AppController(UserRepository userRepo, AssignmentService assignmentService,
                         AssignmentRepository assignmentRepo, ConfigService configService, UserService userService) {
        this.userRepo = userRepo;
        this.assignmentService = assignmentService;
        this.assignmentRepo = assignmentRepo;
        this.configService = configService;
        this.userService = userService;

    }

    @GetMapping("")
    public String viewHomePage() {
        return "index";

    }
}

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/<mydb>
spring.datasource.username=
spring.datasource.password=
spring.sql.init.mode=always
spring.sql.init.platform=mysql
spring.jpa.hibernate.ddl-auto=update

server.error.include-message=always
logging.level.=DEBUG

spring.servlet.multipart.max-file-size=128MB
spring.servlet.multipart.max-request-size=128MB
spring.http.multipart.enabled=true

server.error.include-exception=true
server.error.include-stacktrace=always

index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="ISO-8859-1">
    <title>warname</title>
    <link rel="stylesheet" type="text/css" href="/webjars/bootstrap/css/bootstrap.min.css" />
    <script type="text/javascript" src="/webjars/jquery/jquery.min.js"></script>
    <script type="text/javascript" src="/webjars/bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
<div >
    <h2>Welcome</h2>
    <!--<h3><a href="/@{/login}">Login</a></h3> -->
    <div>
        <h3><a th:href="@{/register}">Register</a> </h3>
        <h3><a th:href="@{/login}"</a>Login</h3>
    </div>
</div>

</body>
</html>

where I'm trying to go : /nameofwar

I've looked through a hundred posts trying to find out what's going wrong and haven't found it yet. I'm using tomcat 9, java 11, java 11 on the server as well. Thanks for your help

CodePudding user response:

Annotate your AppController with @org.springframework.stereotype.Controller instead of a RestController and try to use org.springframework.web.servlet.ModelAndView instead of a string

@GetMapping("")
public ModelAndView viewHomePage() {
    return new ModelAndView("index");
}

CodePudding user response:

turns out I did not have my application.properties file filled out correctly to connect with my database. fixing that fixed the error.

spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=create
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver


spring.datasource.url=jdbc:mysql://localhost:3306/<application_name>
spring.datasource.username=<username>
spring.datasource.password=<password>

and I needed to use Controller and not RestController everywhere

  • Related